spring整合redis缓存

如果对spring cache不了解,这边有一篇文章详细的介绍了
http://jinnianshilongnian.iteye.com/blog/2001040
改项目基于maven构建
相关依赖


org.springframework.data
spring-data-redis
1.3.4.RELEASE



redis.clients
jedis
2.5.2



org.springframework
spring-jdbc
3.2.13.RELEASE




commons-logging
commons-logging-api
1.1




junit
junit
4.11
test



org.springframework
spring-test
3.2.13.RELEASE
test


Spring相关配置


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">






























class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">













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 redisTemplate;
private String name;

public RedisTemplate getRedisTemplate() {
return redisTemplate;
}

public void setRedisTemplate(RedisTemplate 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() {
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() {
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() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
return connection.del(keyf.getBytes());
}
});
}

@Override
public void clear() {
redisTemplate.execute(new RedisCallback() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
connection.flushDb();
return "ok";
}
});
}

/**
* 描述 : .

*


* <使用方法说明>
*


*
* @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;
}

/**
* 描述 : .

*


* <使用方法说明>
*


*
* @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缓存)