1. 程式人生 > >使用RedisTemplate進行Redis存取的工具類設計

使用RedisTemplate進行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 [email protected] <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 [email protected]<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 [email protected]<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 [email protected]<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 [email protected]<br> 121 * @date 2016年12月22日 下午5:48:00 122 * @param key 123 * @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 [email protected]<br> 150 * @date 2016年12月22日 下午4:47:14 151 * @param key 152 * @param value 153 * @param minus 154 * <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 [email protected]<br> 181 * @date 2016年12月22日 下午4:50:16 182 * @param key 183 * @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 [email protected]<br> 209 * @date 2016年12月22日 下午4:52:43 210 * @param key 211 * @param datas 212 * @param minus 213 * <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 [email protected]<br> 243 * @date 2016年12月22日 下午4:50:16 244 * @param key 245 * @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 [email protected]<br> 274 * @date 2016年12月22日 下午5:21:30 275 * @param key 276 * @param datas 277 * @param minus 278 * <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 [email protected]<br> 305 * @date 2016年12月22日 下午4:50:16 306 * @param key 307 * @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 =

相關推薦

使用RedisTemplate進行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.uti

載入指定包名下的全部類或依據的annotation進行過濾的工具

指定 -a art 依據 包名 data scrip 進行 ack 載入指定包名下的全部類或依據類的annotation進行過濾的工具類

基於spring的redisTemplate的緩存工具

pri note you ref tar youdao release 雲筆記 pid pom.xml文件添加 <!-- config redis data and client jar --><dependency> <

基於Java反射的map自動裝配JavaBean工具設計

person urn exception map.entry ati test javabean 好的 declare 我們平時在用Myabtis時不是常常需要用map來傳遞參數,大體是如下的步驟: public List<Role> findRoles(M

使用JFreeChart繪制XY折線圖(工具設計

rtp 折線 int super 單元 sco cat 接口 後端 準備用Java寫通信的仿真平臺作為畢業設計,相比matlab繪圖,Java繪圖需要自己去寫很多工具類,博主在這采用了JFreeChart的開源解決方案,摸索著自己寫了一個XY折線圖工具類,話不多說貼源碼,

JFreeChart繪製XY折線圖(工具設計

準備用Java寫通訊的模擬平臺作為畢業設計,相比matlab繪圖,Java繪圖需要自己去寫很多工具類,博主在這採用了JFreeChart的開源解決方案,摸索著自己寫了一個XY折線圖工具類,話不多說貼原始碼,原始碼中寫了很多註釋,配了一套自己用的繪圖樣式,需要的童鞋可以借鑑借鑑,希望對你有幫助

使用JFreeChart繪製XY折線圖(工具設計

準備用Java寫通訊的模擬平臺作為畢業設計,相比matlab繪圖,Java繪圖需要自己去寫很多工具類,博主在這採用了JFreeChart的開源解決方案,摸索著自己寫了一個XY折線圖工具類,話不多說貼原始碼,原始碼中寫了很多註釋,配了一套自己用的繪圖樣式,需要的童鞋可以借鑑借鑑,希望對你有幫助

獲取一個臨時文件和對中文文件名字進行編碼的工具

臨時工 進行 fff ima odi fileutil sta 先生 java      首先我們明白,一個文件可以命名為任何名稱,比如一個excel,我們可以命名為不帶後綴,然後向裏面寫入對應的內容,只是在導出的時候將文件命名為正確的名字即可。   一個在當前用

獲取一個臨時檔案和對中文檔名字進行編碼的工具

     首先我們明白,一個檔案可以命名為任何名稱,比如一個excel,我們可以命名為不帶字尾,然後向裡面寫入對應的內容,只是在匯出的時候將檔案命名為正確的名字即可。     一個在當前使用者的預設臨時資料夾中生成一個當前日期的資料夾,然後再裡面寫入一個用UUID生成名字的檔案,常用於Java

整理redis工具jedis - 二

程式碼如下: package cn.cslp.ilea.distribute.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputS

整理redis工具jedis

最近專案中有遇到需加分散式鎖問題,記錄一下整理的Redis工具類, 含有redis儲存Object的幾種方式:1、物件序列化儲存。2、物件轉json串儲存。3、hash型別chun儲存。 以下是程式碼: import java.io.ByteArrayInputStream; impor

spring boot 結合Redis 實現工具

自己整理了 spring boot 結合 Redis 的工具類 引入依賴 <dependency>     <groupId>org.springframework.boot</groupId>     <art

Redis工具

import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.io.Serializable; /

RedisUtil(RedisTemplateUtil)操作redis工具

操作redis的工具類: package com.qiang.util; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redi

java各種型別與json進行轉化的工具

package com.jugan.utils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.beans.Introspe

Redis封裝工具

引用到的jedis的封裝類,然後又加了一層封裝 import java.util.List; import java.util.Map; import java.util.Set; import redis.clients.jedis.JedisCommands; i

Redis操作工具

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templat

redis整合spring(redisTemplate工具) redis整合spring(redisTemplate工具)

redis整合spring(redisTemplate工具類) 原文地址:http://blog.csdn.net/qq_34021712/article/details/75949706   ©王賽超 前言 關於哨兵模式的配置,我是參考網上

使用JCrop進行圖片裁剪,裁剪js說明,裁剪預覽,裁剪上傳,裁剪設計的圖片處理的工具和程式碼

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

redis工具學習和使用(RedisTemplate,StringRedisTemplate)

RedisTemplate ,StringRedisTemplate 是對redis操作的一個封裝類.類似jdbcTemplate RedisTemplate是一個泛型類,RedisTemplate可以對任何型別的key-value鍵值對操作。(hash結構同