一、pom 依赖

  1. <!-- 分布式缓存 -->
  2. <dependency>
  3.   <groupId>org.springframework.boot</groupId>
  4.   <artifactId>spring-boot-starter-data-redis</artifactId>
  5. </dependency>
  6. <dependency>
  7.   <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-cache</artifactId>
  9. </dependency>

二、资源文件

  1. # Redis数据库索引(默认为0)
  2. spring.redis.database=
  3. # Redis服务器地址
  4. spring.redis.host=localhost
  5. # Redis服务器连接端口
  6. spring.redis.port=
  7. # Redis服务器连接密码(默认为空)
  8. spring.redis.password=

三、Java文件

  1. import org.springframework.beans.factory.annotation.Value;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
  5.  
  6. @Configuration
  7. public class RedisDBConfig {
  8.  
  9. @Value("${spring.redis.host}")
  10. private String host;
  11.  
  12. @Value("${spring.redis.port}")
  13. private int port;
  14.  
  15. @Value("${spring.redis.password}")
  16. private String password;
  17.  
  18. @Bean
  19. public JedisConnectionFactory jedisConnectionFactory() {
  20. JedisConnectionFactory factory = new JedisConnectionFactory();
  21. factory.setHostName(host);
  22. factory.setPort(port);
  23. factory.setPassword(password);
  24. return factory;
  25. }
  26.  
  27. }
  1. import org.springframework.cache.CacheManager;
  2. import org.springframework.cache.annotation.CachingConfigurerSupport;
  3. import org.springframework.cache.annotation.EnableCaching;
  4. import org.springframework.cache.interceptor.KeyGenerator;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.data.redis.cache.RedisCacheManager;
  8. import org.springframework.data.redis.connection.RedisConnectionFactory;
  9. import org.springframework.data.redis.core.RedisTemplate;
  10.  
  11. import java.lang.reflect.Method;
  12.  
  13. /**
  14. * @author zxguan
  15. * @description
  16. * @create 2018-01-29 15:32
  17. */
  18. @Configuration
  19. @EnableCaching
  20. public class RedisCacheConfig extends CachingConfigurerSupport {
  21. /**
  22. * 缓存管理器.
  23. * @param redisTemplate
  24. * @return
  25. */
  26. @Bean
  27. public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) {
  28. CacheManager cacheManager = new RedisCacheManager(redisTemplate);
  29. return cacheManager;
  30. }
  31.  
  32. /**
  33. * redis模板操作类,类似于jdbcTemplate的一个类;
  34. *
  35. * 虽然CacheManager也能获取到Cache对象,但是操作起来没有那么灵活;
  36. *
  37. * 这里在扩展下:RedisTemplate这个类不见得很好操作,我们可以在进行扩展一个我们
  38. *
  39. * 自己的缓存类,比如:RedisStorage类;
  40. *
  41. * @param factory : 通过Spring进行注入,参数在application.properties进行配置;
  42. * @return
  43. */
  44. @Bean
  45. public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
  46. RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
  47. redisTemplate.setConnectionFactory(factory);
  48.  
  49. //key序列化方式;(不然会出现乱码;),但是如果方法上有Long等非String类型的话,会报类型转换错误;
  50. //所以在没有自己定义key生成策略的时候,以下这个代码建议不要这么写,可以不配置或者自己实现ObjectRedisSerializer
  51. //或者JdkSerializationRedisSerializer序列化方式;
  52. // RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型不可以会出现异常信息;
  53. // redisTemplate.setKeySerializer(redisSerializer);
  54. // redisTemplate.setHashKeySerializer(redisSerializer);
  55.  
  56. return redisTemplate;
  57. }
  58.  
  59. /**
  60. * 自定义key.
  61. * 此方法将会根据类名+方法名+所有参数的值生成唯一的一个key,即使@Cacheable中的value属性一样,key也会不一样。
  62. */
  63. @Override
  64. public KeyGenerator keyGenerator() {
  65. return new KeyGenerator() {
  66. @Override
  67. public Object generate(Object o, Method method, Object... objects) {
  68. // This will generate a unique key of the class name, the method name
  69. //and all method parameters appended.
  70. StringBuilder sb = new StringBuilder();
  71. sb.append(o.getClass().getName());
  72. sb.append(method.getName());
  73. for (Object obj : objects) {
  74. sb.append(obj.toString());
  75. }
  76. return sb.toString();
  77. }
  78. };
  79. }
  80. }
  1. import com.wsjia.ms.model.AliyunOauth2StateCache;
  2. import org.springframework.cache.annotation.CacheEvict;
  3. import org.springframework.cache.annotation.CachePut;
  4. import org.springframework.cache.annotation.Cacheable;
  5. import org.springframework.stereotype.Component;
  6.  
  7. /**
  8. * @author zxguan
  9. * @description
  10. * @create 2018-01-29 15:11
  11. */
  12. @Component
  13. public class CacheService {
  14.  
  15. @Cacheable(value = "aliyunStates", key = "#id")
  16. public AliyunOauth2StateCache findBy(String id) {
  17. return null;
  18. }
  19.  
  20. @CachePut(value = "aliyunStates", key = "#stateCache.id")
  21. public AliyunOauth2StateCache saveOrUpdate(AliyunOauth2StateCache stateCache) {
  22. return stateCache;
  23. }
  24.  
  25. @CacheEvict(value = "aliyunStates", key = "#stateCache.id")
  26. public AliyunOauth2StateCache delete(AliyunOauth2StateCache stateCache) {
  27. return stateCache;
  28. }
  29. }

  其他就是调用接口实现增删改查


Spring Boot Redis 分布式缓存的使用的更多相关文章

  1. spring boot redis分布式锁

    随着现在分布式架构越来越盛行,在很多场景下需要使用到分布式锁.分布式锁的实现有很多种,比如基于数据库. zookeeper 等,本文主要介绍使用 Redis 做分布式锁的方式,并封装成spring b ...

  2. spring boot redis分布式锁 (转)

    一. Redis 分布式锁的实现以及存在的问题 锁是针对某个资源,保证其访问的互斥性,在实际使用当中,这个资源一般是一个字符串.使用 Redis 实现锁,主要是将资源放到 Redis 当中,利用其原子 ...

  3. spring boot redis 数据库缓存用法

    缓存处理方式应该是 1.先从缓存中拿数据,如果有,直接返回.2.如果拿到的为空,则数据库查询,然后将查询结果存到缓存中.由此实现方式应该如下: private String baseKey = &qu ...

  4. spring boot redis缓存JedisPool使用

    spring boot redis缓存JedisPool使用 添加依赖pom.xml中添加如下依赖 <!-- Spring Boot Redis --> <dependency> ...

  5. spring boot redis 缓存(cache)集成

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  6. Spring Boot 自带缓存及结合 Redis 使用

    本文测试环境: Spring Boot 2.1.4.RELEASE + Redis 5.0.4 + CentOS 7 自带缓存 如果没有使用缓存中间件,Spring Boot 会使用默认的缓存,我们只 ...

  7. Spring Boot 入门之缓存和 NoSQL 篇(四)

    原文地址:Spring Boot 入门之缓存和 NoSQL 篇(四) 博客地址:http://www.extlight.com 一.前言 当系统的访问量增大时,相应的数据库的性能就逐渐下降.但是,大多 ...

  8. Redis 分布式缓存 Java 框架

    为什么要在 Java 分布式应用程序中使用缓存? 在提高应用程序速度和性能上,每一毫秒都很重要.根据谷歌的一项研究,假如一个网站在3秒钟或更短时间内没有加载成功,会有 53% 的手机用户会离开. 缓存 ...

  9. Spring Boot中使用缓存

    Spring Boot中使用缓存 随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一. 原始的使 ...

随机推荐

  1. base64和hex

    base64和hex 我们知道,字符分为二种:一种是可见字符:另一种是不可见字符. 1)三种编码方式 hex也称为base16,意思是使用16个可见字符来表示一个二进制数组,编码后数据大小将翻倍,因为 ...

  2. EINVRES Request to https://bower.herokuapp.com/packages/ failed with 502

    Bower install fails with 502 - Bad Gateway when downloading bower packages. For example bower instal ...

  3. vue项目中 favicon.ico不能正确显示的问题

    方法一:修改index.html文件 <link rel="shortcut icon" type="image/x-icon" href="f ...

  4. 23Flutter FloatingActionButton实现类似闲鱼App底部导航凸起按钮:

    /* 一.Flutter FloatingActionButton介绍 FloatingActionButton简称FAB,可以实现浮动按钮,也可以实现类型闲鱼app的底部凸起导航. child:子视 ...

  5. 小程序下载canvas生成图片

    save_share_img:function(img){ var that = this; let { result } = that.data; getData.getData( "sa ...

  6. checkbox 在移动端显示为小圆圈问题

    在desktop显示正常,但是在移动端显示变为小圆圈,无法正确展示选中取消选中效果问题解决方案: display: block; width: 58px; height: 20px; -webkit- ...

  7. PostgreSQL学习笔记——摘要

    因为PostgreSQL和MySQL.DB2等数据库均遵循SQL语法,所以这篇随笔仅记录一些PostgreSQL中和别的数据库有差别或之前学习中遗漏的地方,以及一些我觉得比较重点的地方. 通过psql ...

  8. 【c# 学习笔记】接口与抽象类

    抽象类经常与接口一起使用,共同服务于面向对象的编程,这里简单地分析一下接口与抽象类的区别,如下: 1.抽象类使用abstract关键字进行定义,而接口使用interface进行定义:它们都不能进行实例 ...

  9. 记录一下我的git连接不上GitHub问题

    1.日常操作,提交代码,报错误下: $ git clone git@github.com:hanchao5272/myreflect.git Cloning into 'myreflect'... s ...

  10. rest_framework之ModelViewSet、路由控制、序列化组件快速搭建项目雏形

    以UserInfo表登陆接口为例 ModelViewSet的用法十分简单,定义一个视图类,指定一个模型表,指定一个序列化类即可帮我们完成增删改查等功能 示例: # 视图层 from app01.MyS ...