ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

springboot集成redisson

2021-12-29 12:00:02  阅读:160  来源: 互联网

标签:集成 redisson springboot cache redis celery ttl id


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
  # 连接池大小
  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
    @ComponentScan
    @EnableCaching
    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);
}

标签:集成,redisson,springboot,cache,redis,celery,ttl,id
来源: https://blog.csdn.net/broccoli2/article/details/122211361

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有