Redis缓存在Spring的使用
具体思路
思路很简单,就是在查询数据的时候,先检查redis数据库中有没有,要是有就把它拿出来,没有就先从mysql中取出来,再存到redis中。主要是利用aop的advisor在查mysql之前做一下判断。
1.下载Redis到windows并且修改密码
2.整合spring与redis
1.添加依赖
<!--redis-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
2.在spring配置文件中添加
<!-- jedis 配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxWaitMillis" value="${redis.maxWait}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
</bean>
<!-- redis服务器中心 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="poolConfig"/>
<property name="port" value="${redis.port}"/>
<property name="hostName" value="${redis.host}"/>
<property name="password" value="${redis.password}"/>
<property name="timeout" value="${redis.timeout}"></property>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean>
3.编写redis工具类来存取数据
@Autowired
private RedisTemplate<Serializable, Object> redisTemplate;
public boolean set(final String key, Object value) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate
.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
//设置过期时间单位秒
public boolean set(final String key, Object value, Long expireTime) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate
.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
}
public Object get(final String key){
Object value = null;
ValueOperations<Serializable,Object> operations = redisTemplate
.opsForValue();
value = operations.get(key);
return value;
}
4.编写MethodCacheInterceptor来写逻辑
@Component("methodCacheInterceptor")
public class MethodCacheInterceptor implements MethodInterceptor {
@Autowired
private RedisUtil redisUtil;
private Long defaultCacheExpireTime; // 缓存默认的过期时间
private Long xxxRecordManagerTime; //
private Long xxxSetRecordManagerTime; //
public MethodCacheInterceptor() {
// 加载过期时间设置
defaultCacheExpireTime = 360L;
}
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
Object value = null;
String targetName = methodInvocation.getThis().getClass().getName();
String methodName = methodInvocation.getMethod().getName();
//获取参数
Object[] arguments = methodInvocation.getArguments();
String key = getCacheKey(targetName, methodName, arguments);
try {
if (redisUtil.exists(key)) {
return redisUtil.get(key);
}
value = methodInvocation.proceed();
if (value != null) {
final String fkey = key;
final Object fvalue = value;
new Thread(new Runnable() {
@Override
public void run() {
redisUtil.set(fkey, fvalue, defaultCacheExpireTime);
}
}).start();
}
}catch (Exception e){
e.printStackTrace();
if (value == null){
return methodInvocation.proceed();
}
}
return value;
}
public String getCacheKey(String targetName,String methodName,Object[] arguments){
StringBuffer key = new StringBuffer();
key.append(targetName).append("_").append(methodName);
for (Object o:arguments) {
key.append(o).append("_");
}
return key.toString();
}
}
5.配置一下advisor
<aop:config proxy-target-class="false">
<aop:pointcut id="redisMethodePointcut"
expression="execution(* com.bihang.service.*.select*(..))" />
<aop:advisor advice-ref="methodCacheInterceptor" pointcut-ref="redisMethodePointcut"/>
</aop:config>
Redis缓存在Spring的使用的更多相关文章
- 【redis】3.Spring 集成注解 redis 项目配置使用
spring-data-redis 项目,配合 spring 特性并集成 Jedis 的一些命令和方法. 配置redis继承到spring管理项目,使用注解实现redis缓存功能. 参考:http: ...
- 【redis】4.spring boot集成redis,实现数据缓存
参考地址:https://spring.io/guides/gs/messaging-redis/ ================================================== ...
- springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。
springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...
- SpringBoot开发二十-Redis入门以及Spring整合Redis
安装 Redis,熟悉 Redis 的命令以及整合Redis,在Spring 中使用Redis. 代码实现 Redis 内置了 16 个库,索引是 0-15 ,默认选择第 0 个 Redis 的常用命 ...
- 嵌入式Redis服务器在Spring Boot测试中的使用
1.概述 Spring Data Redis提供了一种与Redis实例集成的简单方法. 但是,在某些情况下,使用嵌入式服务器比使用真实服务器创建开发和测试环境更方便. 因此,我们将学习如何设置和使用嵌 ...
- SpringBoot开发二十四-Redis入门以及Spring整合Redis
需求介绍 安装 Redis,熟悉 Redis 的命令以及整合Redis,在Spring 中使用Redis. 代码实现 Redis 内置了 16 个库,索引是 0-15 ,默认选择第 0 个 Redis ...
- 【redis】5.spring boot项目中,直接在spring data jpa的Repository层使用redis +redis注解@Cacheable直接在Repository层使用,报错问题处理Null key returned for cache operation
spring boot整合redis:http://www.cnblogs.com/sxdcgaq8080/p/8028970.html 首先,明确一下问题的场景 之前在spring boot整合re ...
- Redis客户端之Spring整合Jedis,ShardedJedisPool集群配置
Jedis设计 Jedis作为推荐的java语言redis客户端,其抽象封装为三部分: 对象池设计:Pool,JedisPool,GenericObjectPool,BasePoolableObjec ...
- Redis客户端之Spring整合Jedis
1.下载相关jar包,并引入工程: jedis-2.4.2.jar commons-pool2-2.0.jar 2.将以下XML配置引入spring <bean id="shard ...
随机推荐
- AEAI DP V3.8.0 升级说明,开源综合应用开发平台
1 升级说明AEAI DP 3.8版本是一次常规升级,安全机制是本次开发平台的升级重点,如果开发的应用对外部用户开放,一定要注意升级!升级说明及产品介质已上传至网盘中,地址:http://pan.ba ...
- spring cloud学习(六) 配置中心-自动更新
上一篇学习了spring cloud config的基本使用,但发现有个问题,就是每次更改配置后,都需要重启服务才能更新配置,这样肯定是不行的.在上网查资料了解后,spring cloud支持通过AM ...
- 详述MSSQL服务在渗透测试中的利用 (下篇)
part3 MSSQL写文件 步骤1 sp_makewebtask写文件 因为是`SA`权限,如果目标服务器是web服务器,我们也不用去备份了,可以直接写个一句话木马进去到web目录. 在不知道web ...
- flask_ Mongodb 的语法-排序
MOngoDB的排序是挺有用的 ,跟MySQL有明显的区别 .. 它的原生语法的第一个参数为条件限定,第二个参数为排序字段 db.news.find({},{'_id':1}) #1是升序 ...
- JSONP是什么
摘自:https://segmentfault.com/a/1190000007935557 一.JSONP的诞生 首先,因为ajax无法跨域,然后开发者就有所思考 其次,开发者发现, <scr ...
- Django设置联合唯一约束 -- migrate时报错处理
异常信息: a unique database constraint for 2 or more fields together 场景描述: 对于ORM中多对多关系的中间表,如果该关系表是手动创建的, ...
- (转)Linux PS 详解
原文:https://cn.aliyun.com/jiaocheng/162702.html 摘要:原文地址:http://www.cnblogs.com/wangkangluo1/archive/2 ...
- JavaScript -- Select
-----053-Select.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=" ...
- Silverlight中使用MVVM(1)--基础
Silverlight中使用MVVM(1)--基础 Silverlight中使用MVVM(2)—提高 Silverlight中使用MVVM(3)—进阶 Silverlight中使用MVVM(4)—演练 ...
- MongoDB安装配置教程
数据是每一前端人员必定接触的一样,所有的数据都是后端来编写,如果自己想练习项目,却没有数据,而是写一些假数据,去编写,或者通过json-server搭建一个数据,今天我们就通过MongoDB来搭建一个 ...