redisTemplate常用方法

1:取出來是 Map<String,Object>

//沒有條件查詢的話先查詢redis緩存
Map<String,Object> redisMap= redisTemplate.opsForHash().entries(key);
 //redis中沒有的話查詢數據庫
 if(redisMap!=null&&redisMap.size()>0){
     return (HashMap<String, Object>) redisMap;

2:取出來是object

//查redis
ValueOperations<String, Product> operations = redisTemplate.opsForValue();
//查看key是否存在
boolean hasKey = redisTemplate.hasKey(productid.toString());
if(hasKey){
    Product productRedis = operations.get(productid.toString());
    return productRedis;

3:添加object到redis

//添加到redis
ValueOperations<String, Product> operations = redisTemplate.opsForValue();
operations.set(product.getProductid().toString(), product, 5, TimeUnit.HOURS);

4:添加hashMap到redis

HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("total", count);
hashMap.put("rows", productLsit);
//把map集合保存到redis中
redisTemplate.opsForHash().putAll(key,hashMap);

5:刪除方法

//刪除完數據庫,刪除redis
for (int i = 0; i < ids.length; i++) {
    Integer key = ids[i];
    redisTemplate.delete(key.toString());
 }

6:存取List

 //存儲
 redisTemplate.opsForList().rightPushAll("userList", userList);

 //取出
 List<Person> oowwoo = redisTemplate.opsForList().range("userList", 0, -1);

更詳細請借鑑:
https://blog.csdn.net/sinat_22797429/article/details/89196933

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章