在项目中,我们经常需要将一些常用的数据使用缓存起来,避免频繁的查询数据库造成效率低下。spring 为我们提供了一套基于注解的缓存实现,方便我们实际的开发。我们可以扩展spring的cache接口以达到使用redis来做缓存的效果。
步骤:
1.编写一个类用于实现 org.springframework.cache.Cache 这个接口
2.编写一个类实现 org.springframework.cache.CacheManager 这个接口或继承 org.springframework.cache.support.AbstractCacheManager这个类
3.在配置文件中进行配置。
代码:
1.使用redis实现spring的cache接口 -- 数据以hash的方式存入到redis中
package com.huan.redis.springcache; import org.springframework.cache.Cache; import org.springframework.cache.support.SimpleValueWrapper; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; /** * 自定义redis缓存 * * @描述 * @作者 huan * @时间 2016年6月26日 - 下午2:14:26 */ public class RedisCache implements Cache { private JedisPool jedisPool; /** 缓存的过期时间,单位是秒 */ private int timeouts; public void setJedisPool(JedisPool jedisPool) { this.jedisPool = jedisPool; } public int getTimeouts() { return timeouts; } public void setTimeouts(int timeouts) { this.timeouts = timeouts; } private String name; public void setName(String name) { this.name = name; } @Override public String getName() { return name; } @Override public Object getNativeCache() { return jedisPool; } @Override public ValueWrapper get(Object key) { ValueWrapper result = null; Jedis jedis = null; try { jedis = jedisPool.getResource(); String value = jedis.hget(getName(), (String) key); if (value != null) { result = new SimpleValueWrapper(value); } } catch (Exception e) { e.printStackTrace(); } finally { if (null != jedis) { jedis.close(); } } return result; } @Override public void put(Object key, Object value) { String cacheKey = (String) key; String cacheValue = (String) value; Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.hset(getName(), cacheKey, cacheValue); jedis.expire(getName(), getTimeouts()); } catch (Exception e) { e.printStackTrace(); } finally { if (null != jedis) { jedis.close(); } } } @Override public void evict(Object key) { if (null != key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.hdel(getName(), (String) key); } catch (Exception e) { e.printStackTrace(); } finally { if (null != jedis) { jedis.close(); } } } } @Override public void clear() { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.hdel(getName()); } catch (Exception e) { e.printStackTrace(); } finally { if (null != jedis) { jedis.close(); } } } }
2.实现自己的缓存管理器
package com.huan.redis.springcache; import java.util.Collection; import org.springframework.cache.Cache; import org.springframework.cache.support.AbstractCacheManager; /** * 继承spring的抽象缓存管理器,用于实现我们自己的缓存管理 * @描述 * @作者 huan * @时间 2016年6月26日 - 下午2:17:15 */ public class RedisCacheManager extends AbstractCacheManager{ private Collection<? extends RedisCache> caches; public void setCaches(Collection<? extends RedisCache> caches) { this.caches = caches; } @Override protected Collection<? extends Cache> loadCaches() { return this.caches; } }
3.配置文件中进行配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <context:annotation-config /> <context:component-scan base-package="com.huan.redis" /> <cache:annotation-driven cache-manager="cacheManager"/> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="testWhileIdle" value="true" /> <property name="minEvictableIdleTimeMillis" value="60000" /> <property name="timeBetweenEvictionRunsMillis" value="30000" /> <property name="numTestsPerEvictionRun" value="-1" /> <property name="maxTotal" value="8" /> <property name="maxIdle" value="8" /> <property name="minIdle" value="0" /> </bean> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg ref="jedisPoolConfig" /> <constructor-arg value="192.168.1.5" /> </bean> <bean id="cacheManager" class="com.huan.redis.springcache.RedisCacheManager"> <property name="caches"> <set> <bean class="com.huan.redis.springcache.RedisCache"> <property name="jedisPool" ref="jedisPool" /> <property name="name" value="usersCache" /> <property name="timeouts" value="3600" /> </bean> <bean class="com.huan.redis.springcache.RedisCache"> <property name="jedisPool" ref="jedisPool" /> <property name="name" value="booksCache" /> <property name="timeouts" value="3600" /> </bean> </set> </property> </bean> </beans>
注意:1. <cache:annotation-driven cache-manager="cacheManager"/>这一句用于开启spring的缓存注解
2.redis.clients.jedis.JedisPool 用于配置redis的地址和端口,默认端口是6379,如果自己的redis不是这个端口,可以选择JedisPool中适当的构造方法进行配置
4.编写业务方法 -- UserService类中比较简单,就是UserServiceImpl中方法的申明。
package com.huan.redis.service; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; /** * 测试业务员方法 * @描述 * @作者 huan * @时间 2016年6月26日 - 下午3:20:58 */ @Service public class UserServiceImpl implements UserService { /** 将数据放入到usersCache这个缓存中,缓存的key使用spirng的spel表达式获取值 */ @Override @Cacheable(value = "usersCache", key = "#loginName") public String getUser(String loginName) { System.out.println("no user cache:" + loginName); return loginName; } @Override public String getUserNoCache(String loginName) { return getUser(loginName); } @Override @Cacheable(value = "booksCache", key = "#bookId") public String addBook(String bookId) { System.out.println("no book cache:" + bookId); return bookId; } /** 使usersCache中的缓存key为#loginName这个值的缓存失效 */ @Override @CacheEvict(value = "usersCache", key = "#loginName") public void evictUser(String loginName) { System.out.println("evict cache loginName:" + loginName); } }
5.进行测试
相关推荐
SpringCache整合Redis简单练习
SpringCache与redis集成,优雅的缓存解决方案.docx
主要介绍了Spring cache整合redis代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了Spring Cache整合Redis实现方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
redis主从配置流程,关键配置项,哨兵模式实现高可用。并整合spring cache功能。快速方便
之前整合springcache+redis的时候到网上搜了很多代码方案,但是注解都不起作用,搞了很长时间,也是很郁闷。所以,把成果分享一下,避免更多人踩坑,哈哈
spring boot cache 整合 redis demo (内包含 redis windows 安装包,和redis desktop 桌面 管理工具)
整合shiro框架,项目中带sql文件,可直接运行,需要先启用redis服务,登录名admin 密码123456,
SpringBoot2.0整合SpringCache和Redis 1 1. SpringBoot对应(自带)RedisClient是不同的 1 2. 实战 1 2.1. maven配置 1 2.2. yml配置 1 2.3. RedisConfig配置 1 2.4. RedisController 2 3. Spring缓存注解@...
博客http://blog.csdn.net/poorcoder_/article/details/59541710的代码。主要描述spring通过注解整合redis用作缓存的实例。
Springboot中Spring-cache与redis整合。这也是一个不错的框架,与spring的事务使用类似,只要添加一些注解方法,就可以动态的去操作缓存了,减少代码的操作。
主要介绍了Spring Boot整合Spring Cache及Redis过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Redis-Cluster集群整合SpringCahe的一个demo,将redis作为缓存,解决分布式问题
SpringBoot整合Redis,包括整合单机版Redis、redis-cluster集群、redis哨兵模式
Spring集成Redis实现数据缓存
Spring Boot整合Redis与Cache实现缓存Demo,实现Redis缓存。Redis是一个开源的内存NOSQL数据库,在web开发中主要被用于数据缓存。一般在高并发的情况下,web服务器接受访问时,直接从数据库加载是慢的,需要把常用...
Spring缓存+番石榴+ Redis
spring-boot整合MySQL,mybatis,redis
第十篇:使用Spring Cache集成Redis] 第十一篇:集成Swagger在线调试] 第十二篇:初探RabbitMQ消息队列] 第十三篇:RabbitMQ延迟队列] 第十四篇:强大的 actuator 服务监控与管理] 第十五篇:actuator与...
springboot1.x基于spring注解实现J2Cache两级缓存集成,一级缓存:ehcache,二级缓存:redis,代码可直接运行(根据项目中的README.md提示)