<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-redisartifactId>
<version>1.6.1.RELEASEversion>
dependency>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.7.3version>
dependency>
然后在spring中进行引入
<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">
<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>
<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>
<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>
public class RedisCache {
public Logger logger = LoggerFactory.getLogger(this.getClass());
private RedisTemplate 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 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 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 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 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 redisTemplate) {
this.redisTemplate = redisTemplate;
}
}
@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;
}
}
}
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;
}