redis3.0 java spring_spring3.0结合Redis在项目中的运用

1 /**

2 * 缓存操作3 */

4 public classCacheService {5

6 protected Logger logger =LoggerFactory.getLogger(getClass());7

8 @Autowired9 private RedisTemplateredisTemplate;10

11 /*

12 * 缓存最大过期时间-一个月13 */

14 public static final int EXPIRE_TIME_MAX = 30 * 24 * 3600;15

16 /*

17 * 缓存过期时间-半天18 */

19 public static final int EXPIRE_TIME_HALFDAY = 12 * 3600;20

21 /*

22 * 缓存过期时间-整天23 */

24 public static final int EXPIRE_TIME_ONEDAY = 24 * 3600;25

26 /******************************27 ********* 缓存操作 ***********28 ******************************/

29

30 /**

31 * 设置缓存32 *33 *@paramkey34 *@paramvalue35 */

36 public voidputCache(String key, Object value) {37 try{38 redisTemplate.opsForValue().set(key, value);39 } catch(Exception e) {40 logger.error("PUT cache exception [key=" + key + ", value=" + value + "].", e);41 }42 }43

44 /**

45 * 设置缓存,并设定缓存时长(秒)46 *47 *@paramkey48 *@paramvalue49 *@paramexpire50 */

51 public void putCache(String key, Object value, intexpire) {52 try{53

54 redisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);55 } catch(Exception e) {56 logger.error("PUT cache exception [key=" + key + ", value=" + value + ", expire=" + expire + "].", e);57 }58 }59

60 /**

61 * 获取缓存数据62 *63 *@paramkey64 *@return

65 */

66 publicObject getCache(String key) {67 try{68

69 returnredisTemplate.opsForValue().get(key);70 } catch(Exception e) {71 logger.error("GET cache exception [key=" + key + "].", e);72 }73 return null;74 }75

76 /**

77 * 删除缓存78 *79 *@paramkey80 */

81 public voidremoveCache(String key) {82 try{83

84 redisTemplate.delete(key);85

86 } catch(Exception e) {87 logger.error("Remove cache exception [key=" + key + "].", e);88 }89 }90

91 /******************************92 ********* 队列操作 ***********93 ******************************/

94

95 /**

96 * 队列缓存设置97 *98 *@paramkey99 *@paramvalue100 */

101 public voidputQueue(String key, Object value) {102 try{103

104 redisTemplate.opsForList().leftPush(key, value);105

106 } catch(Exception e) {107 logger.error("PUT Queue cache exception [key=" + key + ", value=" + value + "].", e);108 }109 }110

111 /**

112 * 获取队列缓存113 *114 *@paramkey115 *@return

116 */

117 publicObject getQueue(String key) {118 try{119

120 returnredisTemplate.opsForList().rightPop(key);121

122 } catch(Exception e) {123 logger.error("GET Queue cache exception [key=" + key + "].", e);124 return null;125 }126 }127

128 /******************************129 ********* 栈操作 ***********130 ******************************/

131 public voidputStack(String key, Object value) {132 try{133 redisTemplate.opsForList().leftPush(key, value);134 } catch(Exception e) {135 logger.error("PUT Stack cache exception [key=" + key + ", value=" + value + "].", e);136 }137 }138

139 publicObject getStack(String key) {140 try{141 returnredisTemplate.opsForList().leftPop(key);142

143 } catch(Exception e) {144 logger.error("GET Stack cache exception [key=" + key + "].", e);145 return null;146 }147 }148

149 public intlength(String key) {150

151 try{152 returnredisTemplate.opsForList().size(key).intValue();153 } catch(Exception e) {154 logger.error("GET cache length exception [key=" + key + "].", e);155 return 0;156 }157 }158

159 public void expire(String key, longtimeout, TimeUnit unit) {160 try{161 redisTemplate.expire(key, timeout, unit);162 } catch(Exception e) {163 logger.error("SET expire time exception [key=" + key + "].", e);164 }165 }166

167 /******************************168 ********* hash操作 ***********169 ******************************/

170 /**

171 * hash put all172 *173 *@paramkey174 *@parammap175 * @date 2015年10月12日176 */

177 public void hputs(String key, HashMap extends Object, ? extends Object>map) {178 try{179 redisTemplate.opsForHash().putAll(key, map);180 } catch(Exception e) {181 logger.error("PUT All Hash exception [key=" + key + "].", e);182 }183 }184

185 /**

186 * hash put187 *188 *@paramkey189 *@paramhashKey190 *@paramvalue191 * @date 2015年10月12日192 */

193 public voidhput(String key, Object hashKey, Object value) {194 try{195 redisTemplate.opsForHash().put(key, hashKey, value);196 } catch(Exception e) {197 logger.error("PUT Hash length exception [key=" + key + "].", e);198 }199 }200

201 /**

202 * hash get203 *204 *@paramkey205 *@paramhashKey206 *@return

207 * @date 2015年10月12日208 */

209 publicObject hget(String key, Object hashKey) {210 try{211 returnredisTemplate.opsForHash().get(key, hashKey);212 } catch(Exception e) {213 logger.error("GET Hash exception [key=" + key + "].", e);214 return null;215 }216 }217

218 /**

219 * hash remove220 *221 *@paramkey222 *@paramhashKey223 * @date 2015年10月12日224 */

225 public voidhrem(String key, Object hashKey) {226 try{227 redisTemplate.opsForHash().delete(key, hashKey);228 } catch(Exception e) {229 logger.error("DELETE Hash exception [key=" + key + "].", e);230 }231 }232

233 /**

234 * hash size235 *236 *@paramkey237 *@return

238 * @date 2015年10月12日239 */

240 public longhsize(String key) {241 try{242 returnredisTemplate.opsForHash().size(key);243 } catch(Exception e) {244 logger.error("GET Hash size exception [key=" + key + "].", e);245 return 0;246 }247 }248

249 /**

250 * hash keys251 *252 *@paramkey253 *@return

254 * @date 2015年10月12日255 */

256 public Set>hkeys(String key) {257 try{258 returnredisTemplate.opsForHash().keys(key);259 } catch(Exception e) {260 logger.error("GET Hash size exception [key=" + key + "].", e);261 return null;262 }263 }264

265 /**

266 * 取出Map267 */

268 public MaphMap(String key){269 try{270 returnredisTemplate.opsForHash().entries(key);271 } catch(Exception e) {272 logger.error("GET Map size exception [key=" + key + "].", e);273 return null;274 }275 }276

277

278 /************************************************************279 **********************zset 操作*****************************280 ************************************************************/

281 /**

282 *往Zset插入数据283 */

284 public voidzsetPut(String key,Object hashKey,Double score){285 try{286 redisTemplate.opsForZSet().add(key, hashKey, score);287 }catch(Exception e){288 logger.error("PUT Zset exception [key=" + key + "].", e);289 }290 }291

292 /**

293 * 查询Zset,按照开始结束score294 */

295 public Set>zsetGet(String key,Double arg0,Double arg1){296 try{297 returnredisTemplate.opsForZSet().rangeByScore(key, arg0, arg1);298 }catch(Exception e){299 logger.error("GET Zset exception [key=" + key + "].", e);300 return null;301 }302 }303

304 /**

305 * 模糊查询306 */

307 public SetfuzzyQuery(String pattern){308 try{309 returnredisTemplate.keys(pattern);310 }catch(Exception e){311 logger.error("GET fuzzyQuery exception [key=" + pattern + "].", e);312 return null;313 }314 }315

316 public RedisTemplategetRedisTemplate() {317 returnredisTemplate;318 }319

320 public void setRedisTemplate(RedisTemplateredisTemplate) {321 this.redisTemplate =redisTemplate;322 }323 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值