使用RedisTemplate进行Redis存取的工具类设计

来源:互联网 发布:锦瑟 priest 知乎 编辑:程序博客网 时间:2024/06/06 15:43

通常在访问量大数据更新频率不高的系统中会使用第三方的缓存组件来降低数据库服务的负载,鉴于模块独立分工独立的考虑,针对缓存组件操作的工作全部应该统一接口对其他业务提供服务,这样业务操作只需要关注业务实现不需要关注缓存的具体细节。
本例以Redis缓存组件为例,制定操作Redis的工具类。

 

复制代码
  1 package com.luwei.console.mg.util;  2   3 import java.util.ArrayList;  4 import java.util.HashMap;  5 import java.util.HashSet;  6 import java.util.List;  7 import java.util.Map;  8 import java.util.Set;  9 import java.util.concurrent.TimeUnit; 10  11 import org.slf4j.Logger; 12 import org.slf4j.LoggerFactory; 13 import org.springframework.data.redis.core.HashOperations; 14 import org.springframework.data.redis.core.ListOperations; 15 import org.springframework.data.redis.core.RedisTemplate; 16 import org.springframework.data.redis.core.SetOperations; 17 import org.springframework.data.redis.core.ValueOperations; 18  19 /** 20  *  21  * <Description> Redis工具类<br> 22  * 使用该工具类的JavaBean必须实现Serializable接口 23  *  24  * @author lu.wei<br> 25  * @email 1025742048@qq.com <br> 26  * @date 2016年11月27日 <br> 27  * @since V1.0<br> 28  * @see com.luwei.console.mg.util <br> 29  */ 30 public class RedisUtil { 31  32     private static Logger logger = LoggerFactory.getLogger(RedisUtil.class); 33  34     @SuppressWarnings("unchecked") 35     private static RedisTemplate<String, Object> redisTemplate = (RedisTemplate<String, Object>) ContextUtil.getBean("redisTemplate");; 36  37     /** 38      *  39      * <Description> 根据前缀进行清除缓存<br> 40      *  41      * @author lu.wei<br> 42      * @email wei.lu@qq.com<br> 43      * @date 2016年8月25日 下午2:41:29 44      * @param prefix 45      * <br> 46      */ 47     public static void cleanRedis(String prefix) { 48         logger.info("cleanRedis prefix: {}", prefix); 49         try { 50             if (null != prefix) { 51                 if (null != redisTemplate) { 52                     Set<String> keys = redisTemplate.keys(prefix + "*"); 53                     for (String key : keys) { 54                         redisTemplate.delete(key); 55                     } 56                 } 57             } 58  59         } catch (Exception e) { 60             logger.error("cleanRedis error : {} ", e.getMessage(), e); 61         } 62     } 63  64     /** 65      *  66      * <Description> 根据KEY进行清除缓存<br> 67      *  68      * @author lu.wei<br> 69      * @email 1025742048@qq.com<br> 70      * @date 2016年10月28日 上午10:50:52 71      * @param key 72      * <br> 73      */ 74     public static void cleanRedisByKey(String key) { 75         logger.info("cleanRedisByKey key: {}", key); 76         try { 77             if (null != key) { 78                 if (null != redisTemplate) { 79                     redisTemplate.delete(key); 80                 } 81             } 82  83         } catch (Exception e) { 84             logger.error("cleanRedisByKey error : {} ", e.getMessage(), e); 85         } 86     } 87  88     /** 89      *  90      * <Description> 缓存字符串<br> 91      *  92      * @author lu.wei<br> 93      * @email 1025742048@qq.com<br> 94      * @date 2016年12月22日 下午5:47:44 95      * @param key 96      * @param data 97      * @param minus 98      * <br> 99      */100     public static void putCacheStr(String key, String data, Long minus) {101         logger.info("putCacheStr : {}, {}, {} minute", key, data, minus);102         try {103             ValueOperations<String, Object> opsValue = null;104             if (null != redisTemplate) {105                 opsValue = redisTemplate.opsForValue();106                 if (null != opsValue) {107                     opsValue.set(key, data);108                 }109             }110         } catch (Exception e) {111             logger.error("putCacheStr error : {} ", e.getMessage(), e);112         }113     }114 115     /**116      * 117      * <Description> 获取缓存字符串<br>118      * 119      * @author lu.wei<br>120      * @email 1025742048@qq.com<br>121      * @date 2016年12月22日 下午5:48:00122      * @param key123      * @return <br>124      */125     public static String getCacheStr(String key) {126         logger.info("getCacheStr : {}", key);127 128         String retStr = null;129         try {130             ValueOperations<String, Object> opsValue = null;131             if (null != redisTemplate) {132                 opsValue = redisTemplate.opsForValue();133                 if (null != opsValue) {134                     retStr = String.valueOf(opsValue.get(key));135                 }136             }137         } catch (Exception e) {138             logger.error("getCacheStr error : {} ", e.getMessage(), e);139         }140         return retStr;141     }142 143     /**144      * 145      * <Description> 缓存简单对象<br>146      * 基本数据类型和简单对象147      * 148      * @author lu.wei<br>149      * @email 1025742048@qq.com<br>150      * @date 2016年12月22日 下午4:47:14151      * @param key152      * @param value153      * @param minus154      * <br>155      */156     public static void putCacheSimple(String key, Object data, Long minus) {157         logger.info("putCacheSimple : {}, {}, {} minute", key, data, minus);158         try {159             ValueOperations<String, Object> opsValue = null;160             if (null != redisTemplate) {161                 opsValue = redisTemplate.opsForValue();162                 if (null != opsValue) {163                     opsValue.set(key, data);164 165                     if (null != minus) {166                         redisTemplate.expire(key, minus, TimeUnit.MINUTES);167                     }168                 }169             }170         } catch (Exception e) {171             logger.error("putCacheSimple error : {} ", e.getMessage(), e);172         }173     }174 175     /**176      * 177      * <Description> 获取缓存的简单对象<br>178      * 179      * @author lu.wei<br>180      * @email 1025742048@qq.com<br>181      * @date 2016年12月22日 下午4:50:16182      * @param key183      * @return <br>184      */185     public static Object getCacheSimple(String key) {186         logger.info("getCacheSimple : {}", key);187 188         Object object = null;189         try {190             ValueOperations<String, Object> opsValue = null;191             if (null != redisTemplate) {192                 opsValue = redisTemplate.opsForValue();193                 if (null != opsValue) {194                     object = (Object) opsValue.get(key);195                 }196             }197         } catch (Exception e) {198             logger.error("getCacheSimple error : {} ", e.getMessage(), e);199         }200         return object;201     }202 203     /**204      * 205      * <Description> 缓存List数据<br>206      * 207      * @author lu.wei<br>208      * @email 1025742048@qq.com<br>209      * @date 2016年12月22日 下午4:52:43210      * @param key211      * @param datas212      * @param minus213      * <br>214      */215     public static void putCacheList(String key, List<?> datas, Long minus) {216         logger.info("putCacheList : {}, {}, {} minute", key, datas, minus);217         try {218             ListOperations<String, Object> opsList = null;219             if (null != redisTemplate) {220                 opsList = redisTemplate.opsForList();221                 if (null != opsList) {222                     int size = datas.size();223                     for (int i = 0; i < size; i++) {224                         opsList.leftPush(key, datas.get(i));225                     }226 227                     if (null != minus) {228                         redisTemplate.expire(key, minus, TimeUnit.MINUTES);229                     }230                 }231             }232         } catch (Exception e) {233             logger.error("putCacheList error : {} ", e.getMessage(), e);234         }235     }236 237     /**238      * 239      * <Description> 获取缓存的List对象<br>240      * 241      * @author lu.wei<br>242      * @email 1025742048@qq.com<br>243      * @date 2016年12月22日 下午4:50:16244      * @param key245      * @return <br>246      */247     public static List<Object> getCacheList(String key) {248         logger.info("getCacheList : {}", key);249 250         List<Object> dataList = new ArrayList<Object>();251         try {252             ListOperations<String, Object> opsList = null;253             if (null != redisTemplate) {254                 opsList = redisTemplate.opsForList();255                 if (null != opsList) {256                     Long size = opsList.size(key);257                     for (int i = 0; i < size; i++) {258                         dataList.add(opsList.index(key, i));259                     }260                 }261             }262         } catch (Exception e) {263             logger.error("getCacheList error : {} ", e.getMessage(), e);264         }265         return dataList;266     }267 268     /**269      * 270      * <Description> 缓存SET数据<br>271      * 272      * @author lu.wei<br>273      * @email 1025742048@qq.com<br>274      * @date 2016年12月22日 下午5:21:30275      * @param key276      * @param datas277      * @param minus278      * <br>279      */280     public static void putCacheSet(String key, Set<?> datas, Long minus) {281         logger.info("putCacheList : {}, {}, {} minute", key, datas, minus);282         try {283             SetOperations<String, Object> opsSet = null;284             if (null != redisTemplate) {285                 opsSet = redisTemplate.opsForSet();286                 if (null != opsSet) {287                     opsSet.add(key, datas);288 289                     if (null != minus) {290                         redisTemplate.expire(key, minus, TimeUnit.MINUTES);291                     }292                 }293             }294         } catch (Exception e) {295             logger.error("putCacheList error : {} ", e.getMessage(), e);296         }297     }298 299     /**300      * 301      * <Description> 获取缓存的SET对象<br>302      * 303      * @author lu.wei<br>304      * @email 1025742048@qq.com<br>305      * @date 2016年12月22日 下午4:50:16306      * @param key307      * @return <br>308      */309     public static Set<Object> getCacheSet(String key) {310         logger.info("getCacheSet : {}", key);311 312         Set<Object> dataSet = new HashSet<Object>();313         try {314             SetOperations<String, Object> opsSet = null;315             if (null != redisTemplate) {316                 opsSet = redisTemplate.opsForSet();317                 if (null != opsSet) {318                     dataSet = opsSet.members(key);319                 }320             }321         } catch (Exception e) {322             logger.error("getCacheSet error : {} ", e.getMessage(), e);323         }324         return dataSet;325     }326 327     /**328      * 329      * <Description> 缓存MAP数据<br>330      * 331      * @author lu.wei<br>332      * @email 1025742048@qq.com<br>333      * @date 2016年12月22日 下午5:21:30334      * @param key335      * @param datas336      * @param minus337      * <br>338      */339     public static void putCacheMap(String key, Map<Object, Object> datas, Long minus) {340         logger.info("putCacheMap : {}, {}, {} minute", key, datas, minus);341         try {342             HashOperations<String, Object, Object> opsHash = null;343             if (null != redisTemplate) {344                 opsHash = redisTemplate.opsForHash();345                 if (null != opsHash) {346                     opsHash.putAll(key, datas);347 348                     if (null != minus) {349                         redisTemplate.expire(key, minus, TimeUnit.MINUTES);350                     }351                 }352             }353         } catch (Exception e) {354             logger.error("putCacheMap error : {} ", e.getMessage(), e);355         }356     }357 358     /**359      * 360      * <Description> 获取缓存的MAP对象<br>361      * 362      * @author lu.wei<br>363      * @email 1025742048@qq.com<br>364      * @date 2016年12月22日 下午4:50:16365      * @param key366      * @return <br>367      */368     public static Map<Object, Object> getCacheMap(String key) {369         logger.info("getCacheMap : {}", key);370 371         Map<Object, Object> dataMap = new HashMap<Object, Object>();372         try {373             HashOperations<String, Object, Object> opsHash = null;374             if (null != redisTemplate) {375                 opsHash = redisTemplate.opsForHash();376                 if (null != opsHash) {377                     dataMap = opsHash.entries(key);378                 }379             }380         } catch (Exception e) {381             logger.error("getCacheMap error : {} ", e.getMessage(), e);382         }383         return dataMap;384     }385 386     /**387      * 388      * <Description> TODO<br>389      * 390      * @author lu.wei<br>391      * @email 1025742048@qq.com<br>392      * @date 2016年10月27日 下午1:54:58393      * @return <br>394      */395     public static RedisTemplate<String, Object> getRedisTemplate() {396         return redisTemplate;397     }398 }
复制代码