springboot集成redisson

1.添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.13.6</version>
        </dependency>

2.redisson配置文件(redisson.yaml)

# 单节点配置
singleServerConfig:
  # 连接空闲超时,单位:毫秒
  idleConnectionTimeout: 10000
  # 连接超时,单位:毫秒
  connectTimeout: 10000
  # 命令等待超时,单位:毫秒
  timeout: 3000
  # 命令失败重试次数,如果尝试达到 retryAttempts(命令失败重试次数) 仍然不能将命令发送至某个指定的节点时,将抛出错误。
  # 如果尝试在此限制之内发送成功,则开始启用 timeout(命令等待超时) 计时。
  retryAttempts: 3
  # 命令重试发送时间间隔,单位:毫秒
  retryInterval: 1500
  # 密码
  password: null
  # 单个连接最大订阅数量
  subscriptionsPerConnection: 5
  # 客户端名称
  clientName: null
  #  # 节点地址
  address: redis://ip:6489     //这里是redis的ip地址和端口
  # 发布和订阅连接的最小空闲连接数
  subscriptionConnectionMinimumIdleSize: 1
  # 发布和订阅连接池大小
  subscriptionConnectionPoolSize: 50
  # 最小空闲连接数
  connectionMinimumIdleSize: 32
  #ping时间间隔
  pingConnectionInterval: 10000
  # 连接池大小
  connectionPoolSize: 64
  # 数据库编号
  database: 0
  # DNS监测时间间隔,单位:毫秒
  dnsMonitoringInterval: 5000
# 线程池数量,默认值: 当前处理核数量 * 2
#threads: 0
# Netty线程池数量,默认值: 当前处理核数量 * 2
#nettyThreads: 0
# 编码
codec: !<org.redisson.codec.JsonJacksonCodec> {}
# 传输模式
transportMode : "NIO"

3.配置文件(application.properties)

//是否启用缓存
com.celery.cache.enable=true
//缓存相关配置
com.celery.cache.config-file=/src/main/resources/redisson.yaml
//缓存超时时间
com.celery.cache.ttl-seconds=3600

4.redis配置类

    @Configuration
    @EnableCaching
    @ConditionalOnProperty(prefix = "com.celery.cache", name = "enable", havingValue = "true", matchIfMissing = false)
    public static class RedisConfig {
    
		@Value("${com.celery.cache.config-file}")
		private String configFile;
		@Value("${com.celery.cache.ttl-seconds:1800}")		
		private long ttlSeconds;

        @Bean
        RedissonClient redisson() Resource configFile) throws IOException {
            Config config = Config.fromYAML(configFile);
            return Redisson.create(config);
        }

        @Bean
        CacheManager cacheManager(RedissonClient redissonClient) throws IOException {
            return new RedissonSpringCacheManager(redissonClient, "classpath:/cache-config.json");
        }

		public static class MyRedisCacheManager extends RedissonSpringCacheManager{
			private final long ttl;
			public MyRedisCacheManager(RedissonClient  redisson, long ttl){\
				super(redisson);
				this.ttl = ttl;
			}
			@Override
			protected CacheConfig createDefaultConfig(){
			return new CacheConfig(ttl*1000, ttl*1000);
		}
		}
    }

5.使用缓存

实际使用一般使用@Caching注解、@Cacheable注解,可以放在mapper方法上,service层方法上。
(1)mapper方法使用缓存

@Cacheable(cacheNames = "user", key = "#id")
@Select("select * from user where id=#{id}")
User findById(int id);

(2)service
service层的key可以为具体的字段如id、name,也可以用对象作为key,如User对象,指定时key="#user"即可

@Cacheable(cacheNames = "findUser", key = "#id")
public User findById(int id){
	return userMapper.findById(id);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值