基础配置介绍已经在前面的《RedisTemplate常用集合使用说明(一)》中已经介绍了,现在我们直接介绍opsForSet()方法的使用:
1、add(K key, V... values)
向变量中批量添加值。
redisTemplate.opsForSet().add("setValue","A","B","C","B","D","E","F");
2、members(K key)
获取变量中的值。
Set set = redisTemplate.opsForSet().members("setValue"); System.out.println("通过members(K key)方法获取变量中的元素值:" + set);
3、size(K key)
获取变量中值的长度。
long setLength = redisTemplate.opsForSet().size("setValue"); System.out.println("通过size(K key)方法获取变量中元素值的长度:" + setLength);
4、randomMember(K key)
随机获取变量中的元素。
Object randomMember = redisTemplate.opsForSet().randomMember("setValue"); System.out.println("通过randomMember(K key)方法随机获取变量中的元素:" + randomMember);
5、randomMembers(K key, long count)
随机获取变量中指定个数的元素。
List randomMembers = redisTemplate.opsForSet().randomMembers("setValue",2); System.out.println("通过randomMembers(K key, long count)方法随机获取变量中指定个数的元素:" + randomMembers);
6、isMember(K key, Object o)
检查给定的元素是否在变量中。
boolean isMember = redisTemplate.opsForSet().isMember("setValue","A"); System.out.println("通过isMember(K key, Object o)方法检查给定的元素是否在变量中:" + isMember);
7、move(K key, V value, K destKey)
转移变量的元素值到目的变量。
boolean isMove = redisTemplate.opsForSet().move("setValue","A","destSetValue"); if(isMove){ set = redisTemplate.opsForSet().members("setValue"); System.out.print("通过move(K key, V value, K destKey)方法转移变量的元素值到目的变量后的剩余元素:" + set); set = redisTemplate.opsForSet().members("destSetValue"); System.out.println(",目的变量中的元素值:" + set); }
8、pop(K key)
弹出变量中的元素。
Object popValue = redisTemplate.opsForSet().pop("setValue"); System.out.print("通过pop(K key)方法弹出变量中的元素:" + popValue); set = redisTemplate.opsForSet().members("setValue"); System.out.println(",剩余元素:" + set)
9、remove(K key,
Object... values)
批量移除变量中的元素。
long removeCount = redisTemplate.opsForSet().remove("setValue","E","F","G"); System.out.print("通过remove(K key, Object... values)方法移除变量中的元素个数:" + removeCount); set = redisTemplate.opsForSet().members("setValue"); System.out.println(",剩余元素:" + set);
10、scan(K key,
ScanOptions options)
匹配获取键值对,ScanOptions.NONE为获取全部键值对;ScanOptions.scanOptions().match("C").build()匹配获取键位map1的键值对,不能模糊匹配。
//Cursor<Object> cursor = redisTemplate.opsForSet().scan("setValue", ScanOptions.NONE); Cursor<Object> cursor = redisTemplate.opsForSet().scan("setValue", ScanOptions.scanOptions().match("C").build()); while (cursor.hasNext()){ Object object = cursor.next(); System.out.println("通过scan(K key, ScanOptions options)方法获取匹配的值:" + object); }
11、difference(K key,
Collection<K> otherKeys)
通过集合求差值。
List list = new ArrayList(); list.add("destSetValue"); Set differenceSet = redisTemplate.opsForSet().difference("setValue",list); System.out.println("通过difference(K key, Collection<K> otherKeys)方法获取变量中与给定集合中变量不一样的值:" + differenceSet);
12、difference(K key, K otherKey)
通过给定的key求2个set变量的差值。
differenceSet = redisTemplate.opsForSet().difference("setValue","destSetValue"); System.out.println("通过difference(K key, Collection<K> otherKeys)方法获取变量中与给定变量不一样的值:" + differenceSet);
13、differenceAndStore(K key, K otherKey, K destKey)
将求出来的差值元素保存。
redisTemplate.opsForSet().differenceAndStore("setValue","destSetValue","storeSetValue"); set = redisTemplate.opsForSet().members("storeSetValue"); System.out.println("通过differenceAndStore(K key, K otherKey, K destKey)方法将求出来的差值元素保存:" + set);
14、differenceAndStore(K key,
Collection<K> otherKeys,
K destKey)
将求出来的差值元素保存。
redisTemplate.opsForSet().differenceAndStore("setValue",list,"storeSetValue"); set = redisTemplate.opsForSet().members("storeSetValue"); System.out.println("通过differenceAndStore(K key, Collection<K> otherKeys, K destKey)方法将求出来的差值元素保存:" + set);
15、distinctRandomMembers(K key, long count)
获取去重的随机元素。
set = redisTemplate.opsForSet().distinctRandomMembers("setValue",2); System.out.println("通过distinctRandomMembers(K key, long count)方法获取去重的随机元素:" + set);
16、intersect(K key, K otherKey)
获取2个变量中的交集。
set = redisTemplate.opsForSet().intersect("setValue","destSetValue"); System.out.println("通过intersect(K key, K otherKey)方法获取交集元素:" + set);
17、intersect(K key,
Collection<K> otherKeys)
获取多个变量之间的交集。
set = redisTemplate.opsForSet().intersect("setValue",list); System.out.println("通过intersect(K key, Collection<K> otherKeys)方法获取交集元素:" + set);
18、intersectAndStore(K key,
K otherKey,
K destKey)
获取2个变量交集后保存到最后一个参数上。
redisTemplate.opsForSet().intersectAndStore("setValue","destSetValue","intersectValue"); set = redisTemplate.opsForSet().members("intersectValue"); System.out.println("通过intersectAndStore(K key, K otherKey, K destKey)方法将求出来的交集元素保存:" + set);
19、intersectAndStore(K key, Collection<K> otherKeys, K destKey)
获取多个变量的交集并保存到最后一个参数上。
redisTemplate.opsForSet().intersectAndStore("setValue",list,"intersectListValue"); set = redisTemplate.opsForSet().members("intersectListValue"); System.out.println("通过intersectAndStore(K key, Collection<K> otherKeys, K destKey)方法将求出来的交集元素保存:" + set);
20、union(K key,
K otherKey)
获取2个变量的合集。
set = redisTemplate.opsForSet().union("setValue","destSetValue"); System.out.println("通过union(K key, K otherKey)方法获取2个变量的合集元素:" + set);
21、union(K key, Collection<K> otherKeys)
获取多个变量的合集。
set = redisTemplate.opsForSet().union("setValue",list); System.out.println("通过union(K key, Collection<K> otherKeys)方法获取多个变量的合集元素:" + set);
22、unionAndStore(K key,
K otherKey,
K destKey)
获取2个变量合集后保存到最后一个参数上。
redisTemplate.opsForSet().unionAndStore("setValue","destSetValue","unionValue"); set = redisTemplate.opsForSet().members("unionValue"); System.out.println("通过unionAndStore(K key, K otherKey, K destKey)方法将求出来的交集元素保存:" + set);
23、unionAndStore(K key, Collection<K> otherKeys, K destKey)
获取多个变量的合集并保存到最后一个参数上。
redisTemplate.opsForSet().unionAndStore("setValue",list,"unionListValue"); set = redisTemplate.opsForSet().members("unionListValue"); System.out.println("通过unionAndStore(K key, Collection<K> otherKeys, K destKey)方法将求出来的交集元素保存:" + set);
相关推荐
`RedisTemplate`提供了丰富的API,包括`opsForHash()`, `opsForList()`, `opsForSet()` 和 `opsForZSet()` 等,分别对应Redis的哈希、列表、集合和有序集合操作。这使得在Spring应用中操作Redis变得非常方便。 总之...
这个压缩包文件“SpringDataRedis对Redis的数据类型的常用操作API的使用代码举例.zip”显然包含了关于如何利用Spring Data Redis进行Redis数据类型操作的示例代码。下面我们将深入探讨Spring Data Redis对Redis主要...
5. **RedisTemplate的常用方法** - `opsForValue()`:操作单个值,如设置、获取、增加、减少等。 - `opsForHash()`:操作哈希,支持添加、删除、获取键值对等。 - `opsForList()`:操作列表,进行插入、删除、...
4. **RedisTemplate的使用**:RedisTemplate提供了丰富的操作方法,如`opsForValue()`用于操作字符串,`opsForHash()`处理哈希,`opsForSet()`处理集合,`opsForZSet()`处理有序集合等。这些操作方法都支持泛型,能...
例如,使用RedisTemplate的opsForHash()进行哈希操作,使用opsForList()处理列表数据,或者使用opsForSet()管理集合数据。同时,为了提高并发性能和数据一致性,可以引入Redlock或分布式锁策略。 在提供的压缩包...
3. **创建RedisTemplate Bean**:SpringBoot自动配置了`StringRedisTemplate`,但若需自定义配置,可以创建`RedisTemplate` Bean,设置序列化器等选项。 接下来,我们关注Redis工具类的实现,这通常包含以下功能: ...
`opsForValue()`用于操作字符串类型数据,`opsForHash()`处理哈希类型,`opsForSet()`管理集合,而`opsForList()`则用于列表类型的数据操作。 5. 在Maven的pom.xml文件中,排除依赖的标签是`<exclusions>`,可以...