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 ...
随机推荐
- 迁移桌面程序到MS Store(7)——APPX + Service
本篇我们以一个Sample工程,来说明如何把一个常见结构的desktop application,转制成APPX并在MS Store提供下载. 之前的篇章中,我们已经介绍了一些内容,包括如何通过Vis ...
- SAS Shortcut Keys
最常用的键有F3(运行代码).F6(查看log信息).Ctrl + / (注释代码).Ctrl + Shift + / (取消代码注释) SAS系统一共有4类快捷键,其中部分有重复, 第一类可自定义. ...
- Python-实现图表绘制总结
Numpy是Python开源的数值计算扩展,可用来存储和处理大型矩阵,比Python自身数据结构要高效: matplotlib是一个Python的图像框架,使用其绘制出来的图形效果和MATLAB下绘制 ...
- python-i春秋验证码识别
i春秋作家:hlpureboy python+机器学习+验证码识别+源码 简单介绍 最近在写某网站的自动注册,在注册的过程中遇到一些问题,如js的执行.验证码的识别等等,今天给大家如何用python通 ...
- 使用命令行工具npm新创建一个vue项目
使用vue开发项目的前期工作可以参考前面写的: Vue环境搭建及node安装过程整理 Vue.js 提供一个官方命令行工具,可用于快速搭建大型单页应用.该工具提供开箱即用的构建工具配置,带来现代化的 ...
- mongo开发标准(待完善)
MongoDB开发规范 1.mongoDB库的设计mongodb数据库命名规范:db_xxxx禁止使用任何 " _ "(下划线)外的特殊字符 禁止使用数字打头的库名 数据库名最多为 ...
- spring boot 中使用swagger 来自动生成接口文档
1.依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swa ...
- redis常用命令(一)
一.redis常见的数据操作命令 http://redisdoc.com/ 二.键(key) keys *: 查询所有的key. exists key:判断某个key是否存在. move key db ...
- SQLServer——SQLServer链接外部数据源
学习链接:https://www.cnblogs.com/licin/p/6244169.html 一.新建ODBC数据源 1.打开控制面板→管理工具→ODBC数据源→系统DSN 2.添加新系统数据源 ...
- c++中文件读取
对于C++编译运行文件,我使用过两个编译器,一个是visual studio 2013,另外一个是devcpp,推荐使用devcpp. vs的特点是界面整洁清晰,但是需要收费,这是微软的,并且在电脑上 ...