安装redis数据库 此处省略
1.spring-context.xml
<!-- 读入配置属性文件 -->
<!-- <context:property-placeholder location="classpath:config.properties" />
<context:property-placeholder location="classpath:pgsql-config.properties" />
-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config.properties</value>
<value>classpath:pgsql-config.properties</value>
</list>
</property>
</bean>
<!-- 引入spring-redis.xml -->
<import resource="classpath:spring-redis.xml" />
2.config.properties
# Redis settings
redis.host=127.0.0.1
redis.port=6379
#redis.pass=password
redis.dbIndex=0
redis.expiration=3000
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
3.spring-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:context="http://www.springframework.org/schema/context"
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 config start -->
<!-- 配置JedisPoolConfig实例 -->
<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}" />
</bean>
<!-- 配置JedisConnectionFactory -->
<bean id="jedisConnectionFactory"
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="database" value="${redis.dbIndex}" />
<property name="poolConfig" ref="poolConfig" />
</bean>
<!-- 配置RedisTemplate -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
<!-- 配置RedisCacheManager -->
<bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg name="redisOperations" ref="redisTemplate" />
<property name="defaultExpiration" value="${redis.expiration}" />
</bean>
<!-- 配置RedisCacheConfig -->
<bean id="redisCacheConfig" class="com.bigname.common.utils.RedisCacheConfig">
<constructor-arg ref="jedisConnectionFactory" />
<constructor-arg ref="redisTemplate" />
<constructor-arg ref="redisCacheManager" />
</bean>
<!-- redis config end -->
</beans>
4.RedisCacheConfig
package com.bigname.common.utils;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
//@Configuration
//@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport{
private volatile JedisConnectionFactory jedisConnectionFactory;
private volatile RedisTemplate<String, String> redisTemplate;
private volatile RedisCacheManager redisCacheManager;
public RedisCacheConfig() {
super();
}
/**
* 带参数的构造方法 初始化所有的成员变量
*
* @param jedisConnectionFactory
* @param redisTemplate
* @param redisCacheManager
*/
public RedisCacheConfig(JedisConnectionFactory jedisConnectionFactory, RedisTemplate<String, String> redisTemplate,
RedisCacheManager redisCacheManager) {
this.jedisConnectionFactory = jedisConnectionFactory;
this.redisTemplate = redisTemplate;
this.redisCacheManager = redisCacheManager;
}
public JedisConnectionFactory getJedisConnecionFactory() {
return jedisConnectionFactory;
}
public RedisTemplate<String, String> getRedisTemplate() {
return redisTemplate;
}
public RedisCacheManager getRedisCacheManager() {
return redisCacheManager;
}
//@Bean
public KeyGenerator customKeyGenerator() {
return new KeyGenerator() {
public Object generate(Object target, Method method, Object... objects) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : objects) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
// @Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 使用Jackson2JsonRedisSerialize 替换默认序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
//objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// 设置value的序列化规则和 key的序列化规则
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
redisTemplate.setEnableDefaultSerializer(true);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
5.RedisHelper
package com.bigname.common.utils;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class RedisHelper {
private static Logger logger = Logger.getLogger(RedisHelper.class);
private static JedisPool jedisPool;
public RedisHelper(JedisPool pool) {
RedisHelper.jedisPool = pool;
}
public static String set(String key, String value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String rtn = jedis.set(key, value);
return rtn;
} catch(Exception e) {
logger.error("set方法报错:key=" + key + ",value=" + value, e);
throw new RuntimeException("set方法报错。");
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
public static String set(byte[] key, byte[] value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String rtn = jedis.set(key, value);
return rtn;
} catch(Exception e) {
logger.error("set方法报错:key=" + key + ",value=" + value, e);
throw new RuntimeException("set方法报错。");
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
/** 根据key从redis删除相关值 */
public static void del(String key) {
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
jedis.del(key);
} catch(Exception e) {
logger.error("del方法报错:key=" + key, e);
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
/** 可以设置过期时间的set方法 */
public static String setWithExpireTime(String key, String value, int seconds) {
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
String rtn = jedis.set(key, value);
jedis.expire(key, seconds);
return rtn;
} catch(Exception e) {
logger.error("setWithExpireTime方法报错:key=" + key + ",value=" + value, e);
throw new RuntimeException("get方法报错。");
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
/** 根据key从redis获取相关值 */
public static String get(String key) {
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
String rtn = jedis.get(key);
return rtn;
} catch(Exception e) {
logger.error("get方法报错:key=" + key, e);
throw new RuntimeException("get方法报错。");
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
/**
* 根据key从redis获取相关值
* @param key byte[]
* @return byte[]
*/
public static byte[] get(byte[] key) {
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
byte[] rtn = jedis.get(key);
return rtn;
} catch(Exception e) {
logger.error("get方法报错:key=" + key, e);
throw new RuntimeException("get方法报错。");
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
/** 往redis中设置map对象 */
public static String hmset(String key, Map<String, String> hash) {
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
String rtn = jedis.hmset(key, hash);
return rtn;
} catch(Exception e) {
logger.error("hmset方法报错:key=" + key, e);
throw new RuntimeException("hmset方法报错。");
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
/** 从redis中获取map对象 */
public static Map<String, String> hgetall(String key) {
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
Map<String, String> rtn = jedis.hgetAll(key);
return rtn;
} catch(Exception e) {
logger.error("hgetall方法报错:key=" + key, e);
throw new RuntimeException("hgetall方法报错。");
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
/** 往redis中设置List对象 */
public static void setList(String key, List<String> list) {
if(list != null) {
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
jedis.del(key);
for (String str : list) {
jedis.rpush(key, str);
}
} catch(Exception e) {
logger.error("setList方法报错:key=" + key, e);
throw new RuntimeException("setList方法报错。");
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
}
/** 给特定key的值新增制定值,返回新增后该key的值 */
public static long incrBy(String key, long value) {
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
long rtn = jedis.incrBy(key, value);
return rtn;
} catch(Exception e) {
logger.error("incrBy方法报错:key=" + key + ",value=" + value, e);
throw new RuntimeException("incrBy方法报错。");
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
/** 给特定key的值减少指定,返回修改后该key的值 */
public static long decrBy(String key, long value) {
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
long rtn = jedis.decrBy(key, value);
return rtn;
} catch(Exception e) {
logger.error("decrBy方法报错:key=" + key + ",value=" + value, e);
throw new RuntimeException("decrBy方法报错。");
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
/** 判断key是否已经在缓存中存在 */
public static boolean isKeyExistSetWithExpire(String key, int seconds) {
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
long rtn = jedis.setnx(key, "1");
if(1 == rtn && seconds > 0) {
jedis.expire(key, seconds);
}
return rtn == 0;
} catch(Exception e) {
logger.error("isKeyExistSetWithExpire方法报错:key=" + key + ",seconds=" + seconds, e);
throw new RuntimeException("isKeyExistSetWithExpire方法报错。");
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
/** 设置过期时间的方法 */
public static void expireByKey(String key, int seconds) {
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
if(seconds > 0) {
jedis.expire(key, seconds);
}
} catch(Exception e) {
logger.error("expireByKey方法报错:key=" + key + ",seconds=" + seconds, e);
throw new RuntimeException("expireByKey方法报错。");
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
/** 判断key在缓存中是否存在 */
public static boolean isKeyExist(String key) {
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
return jedis.exists(key);
} catch(Exception e) {
logger.error("isKeyExist方法报错:key=" + key, e);
throw new RuntimeException("isKeyExist方法报错。");
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
/** 批量删除某些字符串开头的缓存 */
public static void batchDel(String key) {
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
Set<String> set = jedis.keys(key +"*");
Iterator<String> it = set.iterator();
while(it.hasNext()){
String keyStr = it.next();
jedis.del(keyStr);
}
} catch(Exception e) {
logger.error("batchDel方法报错:key=" + key, e);
throw new RuntimeException("批量删除方法报错。");
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
/** 批量查询某些字符串开头的缓存 */
public static void queryKeys(String key) {
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
Set<String> set = jedis.keys(key +"*");
Iterator<String> it = set.iterator();
while(it.hasNext()){
String keyStr = it.next();
System.out.println(keyStr);
}
} catch(Exception e) {
logger.error("queryKeys方法报错:key=" + key, e);
throw new RuntimeException("批量删除方法报错。");
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
/**根据表达式查询redis中的key的集合 */
public static Set<String> keys(String pattern) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.keys(pattern);
} catch(Exception e) {
logger.error("keys方法报错:key=" + pattern, e);
throw new RuntimeException("keys方法报错。");
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
/**获取hash key值下所有Item的key */
public static Set<String> hKeys(String pattern) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.hkeys(pattern);
} catch(Exception e) {
logger.error("keys方法报错:key=" + pattern, e);
throw new RuntimeException("keys方法报错。");
} finally {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
}
6.serviceimpl 必须在service实现类加上@Cacheable("getUserById") 这个注解 想做到同步数据 最好在添加和修改的方法上面也加上这个注解 方可实现缓存同步
//@Cacheable("getUserById") //标注该方法查询的结果进入缓存,再次访问时直接读取缓存中的数据
public List queryUserAll() {
// TODO Auto-generated method stub
return userdao.queryAll();
}
注;有可能出现redis数据库乱码问题,但是不影响缓存 目前这块正在研究 网上 说的好多都不行