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
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;
}
}
}
relation类设计:
public class Relation implements Serializable{
private Long id;
//accountId
private Long accountId;
private String userId;
private Integer deviceType; //设备类型 1、android 2、ios 3 winPhone
private String deviceToken; //设备token 只有IOS系统有
private String brand; //设备厂商
private String model;//手机型号
private Date createTime;
private Date updateTime;
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getAccountId() {
return accountId;
}
public void setAccountId(Long accountId) {
this.accountId = accountId;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Integer getDeviceType() {
return deviceType;
}
public void setDeviceType(Integer deviceType) {
this.deviceType = deviceType;
}
public String getDeviceToken() {
return deviceToken;
}
public void setDeviceToken(String deviceToken) {
this.deviceToken = deviceToken;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Relation(Long id, Long accountId, String userId, Integer deviceType, String deviceToken, String brand, Date createTime, Date updateTime) {
this.id = id;
this.accountId = accountId;
this.userId = userId;
this.deviceType = deviceType;
this.deviceToken = deviceToken;
this.brand = brand;
this.createTime = createTime;
this.updateTime = updateTime;
}
public Relation(){}
public String getKey(){
return KeyPrefixs.RELATION;
}
public String getObjectKey(){
return KeyPrefixs.RELATION + accountId + KeyPrefixs.UNDERLINE + userId;
}