Spring Boot Redis 分布式缓存的使用
一、pom 依赖
- <!-- 分布式缓存 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-cache</artifactId>
- </dependency>
二、资源文件
- # Redis数据库索引(默认为0)
- spring.redis.database=
- # Redis服务器地址
- spring.redis.host=localhost
- # Redis服务器连接端口
- spring.redis.port=
- # Redis服务器连接密码(默认为空)
- spring.redis.password=
三、Java文件
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
- @Configuration
- public class RedisDBConfig {
- @Value("${spring.redis.host}")
- private String host;
- @Value("${spring.redis.port}")
- private int port;
- @Value("${spring.redis.password}")
- private String password;
- @Bean
- public JedisConnectionFactory jedisConnectionFactory() {
- JedisConnectionFactory factory = new JedisConnectionFactory();
- factory.setHostName(host);
- factory.setPort(port);
- factory.setPassword(password);
- return factory;
- }
- }
- import org.springframework.cache.CacheManager;
- import org.springframework.cache.annotation.CachingConfigurerSupport;
- import org.springframework.cache.annotation.EnableCaching;
- import org.springframework.cache.interceptor.KeyGenerator;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.data.redis.cache.RedisCacheManager;
- import org.springframework.data.redis.connection.RedisConnectionFactory;
- import org.springframework.data.redis.core.RedisTemplate;
- import java.lang.reflect.Method;
- /**
- * @author zxguan
- * @description
- * @create 2018-01-29 15:32
- */
- @Configuration
- @EnableCaching
- public class RedisCacheConfig extends CachingConfigurerSupport {
- /**
- * 缓存管理器.
- * @param redisTemplate
- * @return
- */
- @Bean
- public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) {
- CacheManager cacheManager = new RedisCacheManager(redisTemplate);
- return cacheManager;
- }
- /**
- * redis模板操作类,类似于jdbcTemplate的一个类;
- *
- * 虽然CacheManager也能获取到Cache对象,但是操作起来没有那么灵活;
- *
- * 这里在扩展下:RedisTemplate这个类不见得很好操作,我们可以在进行扩展一个我们
- *
- * 自己的缓存类,比如:RedisStorage类;
- *
- * @param factory : 通过Spring进行注入,参数在application.properties进行配置;
- * @return
- */
- @Bean
- public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
- RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
- redisTemplate.setConnectionFactory(factory);
- //key序列化方式;(不然会出现乱码;),但是如果方法上有Long等非String类型的话,会报类型转换错误;
- //所以在没有自己定义key生成策略的时候,以下这个代码建议不要这么写,可以不配置或者自己实现ObjectRedisSerializer
- //或者JdkSerializationRedisSerializer序列化方式;
- // RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型不可以会出现异常信息;
- // redisTemplate.setKeySerializer(redisSerializer);
- // redisTemplate.setHashKeySerializer(redisSerializer);
- return redisTemplate;
- }
- /**
- * 自定义key.
- * 此方法将会根据类名+方法名+所有参数的值生成唯一的一个key,即使@Cacheable中的value属性一样,key也会不一样。
- */
- @Override
- public KeyGenerator keyGenerator() {
- return new KeyGenerator() {
- @Override
- public Object generate(Object o, Method method, Object... objects) {
- // This will generate a unique key of the class name, the method name
- //and all method parameters appended.
- StringBuilder sb = new StringBuilder();
- sb.append(o.getClass().getName());
- sb.append(method.getName());
- for (Object obj : objects) {
- sb.append(obj.toString());
- }
- return sb.toString();
- }
- };
- }
- }
- import com.wsjia.ms.model.AliyunOauth2StateCache;
- import org.springframework.cache.annotation.CacheEvict;
- import org.springframework.cache.annotation.CachePut;
- import org.springframework.cache.annotation.Cacheable;
- import org.springframework.stereotype.Component;
- /**
- * @author zxguan
- * @description
- * @create 2018-01-29 15:11
- */
- @Component
- public class CacheService {
- @Cacheable(value = "aliyunStates", key = "#id")
- public AliyunOauth2StateCache findBy(String id) {
- return null;
- }
- @CachePut(value = "aliyunStates", key = "#stateCache.id")
- public AliyunOauth2StateCache saveOrUpdate(AliyunOauth2StateCache stateCache) {
- return stateCache;
- }
- @CacheEvict(value = "aliyunStates", key = "#stateCache.id")
- public AliyunOauth2StateCache delete(AliyunOauth2StateCache stateCache) {
- return stateCache;
- }
- }
其他就是调用接口实现增删改查
Spring Boot Redis 分布式缓存的使用的更多相关文章
- spring boot redis分布式锁
随着现在分布式架构越来越盛行,在很多场景下需要使用到分布式锁.分布式锁的实现有很多种,比如基于数据库. zookeeper 等,本文主要介绍使用 Redis 做分布式锁的方式,并封装成spring b ...
- spring boot redis分布式锁 (转)
一. Redis 分布式锁的实现以及存在的问题 锁是针对某个资源,保证其访问的互斥性,在实际使用当中,这个资源一般是一个字符串.使用 Redis 实现锁,主要是将资源放到 Redis 当中,利用其原子 ...
- spring boot redis 数据库缓存用法
缓存处理方式应该是 1.先从缓存中拿数据,如果有,直接返回.2.如果拿到的为空,则数据库查询,然后将查询结果存到缓存中.由此实现方式应该如下: private String baseKey = &qu ...
- spring boot redis缓存JedisPool使用
spring boot redis缓存JedisPool使用 添加依赖pom.xml中添加如下依赖 <!-- Spring Boot Redis --> <dependency> ...
- spring boot redis 缓存(cache)集成
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- Spring Boot 自带缓存及结合 Redis 使用
本文测试环境: Spring Boot 2.1.4.RELEASE + Redis 5.0.4 + CentOS 7 自带缓存 如果没有使用缓存中间件,Spring Boot 会使用默认的缓存,我们只 ...
- Spring Boot 入门之缓存和 NoSQL 篇(四)
原文地址:Spring Boot 入门之缓存和 NoSQL 篇(四) 博客地址:http://www.extlight.com 一.前言 当系统的访问量增大时,相应的数据库的性能就逐渐下降.但是,大多 ...
- Redis 分布式缓存 Java 框架
为什么要在 Java 分布式应用程序中使用缓存? 在提高应用程序速度和性能上,每一毫秒都很重要.根据谷歌的一项研究,假如一个网站在3秒钟或更短时间内没有加载成功,会有 53% 的手机用户会离开. 缓存 ...
- Spring Boot中使用缓存
Spring Boot中使用缓存 随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一. 原始的使 ...
随机推荐
- base64和hex
base64和hex 我们知道,字符分为二种:一种是可见字符:另一种是不可见字符. 1)三种编码方式 hex也称为base16,意思是使用16个可见字符来表示一个二进制数组,编码后数据大小将翻倍,因为 ...
- 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 ...
- vue项目中 favicon.ico不能正确显示的问题
方法一:修改index.html文件 <link rel="shortcut icon" type="image/x-icon" href="f ...
- 23Flutter FloatingActionButton实现类似闲鱼App底部导航凸起按钮:
/* 一.Flutter FloatingActionButton介绍 FloatingActionButton简称FAB,可以实现浮动按钮,也可以实现类型闲鱼app的底部凸起导航. child:子视 ...
- 小程序下载canvas生成图片
save_share_img:function(img){ var that = this; let { result } = that.data; getData.getData( "sa ...
- checkbox 在移动端显示为小圆圈问题
在desktop显示正常,但是在移动端显示变为小圆圈,无法正确展示选中取消选中效果问题解决方案: display: block; width: 58px; height: 20px; -webkit- ...
- PostgreSQL学习笔记——摘要
因为PostgreSQL和MySQL.DB2等数据库均遵循SQL语法,所以这篇随笔仅记录一些PostgreSQL中和别的数据库有差别或之前学习中遗漏的地方,以及一些我觉得比较重点的地方. 通过psql ...
- 【c# 学习笔记】接口与抽象类
抽象类经常与接口一起使用,共同服务于面向对象的编程,这里简单地分析一下接口与抽象类的区别,如下: 1.抽象类使用abstract关键字进行定义,而接口使用interface进行定义:它们都不能进行实例 ...
- 记录一下我的git连接不上GitHub问题
1.日常操作,提交代码,报错误下: $ git clone git@github.com:hanchao5272/myreflect.git Cloning into 'myreflect'... s ...
- rest_framework之ModelViewSet、路由控制、序列化组件快速搭建项目雏形
以UserInfo表登陆接口为例 ModelViewSet的用法十分简单,定义一个视图类,指定一个模型表,指定一个序列化类即可帮我们完成增删改查等功能 示例: # 视图层 from app01.MyS ...