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中使用缓存 随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一. 原始的使 ...
随机推荐
- arcgis python 布局中所有元素平移
# Author: ESRI # Date: July 5, 2010 # Version: ArcGIS 10.0 # Purpose: This script will loop through ...
- ES6深入浅出-2 新版函数:箭头函数 2 视频-2.视频 箭头函数杂谈
杂谈coffeescript 箭头函数抄的是谁? coffeescript 双箭头的形式 箭头函数简洁的语法 数组内每一个值都平方一下 Map的写法 箭头函数的写法 平方后,每一个值再加1 numbe ...
- jq删除标签
<script>$(function(){ $("div").remove()})</script>
- OLE导出EXCEL 问题处理
需求: 2.资产负债表.利润表导出优化,由于项目公司门店较多,需要增加批量导出功能.按纳税主体维度导出execl文件,输入了几个纳税主体,就生成几个execl文件. 实现: 用程序ZFIR0014XL ...
- VL10B 采购订单转DN
传入采购订单项目建交货单 FUNCTION zmmfmXXXX. *"------------------------------------------------------------ ...
- ClientDataSet中修改,删除,添加数据和Delta属性
ClientDataSet中使用Post提交变更的数据时,实际上并没有更新到后端数据库中,而是提交到了由DataSnap管理的数据缓冲区中.当使用了ClientDataSet.ApplyUpDates ...
- Laravel 别名Redis 与 Redis 扩展冲突
use Redis; //通过别名引用会报错 今天尝试使用了 Laravel 的 redis 结果报了如下错误. Non-static method Redis::xxx() cannot be ca ...
- Paper reading: High-Fidelity Pose and Expression Normalization for Face Recognition in the Wild(HPEN)
1. Introduction 人脸识别受到各种因素影响,其中最重要的两个影响是 pose 和 expression, 这两个因素会对 intra-person 变化产生极大的影响, 有时候甚至会超过 ...
- 35个高级Python知识点总结
原文地址:https://blog.51cto.com/xvjunjie/2156525 No.1 一切皆对象 众所周知,Java中强调“一切皆对象”,但是Python中的面向对象比Java更加彻底, ...
- Linux 教程学习笔记
目录 一.Linux 系统启动过程 1.分为 5 个阶段 3.shell.操作系统.内核的关系 二.Linux 系统目录结构 三.Linux 文件基本属性 1.Linux文件属性 3.更改文件属性: ...