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; }