1、配置redis
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:redis.properties</value>
</list>
</property>
</bean>
<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"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.password}"
p:pool-config-ref="poolConfig" />
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
2、redis使用
import com.google.common.base.Function;
/**
* Redis工具类
*
* /
public class RedisUtil {
private static StringRedisTemplate redisTemplate = null;
private static ApplicationContext app = null;
static{
app = new ClassPathXmlApplicationContext(new String[]{"classpath:redis-context.xml"});
redisTemplate = (StringRedisTemplate) app.getBean("redisTemplate");
}
/**
*
* @param key 需实现方法入参
* @param clazz 序列化/反序列化的类
* @param redisKey 需要存取的redis KEY
* @param timeout redis超时
* @param function 数据源方法
* @param <K> 入参
* @param <V> 出参
* @return
*/
@SuppressWarnings("unchecked")
public static <K, V> V getCachedData(K key, Class<V> clazz,
String redisKey, Integer timeout, Function<K, V> function) {
boolean redisValidFlag = true;
String storeValue;
V storeObject = null;
try {
BoundValueOperations<String, String> op = redisTemplate
.boundValueOps(redisKey);
storeValue = op.get();
if (StringUtils.isNotBlank(storeValue)) {
if (!clazz.equals(String.class)) {
storeObject = JSON.parseObject(storeValue, clazz);
} else {
storeObject = (V) storeValue;
}
} else {
storeObject = function.apply(key);
if (!clazz.equals(String.class)) {
storeValue = JSON.toJSONString(storeObject);
} else {
storeValue = (String) storeObject;
}
if (StringUtils.isNotBlank(storeValue)) {
op.set(storeValue);
if (op.getExpire() < 0) {
op.expire(timeout, TimeUnit.MINUTES);
}
}
}
} catch (Exception e) {
redisValidFlag = false;
}
if (!redisValidFlag) {
storeObject = function.apply(key);
}
return storeObject;
}
/**
* 删除key
*
* @param key
*/
public static void deleteKey(String key) {
try {
redisTemplate.delete(key);
} catch (Exception e) {
}
}
/**
* 更新key过期时间
*
* @param key
*/
public static Boolean updateKeyExpire(String key, Integer timeOut) {
try {
return redisTemplate.expire(key, timeOut, TimeUnit.MINUTES);
} catch (Exception e) {
return false;
}
}
/**
* 获取key内容
*
* @param key
*/
public static String getKeyContent(String key) {
try {
BoundValueOperations<String, String> stringStringBoundValueOperations = redisTemplate
.boundValueOps(key);
return stringStringBoundValueOperations.get();
} catch (Exception e) {
}
return null;
}
/**
* 设置key内容
*
* @param key
*/
public static void setKeyContent(String key, String content) {
try {
BoundValueOperations<String, String> stringStringBoundValueOperations = redisTemplate
.boundValueOps(key);
stringStringBoundValueOperations.set(content);
} catch (Exception e) {
}
}
/**
* 模糊删除key
*
* @param pattern("*"+suffix)
*/
public static void deleteKeyByPattern(String pattern) {
Set<String> keys = redisTemplate.keys(pattern);
if (!keys.isEmpty()) {
redisTemplate.delete(keys);
}
}
/**
*批量删除指定key
*/
public static void deleteByKeys(String... keys) {
redisTemplate.delete(Arrays.asList(keys));
}
}