RedisTemplate操作Hash的API



在这里插入图片描述

在这里插入图片描述


介绍

Hash类型,也叫散列,其value是一个无序字典,类似于Java中的 HashMap 结构。

在这里插入图片描述


String 结构是将对象序列化为JSON字符串后存储,当需要修改对象某个字段时很不方便:

keyvalue
snow:user:1{“id”:1, “name”: “Snow”, “age”: 18}
snow:product:1{“id”:1, “name”: “Apple”, “price”: 9999}

Hash结构可以将对象中的每个字段独立存储,可以针对单个字段做CRUD

在这里插入图片描述


命令

Hash 类型的常见命令🌈🌈
HSET key field value:添加或者修改 hash 类型 key 的 field 的值
HGET key field:获取一个hash类型key的field的值
HMSET:批量添加多个hash类型key的field的值
HMGET:批量获取多个hash类型key的field的值
HGETALL:获取一个hash类型的key中的所有的field和value
HKEYS:获取一个hash类型的key中的所有的field
HVALS:获取一个hash类型的key中的所有的value
HINCRBY:让一个hash类型key的字段值自增并指定步长
HSETNX:添加一个hash类型的key的field值,前提是这个field不存在,否则不执行


RedisTemplate API

❄️❄️添加缓存

//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");

❄️❄️设置过期时间(单独设置)

HashMap<String, String> hashMap = new HashMap<>();
redisTemplate.boundHashOps("HashKey").putAll(hashMap );
// 将"HashKey"设置为10秒后过期
redisTemplate.expire("HashKey", 10, TimeUnit.SECONDS);

注意:只能给大 KEY 设置过期时间, 小 KEY 不能拥有独立的过期时间.


❄️❄️添加一个Map集合

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

❄️❄️提取所有的小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");

❄️❄️提取所有的value值

//1、通过redisTemplate获取值
List values1 = redisTemplate.boundHashOps("HashKey").values();

//2、通过BoundValueOperations获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
List values2 = hashKey.values();

//3、通过ValueOperations获取值
HashOperations hashOps = redisTemplate.opsForHash();
List values3 = hashOps.values("HashKey");

❄️❄️根据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");

❄️❄️获取所有的键值对集合

//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");

❄️❄️删除

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

❄️❄️判断Hash中是否含有该值

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

RedisTemplate API 实战

❄️❄️添加 put / putAll

//	初始数据:
template.opsForHash().put("redisHash","name","tom");
template.opsForHash().put("redisHash","age",26);
template.opsForHash().put("redisHash","class","6");

Map<String,Object> testMap = new HashMap();
testMap.put("name","jack");
testMap.put("age",27);
testMap.put("class","1");
template.opsForHash().putAll("redisHash1",testMap);

仅当hashKey不存在时才设置散列hashKey的值。

System.out.println(template.opsForHash().putIfAbsent("redisHash","age",30));
System.out.println(template.opsForHash().putIfAbsent("redisHash","kkk","kkk"));
//结果:
false
true

❄️❄️删除小Key

template.opsForHash().delete("redisHash","name");

❄️❄️删除大Key

template.opsForHash().delete("redisHash");

❄️❄️判断key是否存在

template.opsForHash().hasKey("redisHash","age")

❄️❄️获取

template.opsForHash().get("redisHash","age")

❄️❄️增加散列hashKey的值(整型)

System.out.println(template.opsForHash().get("redisHash","age"));
System.out.println(template.opsForHash().increment("redisHash","age",1));
//	结果:
26
27

❄️❄️获取key所对应的散列表的key

System.out.println(template.opsForHash().keys("redisHash1"));
//redisHash1所对应的散列表为{class=1, name=jack, age=27}
//结果:[name, class, age]

❄️❄️获取key所对应的散列表的大小个数

template.opsForHash().size("redisHash1");
//redisHash1所对应的散列表为{class=1, name=jack, age=27}
//结果:3

❄️ ❄️获取整个哈希存储的值

template.opsForHash().values("redisHash");
//	结果:[tom, 26, 6]

❄️❄️获取整个哈希存储

template.opsForHash().entries("redisHash");
//	结果:{age=26, class=6, name=tom}

❄️❄️使用Cursor在key的hash中迭代,相当于迭代器。

Cursor<Map.Entry<Object, Object>> curosr 
	= ops.scan("redisHash", ScanOptions.ScanOptions.NONE);
        while(curosr.hasNext()){
            Map.Entry<Object, Object> entry = curosr.next();
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
//结果:
age:28.1
class:6
kkk:kkk

在这里插入图片描述



  • 15
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值