spring redis集成

1、首先引入代码库


 
   org.springframework.data 
   spring-data-redis 
   1.6.1.RELEASE

    
redis.clients   
 jedis   
 2.7.3


2、引入spring redis配置信息:RedisConfig.xml

然后在spring中进行引入

                                                                                                                                                                                                

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

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

你可能感兴趣的:(spring redis集成)