参考:http://www.cnblogs.com/cuglkb/p/6862609.html
1、redis.xml
<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- redis连接池配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大可以有多少个Jedis活动连接实例,根据实际并发连接数设置 -->
<property name="maxTotal" value="${redis.maxTotal}"></property>
<!-- 最大等待连接中的数量 -->
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property>
<property name="lifo" value="${redis.lifo}"></property>
<property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"></property>
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="testOnReturn" value="${redis.testOnReturn}"></property>
<property name="testWhileIdle" value="${redis.testWhileIdle}"></property>
<property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"></property>
</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="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}"
p:port="${redis.port}"
p:pool-config-ref="poolConfig"/> -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<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="redisUtils" class="redis.RedisUtils">
<!-- <property name="redisTemplate" ref="redisTemplate" /> -->
</bean>
</beans>
2、redisUtils
package redis;
import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import javax.annotation.Resource;
public class RedisUtils {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Resource
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) {
log.error("set cache error", e);
}
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) {
log.error("set cache error", e);
}
return result;
}
public long increment(final String key , long delta){
return redisTemplate.opsForValue().increment(key, delta);
}
}
3、redis.properties
#redis config
redis.host=127.0.0.1
redis.port=6379
redis.pass=123456
redis.maxTotal=2000
redis.maxIdle=50
redis.maxWaitMillis=15000
redis.lifo=true
redis.blockWhenExhausted=true
redis.testOnBorrow=false
redis.testOnReturn=false
redis.testWhileIdle=false
redis.timeBetweenEvictionRunsMillis=30000
4、使用
@Resource
private RedisUtils redis;
/**
* 登录界面
* @param request
* @param response
* @return
*/
@RequestMapping(value = "login", method = RequestMethod.GET)
public ModelAndView login(HttpServletRequest request,HttpServletResponse response){
ModelAndView mav = new ModelAndView();
mav.setViewName("login");
//RedisUtils redis = new RedisUtils();
redis.set("a", "bb");
return mav;
}
注意:RedisUtils里面的redisTemplate使用spring注入,所以redisUtils必须也要注入,其成员变量才会执行spring注入。
如果redisUtils通过new实例化,其成员变量不会自动注入,redisTemplate必须通过getBean去实例化