一、引入两个需要的jar包
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
二、配置
1、新建applicationContext-redis.xml配置文件
<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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- scanner redis properties -->
<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true" />
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="testOnReturn" value="${redis.testOnReturn}" />
</bean>
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"
p:pool-config-ref="poolConfig" />
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<!-- 如果不配置Serializer,那么存储的时候智能使用String,如果用User类型存储,那么会提示错误User can't cast
to String!!! -->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<!-- <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> -->
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
</bean>
</beans>
2、redis.properties 放在sources文件夹下
#######Test#####
redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
redis.testOnReturn=true
#10mins
redis.expireTime.seconds=600
3、目录结构
4、在web.xml中配置上下文
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
三、redis工具类 CacheContext.java
package com.yutons.cms.util;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@Component
public class CacheContext {
private static final Logger logger = Logger.getLogger(CacheContext.class);
@Autowired
private RedisTemplate<String, String> redisTemplate;
/**
* 缓存有效期(单位:秒)
*/
// @Value("redis.expireTime.seconds")
private int expireTime =600;
public static final int forever = -1;
public void setValue(String key, String value) {
logger.info("set key:" + key + ",value:" + value + ",expire:"
+ expireTime);
redisTemplate.opsForValue().set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
}
/**
*
* @param key
* @param value
* @param expire
* 单位秒seconds
*/
public void setValue(String key, String value, int expire) {
logger.info("set key:" + key + ",value:" + value + ",expire:" + expire);
redisTemplate.opsForValue().set(key, value);
if (expire > 0) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
}
/**
* 取得缓存中得值,不改变过期时间
*
* @param key
* @return
*/
public String getValue(String key) {
logger.info("get key:" + key);
String value = redisTemplate.opsForValue().get(key);
return value;
}
/**
* 取得缓存中得值,重设默认过期时间
*
* @param key
* @return
*/
public String getValueResetExpire(String key) {
logger.info("get key:" + key);
String value = redisTemplate.opsForValue().get(key);
if (value != null) {
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
}
return value;
}
/**
* 取得缓存中得值,设置指定的过期时间<br>
* 如果过期时间<=0的话,就不改变过期时间。
*
* @param key
* @param expire
* 单位秒
* @return
*/
public String getValue(String key, int expire) {
logger.info("get key:" + key);
if (expire > 0) {
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
}
return redisTemplate.opsForValue().get(key);
}
public void del(String key) {
logger.info("delete key:" + key);
redisTemplate.delete(key);
}
}
使用方法:在controller中
@Autowired
private CacheContext redis;
@ResponseBody
@RequestMapping(value = "/testsave", method = RequestMethod.GET)
public String testsave(Model model) {
redis.setValue("123123", "redis");
return "success";
}
@ResponseBody
@RequestMapping(value = "/testget", method = RequestMethod.GET)
public String testget(Model model) {
String string = redis.getValue("123123");
return string;
}