1、简述
Redis是一种nosql数据库,在开发中常用做缓存。Jedis是Redis在Java中的redis- client。spring把专门的数据操作独立封装在spring-data系列中,spring-data-redis自然是针对Redis的独立封装了。
2、使用
2.1 pom.xml
<!-- redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!-- spring-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.4.RELEASE</version>
</dependency>
2.2 redis.properties配置
#最大分配的对象数
redis.pool.maxTotal=1024
#最大能够保持idel状态的对象数
redis.pool.maxIdle=200
#当池内没有返回对象时,最大等待时间
redis.pool.maxWaitMillis=1000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
#当调用return Object方法时,是否进行有效性检查
redis.pool.testOnReturn=true
#连接密码
redis.password=
#IP
redis.ip=xxxx
#Port
redis.port=6379
2.3 spring-redis.xml配置
<!-- 缓存的层级-->
<context:component-scan base-package="com.xxxx" />
<!-- 引入redis配置 -->
<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>
<!-- Jedis 连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean>
<!-- Jedis ConnectionFactory 数据库连接配置 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.ip}" />
<property name="port" value="${redis.port}" />
<!--<property name="password" value="${redis.pass}" />-->
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
<!-- redisTemplate配置,redisTemplate是对Jedis的对redis操作的扩展,有更多的操作,封装使操作更便捷 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
redisTemplate已经基本配置完成。接下来可以创建一个Goods类,必须实现或者间接实现Serializable接口。
Redis存储对象是使用序列化,spring-data-redis已经将序列化的功能内置,不需要我们去管,我们只需要调用api就可以使用。SerialVersionUID字段对序列化扩展有用,为了以后扩展或者缩减字段时不会造成反序列化出错。
2.4 创建redis的增删改查操作
@Component
public class RedisCache {
public final static String CAHCENAME="cache";//缓存名
public final static int CAHCETIME=60;//默认缓存时间
//自动注入RedisTemplate
@Autowired
private RedisTemplate<String, String> redisTemplate;
public <T> boolean putCache(String key, T obj) {
final byte[] bkey = key.getBytes();
final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj);
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
return connection.setNX(bkey, bvalue);
}
});
return result;
}
public <T> boolean setCache(String key, T obj) {
final byte[] bkey = key.getBytes();
final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj);
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
connection.set(bkey, bvalue);
return true;
}
});
return result;
}
public <T> void putCacheWithExpireTime(String key, T obj, final long expireTime) {
final byte[] bkey = key.getBytes();
final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj);
redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
connection.setEx(bkey, expireTime, bvalue);
return true;
}
});
}
public <T> boolean putListCache(String key, List<T> objList) {
final byte[] bkey = key.getBytes();
final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList);
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
return connection.setNX(bkey, bvalue);
}
});
return result;
}
public <T> boolean putListCacheWithExpireTime(String key, List<T> objList, final long expireTime) {
final byte[] bkey = key.getBytes();
final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList);
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
connection.setEx(bkey, expireTime, bvalue);
return true;
}
});
return result;
}
public <T> T getCache(final String key, Class<T> targetClass) {
byte[] result = redisTemplate.execute(new RedisCallback<byte[]>() {
@Override
public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
return connection.get(key.getBytes());
}
});
if (result == null) {
return null;
}
return ProtoStuffSerializerUtil.deserialize(result, targetClass);
}
public <T> List<T> getListCache(final String key, Class<T> targetClass) {
byte[] result = redisTemplate.execute(new RedisCallback<byte[]>() {
@Override
public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
return connection.get(key.getBytes());
}
});
if (result == null) {
return null;
}
return ProtoStuffSerializerUtil.deserializeList(result, targetClass);
}
/**
* 精确删除key
*
* @param key
*/
public void deleteCache(String key) {
redisTemplate.delete(key);
}
/**
* 模糊删除key
*
* @param pattern
*/
public void deleteCacheWithPattern(String pattern) {
Set<String> keys = redisTemplate.keys(pattern);
redisTemplate.delete(keys);
}
/**
* 清空所有缓存
*/
public void clearCache() {
deleteCacheWithPattern(RedisCache.CAHCENAME+"|*");
}
}
2.5 get和set操作示例
@Service("goodsService")
public class GoodsServiceImpl implements IGoodsService {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Resource
private IElasticService elasticService;
@Autowired
private RedisCache cache;
@Value("${searchUrl}")
private String searchUrl;
@Override
public List<Goods> getGoodsList(int offset, int limit) {
String cacheKey = RedisCache.CAHCENAME + "|getGoodsList|" + offset + "|" + limit;
//先去缓存中取
List<Goods> resultCache = cache.getListCache(cacheKey, Goods.class);
if (resultCache == null) {
//缓存中没有再去数据库取,并插入缓存(缓存时间为60秒)
resultCache = elasticService.getGoodsList(offset, limit);
cache.putListCacheWithExpireTime(cacheKey, resultCache, RedisCache.CAHCETIME);
LOG.info("put cache with key:" + cacheKey);
} else {
LOG.info("get cache with key:" + cacheKey);
}
return resultCache;
}
@Override
public ReportResultSimple searchGoods(String queryData) {
ReportResultSimple result = new ReportResultSimple();
try {
JsonParser jsonParser = new ObjectMapper().readValue(queryData, new TypeReference<JsonParser>() {
});
LOG.info("query begin, query is:[ " + jsonParser.toString() + " ]");
String cacheKey = RedisCache.CAHCENAME + "|" + queryData;
//暂时只对没有search的查询条件做缓存
if (jsonParser.getQuerys() == null) {
//先去缓存中取
result = cache.getCache(cacheKey, ReportResultSimple.class);
}
if (result == null || jsonParser.getQuerys() != null) {
//缓存没有结果,从elasticsearch查询
//
if (jsonParser.getQuerys() == null) {
cache.putCacheWithExpireTime(cacheKey, result, RedisCache.CAHCETIME);
}
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
result.setFlag("false");
result.setMsg(e.getMessage());
}
return result;
}
}