1、首先引入代码库
<!-- redis cache start -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
<!-- redis cache end -->
2、引入spring redis配置信息:RedisConfig.xml
然后在spring中进行引入
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- jedis 配置 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" > <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxWaitMillis" value="${redis.maxWait}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <!-- redis服务器中心 --> <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" > <property name="poolConfig" ref="poolConfig" /> <property name="port" value="${redis.port}" /> <property name="hostName" value="${redis.host}" /> <property name="password" value="${redis.password}" /> <property name="timeout" value="${redis.timeout}" ></property> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" > <property name="connectionFactory" ref="redisConnectionFactory" /> <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> <!-- cache配置 --> <!--<bean id="methodCacheInterceptor" class="com.mucfc.msm.common.MethodCacheInterceptor" >--> <!--<property name="redisUtil" ref="redisUtil" />--> <!--</bean >--> <bean id="redisCache" class="com.drpeng.pengxin.api.cache.redis.RedisCache" > <property name="redisTemplate" ref="redisTemplate" /> </bean> <bean id="relationCache" class="com.drpeng.pengxin.api.cache.redis.RelationCache" > <property name="redisTemplate" ref="redisTemplate" /> </bean></beans>
3、RedisCache接口封装:RedisCache.java
package com.drpeng.pengxin.api.cache.redis;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/** * Description *
Created by qiupeng.wang on 16/6/16.
*/
public class RedisCache {
public Logger logger = LoggerFactory.getLogger(this.getClass());
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) {
e.printStackTrace();
}
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) {
e.printStackTrace();
}
return result;
}
public void setRedisTemplate(
RedisTemplate<Serializable, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
}
4、对于以上缓存设计进行测试使用
@Service("relationService")
public class RelationServiceImpl extends BaseServiceImpl implements RelationService {
@Autowired
private RelationDao relationDao;
@Autowired
private RedisCache redisCache;
/* @Autowired private RelationCache relationCache;*/
//设定失效时间为一周,每一个缓存必须设置有效期,后期如果数据量大,保证缓存中都为活跃用户
private final Long expireTime = 3600*24*5L;
public int createRelation(Relation relation){
relationDao.createRelation(relation);
redisCache.set(relation.getObjectKey(),relation,expireTime);
return 1;
}
public int updateRelation(Relation relation){
relationDao.updateRelation(relation);
redisCache.remove(relation.getObjectKey());
redisCache.set(relation.getObjectKey(),relation,expireTime);
return 1;
}
public Relation queryRelation(Relation relation){
Relation relation1 = (Relation)redisCache.get(relation.getObjectKey());
if(relation1 != null){
return relation1;
}else {
Relation relation2 = relationDao.queryRelation(relation);
if(relation2 != null){
redisCache.set(relation.getObjectKey(),relation2,expireTime);
}
return relation2;
}
}}
网友评论