Springboot整合Sping-data-redis
导入依赖*
版本:springboot 2.0.6.RELEASE
导入依赖
设置redis连接信息
前提:关闭linux中的防火墙,以文件的方式开启redis
使用StringRedisTemplate进行插入操作
实现对象存入redis
springboot中需要自定义实现对象的序列化操作进行对象的存储
使用Jackson提供的序列化功能
SpringBoot集成CaffeineCache
学习地址:CaffeineCache+高性能使用
导入springcache的依赖和caffeinecache的依赖
在SpringBoot的启动类上开启本地缓存
配置CaffeineCahche缓存信息
CaffeineCache构造参数说明
Create a {@link CaffeineCache} instance with the specified name and the
given internal {@link com.github.benmanes.caffeine.cache.Cache} to use.
@param name the name of the cache 缓存的名字
@param cache the backing Caffeine Cache instance CaffeineCache实例
@param allowNullValues whether to accept and convert {@code null} 该缓存实例是否接受null值并将其进行装换
CaffeineCache实例的创建允许流式的创建方式
expireAfterWrite(1000, TimeUnit.SECONDS)
:指定缓存写入后的过期时间
maximumSize(10000)
:指定缓存的最大条数
recordStats()
:用于开启缓存的统计功能
SpringCache和redis
添加依赖
在配置文件中设定使用的缓存为redis
自定义配置
Cacheable测试
测试结果
可以看出这里只执行了一次方法体,之后去缓存里面查询
CacheEvict测试
测试结果
CachePut测试
测试结果