Redisson提供了将Redis无缝整合到Spring框架的能力。Redisson依照Spring Cache标准提供了基于Redis的Spring缓存实现。 每个缓存(Cache)实例都提供了了两个重要的可配置参数:过期时间(ttl)
和最长空闲时间(maxIdleTime)
,如果这两个参数都未指定或值为0
,那么实例管理的数据将永久保存。
Jar包
-
<!-- JDK 1.8+ compatible -->
-
<dependency>
-
<groupId>org.redisson</groupId>
-
<artifactId>redisson</artifactId>
-
<version>3.7.5</version>
-
</dependency>
RedissonCacheConfig 缓存配置类
创建一个RedissonClient客户端对象,生成了CacheManager缓存管理器,配置一个名叫“testMap”的缓存器,最大缓存时间为24分钟,最大空闲时间12分钟(客户端连接过期时间)
-
@Configuration
-
@EnableCaching
-
public class RedissonCacheConfig {
-
@Bean(destroyMethod="shutdown")
-
RedissonClient redisson() throws IOException {
-
Config config = new Config();
-
//config.setCodec(new JsonJacksonCodec()); 默认
-
config.useClusterServers()
-
.addNodeAddress("redis://192.168.56.128:7000", "redis://192.168.56.128:7001","redis://192.168.56.128:7002");
-
return Redisson.create(config);
-
}
-
@Bean
-
CacheManager cacheManager(RedissonClient redissonClient){
-
Map<String,CacheConfig> config = new HashMap<>(16);
-
// create "testMap" cache with ttl = 24 minutes and maxIdleTime = 12 minutes
-
config.put("testMap",new CacheConfig(24*60*1000,12*60*1000));
-
return new RedissonSpringCacheManager(redissonClient,config);
-
}
-
}
或者使用yml读取方式配置
-
/**
-
* redisson客户端
-
* @return
-
* @throws IOException
-
*/
-
@Bean(destroyMethod = "shutdown")
-
public RedissonClient redissonClient() throws IOException{
-
Config config = Config.fromYAML(new ClassPathResource("redisson.yml").getInputStream());
-
//config.setCodec(new JsonJacksonCodec()); 默认
-
return Redisson.create(config);
-
}
redisson.yml
-
clusterServersConfig:
-
# 连接空闲超时 如果当前连接池里的连接数量超过了最小空闲连接数,而同时有连接空闲时间超过了该数值,那么这些连接将会自动被关闭,并从连接池里去掉。时间单位是毫秒。
-
idleConnectionTimeout: 10000
-
pingTimeout: 1000
-
# 连接超时
-
connectTimeout: 10000
-
# 命令等待超时
-
timeout: 3000
-
# 命令失败重试次数
-
retryAttempts: 3
-
# 命令重试发送时间间隔
-
retryInterval: 1500
-
# 重新连接时间间隔
-
reconnectionTimeout: 3000
-
# failedAttempts
-
failedAttempts: 3
-
# 密码
-
password: null
-
# 单个连接最大订阅数量
-
subscriptionsPerConnection: 5
-
# 客户端名称
-
clientName: null
-
#负载均衡算法类的选择 默认轮询调度算法RoundRobinLoadBalancer
-
loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
-
slaveSubscriptionConnectionMinimumIdleSize: 1
-
slaveSubscriptionConnectionPoolSize: 50
-
# 从节点最小空闲连接数
-
slaveConnectionMinimumIdleSize: 32
-
# 从节点连接池大小
-
slaveConnectionPoolSize: 64
-
# 主节点最小空闲连接数
-
masterConnectionMinimumIdleSize: 32
-
# 主节点连接池大小
-
masterConnectionPoolSize: 64
-
# 只在从服务节点里读取
-
readMode: "SLAVE"
-
nodeAddresses:
-
- "redis://192.168.56.128:7000"
-
- "redis://192.168.56.128:7001"
-
- "redis://192.168.56.128:7002"
-
#集群扫描间隔时间 单位毫秒
-
scanInterval: 1000
-
threads: 0
-
nettyThreads: 0
-
codec: !<org.redisson.codec.JsonJacksonCodec> {}
UserServiceImpl 服务类
@Cacheable 属性value指定缓存,属性key指定缓存对象的标识
-
@Service("userService")
-
public class UserServiceImpl implements UserService {
-
private UserMapper userMapper;
-
@Cacheable(value="testMap",key="targetClass.getName()+'.'+methodName+'.'+#id")
-
@Override
-
public User searchUserById(int id) throws Exception {
-
return userMapper.findUserById(id);
-
}
-
}
当我们去执行userService的searchUserById服务方法时,第一次执行去数据库(mysql)中查询数据(耗时较大),完成后将结果缓存到分布式的redis中,再次查询时直接从redis缓存中取出并返回(耗时较小)
连接redis 观察存储对象值
./redis-cli -h 192.168.56.129 -p 7000 -c # 需要 -c命令 表示是集群连接,不然进行redis操作,会报(error) MOVED错误
存储结构为hash,key值为@Cacheable的key属性,value值为对象的json形式
参考