Redis五种基本数据类型实践

Redis五种基本数据类型实践

先获取对应类型的数据操作对象
在这里插入图片描述
或者
在这里插入图片描述
opsForValue和boundValueOps在操作目的上没有什么区别,仅仅是boundValueOps提供了对key的“bound”(绑定)便捷化操作API,可以通过bound封装指定的key,然后进行一系列的操作而无须“显式”的再次指定Key,即BoundKeyOperations

  1. String
//获取String的操作对象
ValueOperations opsForValue = redisTemplate.opsForValue();
opsForValue.set(key, value);
//设置过期时间一分钟 
opsForValue.set(key, value, 10, TimeUnit.SECONDS);
//存在则追加 不存在则新建
opsForValue.append(key, value);
//key的自减操作 decrement
//key的自加操作 increment 
//long delta 每次变更的数值
opsForValue.decrement(key);
opsForValue.increment(key);
  1. Hash
//获取Hash的操作对象
HashOperations hashOperations = redisTemplate.opsForHash();
//hash 保存的是一个map对象
HashMap<String, Object> stringObjectHashMap = new HashMap<>();
stringObjectHashMap.put("公司名称", "alibaba");
stringObjectHashMap.put("规模", "1w");
stringObjectHashMap.put("位置", "成都");
hashOperations.putAll("alibaba", stringObjectHashMap);
//hash设置过期时间,并不能想String一样设置过期时间
redisTemplate.expire("alibaba", 10, TimeUnit.SECONDS);
  1. List
ListOperations listOperations = redisTemplate.opsForList();
//pop 弹出  可以left,可以right
//push 入栈
//Long rightPush(K key, V pivot, V value)  在链表 key 发现pivot的右边添加一个value ;
listOperations.leftPush("num", 1);
listOperations.leftPush("num", 2);
listOperations.leftPush("num", 4);
listOperations.leftPush("num", 6);
listOperations.leftPush("num", 7);
listOperations.leftPush("num", 4, 5);
listOperations.rightPush("num", 4, 3);
  1. Set
SetOperations setOperations = redisTemplate.opsForSet();
//add 给一个key增加元素,这个元素可以是一个 可以是多个
//members,取出key中的元素
//pop 随机弹出一个元素,可以指定数量
//intersect 两个集合的交集
//difference 两个集合的差异
/*使用 intersect() 方法可以计算两个集合,或者多个集合跟某个集合的交集。方法定义如下:
Set<V> intersect(K key, Collection<K> otherKeys) 获取 key 和所有 otherKeys 集合中键的交集
Set<V> intersect(K key, K otherKey) 获取 key 和 otherKey 对应集合间的交集
Long intersectAndStore(K key, Collection<K> otherKeys, K destKey) 获取 key 和所有 otherKeys 集合中键的交集,并将结果存储在 destKey 键中。
Long intersectAndStore(K key, K otherKey, K destKey) 获取 key 和 otherKey 对应集合间的交集,并将结果存储在 destKey 键中。
Set<V> difference(K key, Collection<K> otherKeys) 计算 key 和 otherKeys 集合的差异,返回 key 集合中所有没有在 otherKeys 所有集合中出现的元素集合。
Set<V> difference(K key, K otherKey) 计算 key 和 otherKey 集合的差异,返回 key 集合中所有没有在 otherKey 集合中出现的元素集合。
Long differenceAndStore(K key, Collection<K> otherKeys, K destKey) 计算 key 和 otherKeys 集合的差异,返回 key 集合中所有没有在 otherKeys 所有集合中出现的元素集合。并且将计算结果保存到 destKey 中。
Long differenceAndStore(K key, K otherKey, K destKey) 计算 key 和 otherKey 集合的差异,返回 key 集合中所有没有在 otherKey 集合中出现的元素集合。并且将计算结果保存到 destKey 中。
*/
setOperations.add("number","1", "2", "2", "3");//会去重
setOperations.members("number");//1,2,3
setOperations.pop("number");
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, "1", "2", "2", "3");
//setOperations.difference(list);
return "Success to set Set";
  1. ZSet
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
//add 添加一个元素 key value 权重/分数
//count(K,Smin,Smax):键为K的集合,Smin<=score<=Smax的元素个数
//remove(K,obj):删除,键为K的集合,value为obj的元素
//removeRange(K,start,end):删除,键为K的集合,索引start<=index<=end的元素子集
//incrementScore(K,V,delta):元素分数增加,delta是增量
zSetOperations.add("cat", "A", 1);
zSetOperations.add("cat", "B", 5);
Long count = zSetOperations.count("cat", 0, 10);
System.out.println(count);
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值