如果对spring cache不了解,这边有一篇文章详细的介绍了
http://jinnianshilongnian.iteye.com/blog/2001040
改项目基于maven构建
相关依赖
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.13.RELEASE</version>
</dependency>
<!-- 日志记录依赖包,很多都依赖此包,像log4j,json-lib等等 -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging-api</artifactId>
<version>1.1</version>
</dependency>
<!-- junit 测试包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.13.RELEASE</version>
<scope>test</scope>
</dependency>
Spring相关配置
<?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:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="
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/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"
default-lazy-init="true">
<context:property-placeholder location="classpath:redis.properties" />
<!-- 自动扫描包 -->
<context:component-scan base-package="com.layou" />
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
<cache:annotation-driven cache-manager="cacheManager" />
<!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="com.layou.RedisCache">
<property name="redisTemplate" ref="redisTemplate" />
<property name="name" value="default" />
</bean>
</set>
</property>
</bean>
<!-- redis 相关配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="maxTotal" value="${redis.pool.maxActive}" />
<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean>
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="password" value="${redis.password}" />
<property name="timeout" value="${redis.timeout}" />
<property name="usePool" value="true" />
<property name="poolConfig" ref="poolConfig" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
</beans>
redis相关配置
#redis.host=localhost
redis.host=192.168.71.145
redis.port=6379
redis.password=
#客户端超时时间单位是毫秒
redis.timeout=100000
#最大连接数
redis.pool.maxActive=500
#最大能够保持idel状态的对象数
redis.pool.maxIdle=200
#最大建立连接等待时间
redis.pool.maxWait=2000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
首先,通过自己实现Cache接口来实现对redis的相关操作
package com.layou;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
public class RedisCache implements Cache {
private RedisTemplate<String, Object> redisTemplate;
private String name;
public RedisTemplate<String, Object> getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
@Override
public Object getNativeCache() {
return this.redisTemplate;
}
@Override
public ValueWrapper get(Object key) {
final String keyf = (String) key;
Object object = null;
object = redisTemplate.execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) throws DataAccessException {
byte[] key = keyf.getBytes();
byte[] value = connection.get(key);
if (value == null) {
return null;
}
return toObject(value);
}
});
return (object != null ? new SimpleValueWrapper(object) : null);
}
@Override
public void put(Object key, Object value) {
final String keyf = (String) key;
final Object valuef = value;
final long liveTime = 86400;
redisTemplate.execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
byte[] keyb = keyf.getBytes();
byte[] valueb = toByteArray(valuef);
connection.set(keyb, valueb);
if (liveTime > 0) {
connection.expire(keyb, liveTime);
}
return 1L;
}
});
}
@Override
public void evict(Object key) {
final String keyf = (String) key;
redisTemplate.execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
return connection.del(keyf.getBytes());
}
});
}
@Override
public void clear() {
redisTemplate.execute(new RedisCallback<String>() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
connection.flushDb();
return "ok";
}
});
}
/**
* 描述 : <Object转byte[]>. <br>
* <p>
* <使用方法说明>
* </p>
*
* @param obj
* @return
*/
private byte[] toByteArray(Object obj) {
byte[] bytes = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
bytes = bos.toByteArray();
oos.close();
bos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return bytes;
}
/**
* 描述 : <byte[]转Object>. <br>
* <p>
* <使用方法说明>
* </p>
*
* @param bytes
* @return
*/
private Object toObject(byte[] bytes) {
Object obj = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
obj = ois.readObject();
ois.close();
bis.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return obj;
}
}
使用junit进行测试
package com.layou;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
@ContextConfiguration(locations = {"classpath:applicationContext-cache-redis.xml"})
public class RedisTest extends AbstractJUnit4SpringContextTests {
private RedisCache cache;
@Autowired
private CacheManager manager;
@Before
public void before() {
cache = (RedisCache) manager.getCache("default");
}
@Test
public void testAdd() throws Exception {
cache.put("test", 1);
}
@Test
public void testGet() throws Exception {
System.out.println(cache.get("test").get());
}
@Test
public void testEvict() throws Exception {
cache.evict("test");
}
@Test
public void testClear() throws Exception {
cache.clear();
}
}
在实际项目中,多是通过Spring的注解完成相关操作
package com.layou;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class RedisAnnotation {
/**
* 第一次查询会到数据库中差寻,第二次就会从缓存中取
* #id代表使用参数id
* 一定要放在spring容器中管理,不然拦截不到
* @param id
* @return
*/
@Cacheable(key="'name' + #id", value="default")
public String getNameById(int id) {
//TODO 进行数据库查询并返回查询数据库内容
System.out.println("查询数据库");
return "" + id;
}
/**
* 从缓存中删除
* @param id
*/
@CacheEvict(key="'name' + #id", value="default")
public void deleteById(int id) {
System.out.println("数据库删除");
}
}
使用junit进行相关测试
package com.layou;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
@ContextConfiguration(locations = {"classpath:applicationContext-cache-redis.xml"})
public class RedisAnnotationTest extends AbstractJUnit4SpringContextTests {
@Autowired
private RedisAnnotation redis;
@Test
public void testQuery() {
//第二次查询的时候就会从缓存中取
System.out.println(redis.getNameById(1));
}
@Test
public void testDelete() {
redis.deleteById(1);
}
}
分享到:
相关推荐
在Spring体系中使用redis.zip spring集成redis缓存
SpringCloud整合Redis缓存;版本:SpringCloud :Dalston.SR4;SpringBoot:1.5.14.RELEASE;Redis:3.2.0;Maven :3.5.3.代码下载下来即可用
SpringMVC集成Redis进行缓存管理,SpringMVC基于接口编程的实例,从代码中学习Spring是怎么集成缓存。
spring整合redis做缓存用注解实现的完整例子,带jar包,可以直接运行
在一些要求高一致性(任何数据变化都能及时的被查询到)的系统和应用中,就不能再使用EhCache来解决了,这个时候使用集中式缓存是个不错的选择,因此本文将介绍如何在Spring Boot的缓存支持中使用Redis进行数据缓存...
Spring集成Redis实现数据缓存
博客http://blog.csdn.net/poorcoder_/article/details/59541710的代码。主要描述spring通过注解整合redis用作缓存的实例。
spring集成redis的demo项目。其中包括redis作为mybatis的第三方缓存配置和redis直接操作缓存数据库的集成。说明:将application.properties中数据源的修改成你的配置。若你只想看spring集成redis的操作,则可以将...
这是Maven项目,Spring集成Redis来实现注解缓存,代码配置方面都有详细代码,并在相关配置文件里添加了相关参考文档地址,都是博主自己一手敲出来的,如果觉得资源还可以,请五星好评哦,亲(づ ̄3 ̄)づ╭❤~
Redis缓存+Spring的集成示例 Redis缓存+Spring的集成示例Redis缓存+Spring的集成示例 Redis缓存+Spring的集成示例
本资源是一个最新 spring4 + mybatis3 + Redis 缓存集成的一个简单的 demo,也是博客《Redis 缓存 + Spring 的集成示例》的配套示例项目,该博客地址是:http://blog.csdn.net/defonds/article/details/48716161。
Redis 是一个开源的 key-value 存储系统,由于出众的性能,大部分互联网企业都用来做服务器端缓存。Redis 支持单实例模式,集群主从模式、哨兵模式等部署
springboot,mybatis,redis缓存 集成
本篇文章主要介绍了spring整合redis缓存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用,具有一定的参考价值,有兴趣的可以了解一下。
使用spring+mvc+hibernate集成redis缓存,如有需要详细使用阅读内容可以访问我的博客来查看http://blog.csdn.net/wohuozheng?ref=toolbar
mybatis二级缓存 + reads做第三级缓存
Redis 缓存 + Spring 的集成示例 源码分享! Redis 缓存 + Spring 的集成示例。 本资源是一个最新 spring4 + mybatis3 + Redis 缓存集成的一个简单的 demo,也是...Redis缓存 Redis Spring+Redis Spring缓存 缓存集成
Spring Security应用实践,整合redis缓存,实现用户利用token进行登录功能。 Spring Security*是 Spring 家族中的一个安全管理框架。相比与另外一个安全框架Shiro,它提供了更丰富的功能,社区资源也比Shiro丰富。 ...
注解redis缓存数据,Spring-session和redis实现分布式session同步(建议按功能模块划分系统)。 6、日志 =========== logback打印日志,业务日志和调试日志分开打印。同时基于时间和文件大小分割日志文件。 9、...
Spring+Redis集成,实现缓存存储。