自定义RedisTemplate
没有序列化报错:
下面我们将user对象序列化
再测试:
默认的是用jdk序列化,如果用自己的就要在配置类中重新编写
1 package com.company.utils; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.data.redis.core.RedisTemplate; 5 import org.springframework.stereotype.Component; 6 import org.springframework.util.CollectionUtils; 7 8 import java.util.Collection; 9 import java.util.List; 10 import java.util.Map; 11 import java.util.Set; 12 import java.util.concurrent.TimeUnit; 13 14 //真实开发中,在公司都会看到一个公司自己封装的RedisUtil等工具类 15 @Component 16 public final class RedisUtil { 17 /** 18 * Redis工具类 19 * @author ZENG.XIAO.YAN 20 * @date 2018年6月7日 21 */ 22 23 24 25 @Autowired 26 private RedisTemplate<String, Object> redisTemplate; 27 28 // =============================common============================ 29 /** 30 * 指定缓存失效时间 (工作经常使用) 31 * @param key 键 32 * @param time 时间(秒) 33 34 */ 35 public boolean expire(String key, long time) { 36 try { 37 if (time > 0) { 38 redisTemplate.expire(key, time, TimeUnit.SECONDS); 39 } 40 return true; 41 } catch (Exception e) { 42 e.printStackTrace(); 43 return false; 44 } 45 } 46 47 /** 48 * 根据key 获取过期时间 49 * @param key 键 不能为null 50 * @return 时间(秒) 返回0代表为永久有效 51 */ 52 public long getExpire(String key) { 53 return redisTemplate.getExpire(key, TimeUnit.SECONDS); 54 } 55 56 /** 57 * 判断key是否存在 58 * @param key 键 59 * @return true 存在 false不存在 60 */ 61 public boolean hasKey(String key) { 62 try { 63 return redisTemplate.hasKey(key); 64 } catch (Exception e) { 65 e.printStackTrace(); 66 return false; 67 } 68 } 69 70 /** 71 * 删除缓存 72 * @param key 可以传一个值 或多个 73 */ 74 @SuppressWarnings("unchecked") 75 public void del(String... key) { 76 if (key != null && key.length > 0) { 77 if (key.length == 1) { 78 redisTemplate.delete(key[0]); 79 } else { 80 redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key)); 81 } 82 } 83 } 84 85 // ============================String============================= 86 /** 87 * 普通缓存获取 88 * @param key 键 89 * @return 值 90 */ 91 public Object get(String key) { 92 return key == null ? null : redisTemplate.opsForValue().get(key); 93 } 94 95 /** 96 * 普通缓存放入 97 * @param key 键 98 * @param value 值 99 * @return true成功 false失败 100 */ 101 public boolean set(String key, Object value) { 102 try { 103 redisTemplate.opsForValue().set(key, value); 104 return true; 105 } catch (Exception e) { 106 e.printStackTrace(); 107 return false; 108 } 109 110 } 111 112 /** 113 * 普通缓存放入并设置时间 114 * @param key 键 115 * @param value 值 116 * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期 117 * @return true成功 false 失败 118 */ 119 public boolean set(String key, Object value, long time) { 120 try { 121 if (time > 0) { 122 redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); 123 } else { 124 set(key, value); 125 } 126 return true; 127 } catch (Exception e) { 128 e.printStackTrace(); 129 return false; 130 } 131 } 132 133 /** 134 * 递增 135 * @param key 键 136 * @param delta 要增加几(大于0) 137 * @return 138 */ 139 public long incr(String key, long delta) { 140 if (delta < 0) { 141 throw new RuntimeException("递增因子必须大于0"); 142 } 143 return redisTemplate.opsForValue().increment(key, delta); 144 } 145 146 /** 147 * 递减 148 * @param key 键 149 * @param delta 要减少几(小于0) 150 * @return 151 */ 152 public long decr(String key, long delta) { 153 if (delta < 0) { 154 throw new RuntimeException("递减因子必须大于0"); 155 } 156 return redisTemplate.opsForValue().increment(key, -delta); 157 } 158 159 // ================================Map================================= 160 /** 161 * HashGet 162 * @param key 键 不能为null 163 * @param item 项 不能为null 164 * @return 值 165 */ 166 public Object hget(String key, String item) { 167 return redisTemplate.opsForHash().get(key, item); 168 } 169 170 /** 171 * 获取hashKey对应的所有键值 172 * @param key 键 173 * @return 对应的多个键值 174 */ 175 public Map<Object, Object> hmget(String key) { 176 return redisTemplate.opsForHash().entries(key); 177 } 178 179 /** 180 * HashSet 181 * @param key 键 182 * @param map 对应多个键值 183 * @return true 成功 false 失败 184 */ 185 public boolean hmset(String key, Map<String, Object> map) { 186 try { 187 redisTemplate.opsForHash().putAll(key, map); 188 return true; 189 } catch (Exception e) { 190 e.printStackTrace(); 191 return false; 192 } 193 } 194 195 /** 196 * HashSet 并设置时间 197 * @param key 键 198 * @param map 对应多个键值 199 * @param time 时间(秒) 200 * @return true成功 false失败 201 */ 202 public boolean hmset(String key, Map<String, Object> map, long time) { 203 try { 204 redisTemplate.opsForHash().putAll(key, map); 205 if (time > 0) { 206 expire(key, time); 207 } 208 return true; 209 } catch (Exception e) { 210 e.printStackTrace(); 211 return false; 212 } 213 } 214 215 /** 216 * 向一张hash表中放入数据,如果不存在将创建 217 * @param key 键 218 * @param item 项 219 * @param value 值 220 * @return true 成功 false失败 221 */ 222 public boolean hset(String key, String item, Object value) { 223 try { 224 redisTemplate.opsForHash().put(key, item, value); 225 return true; 226 } catch (Exception e) { 227 e.printStackTrace(); 228 return false; 229 } 230 } 231 232 /** 233 * 向一张hash表中放入数据,如果不存在将创建 234 * @param key 键 235 * @param item 项 236 * @param value 值 237 * @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间 238 * @return true 成功 false失败 239 */ 240 public boolean hset(String key, String item, Object value, long time) { 241 try { 242 redisTemplate.opsForHash().put(key, item, value); 243 if (time > 0) { 244 expire(key, time); 245 } 246 return true; 247 } catch (Exception e) { 248 e.printStackTrace(); 249 return false; 250 } 251 } 252 253 /** 254 * 删除hash表中的值 255 * @param key 键 不能为null 256 * @param item 项 可以使多个 不能为null 257 */ 258 public void hdel(String key, Object... item) { 259 redisTemplate.opsForHash().delete(key, item); 260 } 261 262 /** 263 * 判断hash表中是否有该项的值 264 * @param key 键 不能为null 265 * @param item 项 不能为null 266 * @return true 存在 false不存在 267 */ 268 public boolean hHasKey(String key, String item) { 269 return redisTemplate.opsForHash().hasKey(key, item); 270 } 271 272 /** 273 * hash递增 如果不存在,就会创建一个 并把新增后的值返回 274 * @param key 键 275 * @param item 项 276 * @param by 要增加几(大于0) 277 * @return 278 */ 279 public double hincr(String key, String item, double by) { 280 return redisTemplate.opsForHash().increment(key, item, by); 281 } 282 283 /** 284 * hash递减 285 * @param key 键 286 * @param item 项 287 * @param by 要减少记(小于0) 288 * @return 289 */ 290 public double hdecr(String key, String item, double by) { 291 return redisTemplate.opsForHash().increment(key, item, -by); 292 } 293 294 // ============================set============================= 295 /** 296 * 根据key获取Set中的所有值 297 * @param key 键 298 299 */ 300 public Set<Object> sGet(String key) { 301 try { 302 return redisTemplate.opsForSet().members(key); 303 } catch (Exception e) { 304 e.printStackTrace(); 305 return null; 306 } 307 } 308 309 /** 310 * 根据value从一个set中查询,是否存在 311 * @param key 键 312 * @param value 值 313 * @return true 存在 false不存在 314 */ 315 public boolean sHasKey(String key, Object value) { 316 try { 317 return redisTemplate.opsForSet().isMember(key, value); 318 } catch (Exception e) { 319 e.printStackTrace(); 320 return false; 321 } 322 } 323 324 /** 325 * 将数据放入set缓存 326 * @param key 键 327 * @param values 值 可以是多个 328 * @return 成功个数 329 */ 330 public long sSet(String key, Object... values) { 331 try { 332 return redisTemplate.opsForSet().add(key, values); 333 } catch (Exception e) { 334 e.printStackTrace(); 335 return 0; 336 } 337 } 338 339 /** 340 * 将set数据放入缓存 341 * @param key 键 342 * @param time 时间(秒) 343 * @param values 值 可以是多个 344 * @return 成功个数 345 */ 346 public long sSetAndTime(String key, long time, Object... values) { 347 try { 348 Long count = redisTemplate.opsForSet().add(key, values); 349 if (time > 0) 350 expire(key, time); 351 return count; 352 } catch (Exception e) { 353 e.printStackTrace(); 354 return 0; 355 } 356 } 357 358 /** 359 * 获取set缓存的长度 360 * @param key 键 361 * @return 362 */ 363 public long sGetSetSize(String key) { 364 try { 365 return redisTemplate.opsForSet().size(key); 366 } catch (Exception e) { 367 e.printStackTrace(); 368 return 0; 369 } 370 } 371 372 /** 373 * 移除值为value的 374 * @param key 键 375 * @param values 值 可以是多个 376 * @return 移除的个数 377 */ 378 public long setRemove(String key, Object... values) { 379 try { 380 Long count = redisTemplate.opsForSet().remove(key, values); 381 return count; 382 } catch (Exception e) { 383 e.printStackTrace(); 384 return 0; 385 } 386 } 387 // ===============================list================================= 388 389 /** 390 * 获取list缓存的内容 391 * @param key 键 392 * @param start 开始 393 * @param end 结束 0 到 -1代表所有值 394 395 */ 396 public List<Object> lGet(String key, long start, long end) { 397 try { 398 return redisTemplate.opsForList().range(key, start, end); 399 } catch (Exception e) { 400 e.printStackTrace(); 401 return null; 402 } 403 } 404 405 /** 406 * 获取list缓存的长度 407 * @param key 键 408 409 */ 410 public long lGetListSize(String key) { 411 try { 412 return redisTemplate.opsForList().size(key); 413 } catch (Exception e) { 414 e.printStackTrace(); 415 return 0; 416 } 417 } 418 419 /** 420 * 通过索引 获取list中的值 421 * @param key 键 422 * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推 423 424 */ 425 public Object lGetIndex(String key, long index) { 426 try { 427 return redisTemplate.opsForList().index(key, index); 428 } catch (Exception e) { 429 e.printStackTrace(); 430 return null; 431 } 432 } 433 434 /** 435 * 将list放入缓存 436 * @param key 键 437 * @param value 值 438 439 */ 440 public boolean lSet(String key, Object value) { 441 try { 442 redisTemplate.opsForList().rightPush(key, value); 443 return true; 444 } catch (Exception e) { 445 e.printStackTrace(); 446 return false; 447 } 448 } 449 450 /** 451 * 将list放入缓存 452 * @param key 键 453 * @param value 值 454 * @param time 时间(秒) 455 456 */ 457 public boolean lSet(String key, Object value, long time) { 458 try { 459 redisTemplate.opsForList().rightPush(key, value); 460 if (time > 0) 461 expire(key, time); 462 return true; 463 } catch (Exception e) { 464 e.printStackTrace(); 465 return false; 466 } 467 } 468 469 /** 470 * 将list放入缓存 471 * @param key 键 472 * @param value 值 473 474 */ 475 public boolean lSet(String key, List<Object> value) { 476 try { 477 redisTemplate.opsForList().rightPushAll(key, value); 478 return true; 479 } catch (Exception e) { 480 e.printStackTrace(); 481 return false; 482 } 483 } 484 485 /** 486 * 将list放入缓存 487 * 488 * @param key 键 489 * @param value 值 490 * @param time 时间(秒) 491 * @return 492 */ 493 public boolean lSet(String key, List<Object> value, long time) { 494 try { 495 redisTemplate.opsForList().rightPushAll(key, value); 496 if (time > 0) 497 expire(key, time); 498 return true; 499 } catch (Exception e) { 500 e.printStackTrace(); 501 return false; 502 } 503 } 504 505 /** 506 * 根据索引修改list中的某条数据 507 * @param key 键 508 * @param index 索引 509 * @param value 值 510 * @return 511 */ 512 public boolean lUpdateIndex(String key, long index, Object value) { 513 try { 514 redisTemplate.opsForList().set(key, index, value); 515 return true; 516 } catch (Exception e) { 517 e.printStackTrace(); 518 return false; 519 } 520 } 521 522 /** 523 * 移除N个值为value 524 * @param key 键 525 * @param count 移除多少个 526 * @param value 值 527 * @return 移除的个数 528 */ 529 public long lRemove(String key, long count, Object value) { 530 try { 531 Long remove = redisTemplate.opsForList().remove(key, count, value); 532 return remove; 533 } catch (Exception e) { 534 e.printStackTrace(); 535 return 0; 536 } 537 } 538 }
所有的redis操作容易,理解数据结构的作用场景