RedisTemplate方法 - Hash

1. 添加缓存

//1. 通过redisTemplate设置值
redisTemplate.boundHashOps("HashKey").put("SmallKey", "HashVaue");

//2. 通过BoundValueOperations设置值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
hashKey.put("SmallKey", "HashVaue");

//3. 通过ValueOperations设置值
HashOperations hashOps = redisTemplate.opsForHash();
hashOps.put("HashKey", "SmallKey", "HashVaue");

2. 设置key过期时间

//方法1
redisTemplate.boundValueOps("HashKey").expire(1,TimeUnit.MINUTES);

//方法2
redisTemplate.expire("HashKey",1,TimeUnit.MINUTES);

3. 添加一个map集合

HashMap<String, String> hashMap = new HashMap<>();
redisTemplate.boundHashOps("HashKey").putAll(hashMap);

4. 提取所有小key

//1. 通过redisTemplate获取值
Set keys1 = redisTemplate.boundHashOps("HashKey").keys();

//2. 通过BoundValueOperations获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
Set keys2 = hashKey.keys();

//3. 通过ValueOperations获取值
HashOperations hashOps = redisTemplate.opsForHash();
Set keys3 = hashOps.keys("HashKey");

5. 提取所有value值

//1. 通过redisTemplate获取值
Set keys1 = redisTemplate.boundHashOps("HashKey").keys();

//2. 通过BoundValueOperations获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
Set keys2 = hashKey.keys();

//3. 通过ValueOperations获取值
HashOperations hashOps = redisTemplate.opsForHash();
Set keys3 = hashOps.keys("HashKey");

6. 根据key提取value值

//1. 通过redisTemplate获取
String value1 = (String) redisTemplate.boundHashOps("HashKey").get("SmallKey");

//2. 通过BoundValueOperations获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
String value2 = (String) hashKey.get("SmallKey");

//3. 通过ValueOperations获取值
HashOperations hashOps = redisTemplate.opsForHash();
String value3 = (String) hashOps.get("HashKey", "SmallKey");

7. 获取所有键值对集合

//1. 通过redisTemplate获取
Map entries = redisTemplate.boundHashOps("HashKey").entries();

//2. 通过BoundValueOperations获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
Map entries1 = hashKey.entries();

//3. 通过ValueOperations获取值
HashOperations hashOps = redisTemplate.opsForHash();
Map entries2 = hashOps.entries("HashKey");

8. 删除

//1. 删除小key
redisTemplate.boundHashOps("HashKey").delete("SmallKey");
//2. 删除大key
redisTemplate.delete("HashKey");

9. 判断Hash中是否含有该值

Boolean isEmpty = redisTemplate.boundHashOps("HashKey").hasKey("SmallKey");

完整演示案例

public Object redisHash(String key,String value) {
    String smallKey = "mall:goods:category:list";
    //1、通过redisTemplate设置值
    redisTemplate.boundHashOps(key).put(smallKey, value);
    System.out.println("1.添加hash - String型数据");

    //设置过期时间
    boolean isExpire = redisTemplate.expire(key, 1, TimeUnit.MINUTES);
    System.out.println("2.置过期时间:" + isExpire);

    //添加Map集合
    HashMap<String, String> hashMap = new HashMap<>();
    hashMap.put("name", "这是一个map集合数据");
    redisTemplate.boundHashOps(key).putAll(hashMap);
    System.out.println("3.添加hash - Map型数据");

    //提取所有小key
    Set keys1 = redisTemplate.boundHashOps(key).keys();
    System.out.println("4.提取所有小key:" + keys1);

    //提取所有value
    List values1 = redisTemplate.boundHashOps(key).values();
    System.out.println("5.提取所有value" + values1.toString());

    // 根据key提取value值
    String value1 = (String) redisTemplate.boundHashOps(key).get(smallKey);
    System.out.println("6.根据key提取value值:" + value1);

    // 获取所有的键值对集合
    Map entries = redisTemplate.boundHashOps(key).entries();
    System.out.println("7.获取所有的键值对集合:" + entries);

    //删除小key
    long minDel = redisTemplate.boundHashOps(key).delete(smallKey);
    System.out.println("8.删除指定的小key:" + minDel);

    //删除大key
    boolean maxDel = redisTemplate.delete(key);
    System.out.println("9.删除大key:" + maxDel);

    //判断Hash中是否含有该值
    Boolean isEmpty = redisTemplate.boundHashOps(key).hasKey(smallKey);
    System.out.println("10.判断Hash中是否含有该值" + isEmpty);
    return null;
}

//结果输出:
1.添加hash - String型数据
2.置过期时间:true
3.添加hash - Map型数据
4.提取所有小key:[mall:goods:category:list, name]
5.提取所有value[啦啦啦, 这是一个map集合数据]
6.根据key提取value值:啦啦啦
7.获取所有的键值对集合:{mall:goods:category:list=啦啦啦, name=这是一个map集合数据}
8.删除指定的小key:1
9.删除大key:true
10.判断Hash中是否含有该值:false