[关闭]
@File 2019-11-28T09:46:32.000000Z 字数 4074 阅读 144

spring-boot redis配置

java


一、 依赖

  1. <!-- redis -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-redis</artifactId>
  5. </dependency>
  6. <!-- fastjson -->
  7. <dependency>
  8. <groupId>com.alibaba</groupId>
  9. <artifactId>fastjson</artifactId>
  10. <version>1.2.46</version>
  11. </dependency>
  12. <!-- pool 连接池 -->
  13. <dependency>
  14. <groupId>org.apache.commons</groupId>
  15. <artifactId>commons-pool2</artifactId>
  16. <version>2.7.0</version>
  17. </dependency>

二、 yml配置

  1. spring:
  2. redis:
  3. # 选库:0~16
  4. database: 0
  5. # ip地址
  6. host: 127.0.0.1
  7. # 端口号
  8. port: 6379
  9. # 密码
  10. #password:
  11. jedis:
  12. pool:
  13. # 连接池最大连接数(使用负值表示没有限制)
  14. max-active: 200
  15. # 连接池最大阻塞等待时间(使用负值表示没有限制)
  16. max-wait: -1ms
  17. # 连接池中的最大空闲连接
  18. max-idle: 10
  19. # 连接池中的最小空闲连接
  20. min-idle: 0
  21. # 连接超时时间
  22. timeout: 1000ms
  1. lettuce:
  2. pool:
  3. min-idle:
  4. max-active:

三、 config配置

  1. @Configuration
  2. public class RedisConfiguration {
  3. /**
  4. * 注册 RedisTemplate
  5. * @param factory
  6. * @return
  7. */
  8. @Bean
  9. public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
  10. RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
  11. // 注册到系统中去
  12. redisTemplate.setConnectionFactory(factory);
  13. // 1.2.36 低版本
  14. GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer();
  15. // 需要改 value 序列化方式
  16. redisTemplate.setValueSerializer(serializer);
  17. // 修改 key 序列化方式
  18. redisTemplate.setKeySerializer(new StringRedisSerializer());
  19. return redisTemplate;
  20. }
  21. /**
  22. * 注册 hash
  23. * @param redisTemplate
  24. * @return
  25. */
  26. @Bean
  27. public HashOperations<String,String,Object> hashOperations(RedisTemplate<String,Object> redisTemplate){
  28. return redisTemplate.opsForHash();
  29. }
  30. /**
  31. * 注册 list
  32. * @param redisTemplate
  33. * @return
  34. */
  35. @Bean
  36. public ListOperations<String,Object> listOperations(RedisTemplate<String,Object> redisTemplate){
  37. return redisTemplate.opsForList();
  38. }
  39. /**
  40. * 注册 value
  41. * @param redisTemplate
  42. * @return
  43. */
  44. @Bean
  45. public ValueOperations<String,Object> valueOperations(RedisTemplate<String,Object> redisTemplate){
  46. return redisTemplate.opsForValue();
  47. }
  48. /**
  49. * 注册 set
  50. * @param redisTemplate
  51. * @return
  52. */
  53. @Bean
  54. public SetOperations<String,Object> setOperations(RedisTemplate<String,Object> redisTemplate){
  55. return redisTemplate.opsForSet();
  56. }
  57. /**
  58. * 注册 zSet
  59. * @param redisTemplate
  60. * @return
  61. */
  62. @Bean
  63. public ZSetOperations<String,Object> zSetOperations(RedisTemplate<String,Object> redisTemplate){
  64. return redisTemplate.opsForZSet();
  65. }
  66. /**
  67. * 注册 geo
  68. * @param redisTemplate
  69. * @return
  70. */
  71. @Bean
  72. public GeoOperations<String,Object> geoOperations(RedisTemplate<String,Object> redisTemplate){
  73. return redisTemplate.opsForGeo();
  74. }
  75. /**
  76. * 注册 cluster
  77. * @param redisTemplate
  78. * @return
  79. */
  80. @Bean
  81. public ClusterOperations<String,Object> clusterOperations(RedisTemplate<String,Object> redisTemplate){
  82. return redisTemplate.opsForCluster();
  83. }
  84. /**
  85. * 注册 hyperLogLog
  86. * @param redisTemplate
  87. * @return
  88. */
  89. @Bean
  90. public HyperLogLogOperations<String,Object> hyperLogLogOperations(RedisTemplate<String,Object> redisTemplate){
  91. return redisTemplate.opsForHyperLogLog();
  92. }
  93. }

四、 具体调用

>> 工具类 <<

五、 Mybatis二级缓存

1. 缓存类的实现

  1. public class RedisCache implements Cache {
  2. /**
  3. * redis 对象(不通过注入获取)
  4. */
  5. private RedisTemplate<Object, Object> redisTemplate;
  6. private String id;
  7. public RedisCache(){}
  8. /**
  9. * 构造时传入一个id
  10. */
  11. public RedisCache(String id) {
  12. this.id = id;
  13. }
  14. /**
  15. * 获取 redis 对象
  16. * @return 返回 RedisTemplate 对象
  17. */
  18. private RedisTemplate<Object, Object> getRedisTemplate() {
  19. if(null == this.redisTemplate) {
  20. // 注意要检查是否创建了 ApplicationContextHolder
  21. this.redisTemplate = ApplicationContextHolder.getBean("redisTemplate", RedisTemplate.class);
  22. }
  23. return this.redisTemplate;
  24. }
  25. @Override
  26. public String getId() {
  27. return this.id;
  28. }
  29. @Override
  30. public void putObject(Object key, Object value) {
  31. if(value != null) {
  32. this.getRedisTemplate().opsForValue().set(key, value);
  33. }
  34. }
  35. @Override
  36. public Object getObject(Object key) {
  37. if(null != key) {
  38. return this.getRedisTemplate().opsForValue().get(key);
  39. }
  40. return null;
  41. }
  42. @Override
  43. public Object removeObject(Object key) {
  44. if(null != key) {
  45. this.getRedisTemplate().delete(key);
  46. }
  47. return null;
  48. }
  49. @Override
  50. public void clear() {
  51. Set<Object> set = getRedisTemplate().keys("*:" + this.id + "*");
  52. if(null != set) {
  53. getRedisTemplate().delete(set);
  54. }
  55. }
  56. @Override
  57. public int getSize() {
  58. return 0;
  59. }
  60. @Override
  61. public ReadWriteLock getReadWriteLock() {
  62. return new ReentrantReadWriteLock();
  63. }
  64. }

2. mapper.xml配置

  1. <!--
  2. flushInterval: 清空缓存的时间间隔,单位为毫秒; 默认情况是不设置,也就是没有刷新间隔,缓存仅仅调用更新语句时刷新。
  3. size: 可以被设置为任意正整数, 缓存的数量,默认是1024;
  4. evication: LRU 移除最长时间不被使用的对象。
  5. blocking: 默认是false;
  6. -->
  7. <cache size="1024" type="com.lidaye.cache.RedisCache" />
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注