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 ...
随机推荐
- Eleasticsearch启动失败问题解决
问题: [root@dnode1 bin]# ./elasticsearch -d [root@dnode1 bin]# Exception in thread "main" ja ...
- c++程序时间统计
如下所示,引入<time.h>我们就可以统计时间了: #include<iostream> #include<time.h> #include<windows ...
- 图片训练:使用卷积神经网络(CNN)识别手写数字
这篇文章中,我们将使用CNN构建一个Tensorflow.js模型来分辨手写的数字.首先,我们通过使之“查看”数以千计的数字图片以及他们对应的标识来训练分辨器.然后我们再通过此模型从未“见到”过的测试 ...
- ASP.NET Core 中使用 Hangfire 定时启动 Scrapyd 爬虫
用 Scrapy 做好的爬虫使用 Scrapyd 来管理发布启动等工作,每次手动执行也很繁琐;考虑可以使用 Hangfire 集成在 web 工程里. Scrapyd 中启动爬虫的请求如下: curl ...
- JavaScript -- Math
----- 016-Math.html ----- <!DOCTYPE html> <html> <head> <meta http-equiv=" ...
- js去除字符串中的标签
var str="<p>js去除字符串中的标签</p>"; var result=str.replace(/<.*?>/ig,"&qu ...
- “声讨”高云的《jQuery技术内幕》
1. 前言: 其实本文有点太标题党了,哈哈,见谅.说“声讨”,就是说说我作为一个<jQuery技术内幕>一个忠实读者,读本书的一些想法和建议. 2014年2月20日,我收到了<jQu ...
- 最常用的两种C++序列化方案的使用心得(protobuf和boost serialization)
导读 1. 什么是序列化? 2. 为什么要序列化?好处在哪里? 3. C++对象序列化的四种方法 4. 最常用的两种序列化方案使用心得 正文 1. 什么是序列化? 程序员在编写应用程序的时候往往需要将 ...
- postgresql逻辑结构--视图(五)
定义 一.创建视图 1.语法 create [or replace ] [ temp | temporary ] view name [(column_name [,...])] as que ...
- Tomcat学习总结(13)—— Tomcat常用参数配置说明
1.修改端口号 Tomcat端口配置在server.xml文件的Connector标签中,默认为8080,可根据实际情况修改. 修改端口号 2.解决URL中文参数乱码 在server.xml文件的Co ...