maven的pom.xml配置
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.0.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
使用main方法测试一下:
public class JedisClient {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.226.71");
jedis.auth("123456");
String keys = "name";
// // 删数据
// jedis.del(keys);
// // 存数据
// jedis.set(keys, "snowolf");
// 取数据
String value = jedis.get(keys);
System.out.println(value);
}
}
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:p="http://cxf.apache.org/policy"
xmlns:jms="http://www.springframework.org/schema/jms" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<cache:annotation-driven cache-manager="cacheManager" key-generator="customKeyGenerator"/>
<bean id="customKeyGenerator" class="com.hongkun.greenpass.common.base.redis.CustomKeyGenerator"/>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="MaxWaitMillis" value="${redis.MaxWaitMillis}" />
<property name="testOnBorrow" value="${redis.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.pass}" />
<property name="poolConfig" ref="poolConfig" />
<property name="timeout" value="${redis.timeout}" />
</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>
<bean id="redisUtil" class="com.hongkun.greenpass.common.base.redis.RedisUtil" >
<property name="redisTemplate" ref="redisTemplate" />
</bean >
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg name="redisOperations" ref="redisTemplate"/>
<constructor-arg name="cacheNames">
<set>
<value>line</value>
</set>
</constructor-arg>
</bean>
properties文件配置
redis.host=192.168.226.71
redis.port=6379
redis.pass=123456
redis.maxIdle=300
redis.maxTotal=600
redis.MaxWaitMillis=1000
redis.testOnBorrow=true
测试
@Cacheable(value="line",key="#p0")
public List<LineManagerment> gethotline(LineManagerment bean,boolean needExSta) {
System.out.println("aaaaaaaa");
List<LineManagerment> list=lineManagermentDaoImpl.gethotline(bean);
if(list!=null && list.size()>0 && needExSta){
list=setSeList(list);
}
return list;
}
测试结果
第一次会去查数据库,控制台输出aaaaaaaa
第二次直接访问缓存输出结果,控制台未输出aaaaaaaa
自定义key生成
public class CustomKeyGenerator implements KeyGenerator{
@Override
public Object generate(Object paramObject, Method paramMethod, Object... paramArrayOfObject) {
StringBuilder sb = new StringBuilder();
sb.append(paramObject.getClass().getName()).append("_");
sb.append(paramMethod.getName());
for (Object obj : paramArrayOfObject) {
sb.append("_").append(obj.toString());
}
return sb.toString();
}
}
RedisUtil缓存工具类
public final class RedisUtil {
private static final Logger LOG = LoggerFactory.getLogger(RedisUtil.class);
private RedisTemplate<Serializable, Object> redisTemplate;
/**
* 批量删除对应的value
*
* @param keys
*/
public void remove(final String... keys) {
for (String key : keys) {
remove(key);
}
}
/**
* 批量删除key
*
* @param pattern
*/
public void removePattern(final String pattern) {
Set<Serializable> keys = redisTemplate.keys(pattern);
if (keys.size() > 0)
redisTemplate.delete(keys);
}
/**
* 删除对应的value
*
* @param key
*/
public void remove(final String key) {
if (exists(key)) {
redisTemplate.delete(key);
}
}
/**
* 判断缓存中是否有对应的value
*
* @param key
* @return
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
}
/**
* 读取缓存
*
* @param key
* @return
*/
public Object get(final String key) {
Object result = null;
ValueOperations<Serializable, Object> operations = redisTemplate
.opsForValue();
result = operations.get(key);
return result;
}
/**
* 写入缓存
*
* @param key
* @param value
* @return
*/
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;
}
/**
* 写入缓存
*
* @param key
* @param value
* @return
*/
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 void setRedisTemplate(
RedisTemplate<Serializable, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
}
测试
@RequestMapping(value = "test", method = RequestMethod.GET)
public void test(HttpServletRequest request, HttpServletResponse response) {
ResponseVo responseVo = new ResponseVo();
redisUtil.set("a", "test");
System.out.println("-------------" + redisUtil.get("a"));
TRestUtil.write(response, JsonUtil.toString(responseVo));
}