根据key获取Set中的所有值
/**
* sMembers命令 根据key获取Set中的所有值
*
* @param key 键
* @return set 值的集合
*/
public Set<Object> sMembers(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
根据value从一个set中查询,是否存在
/**
* sIsMember命令 根据value从一个set中查询,是否存在
*
* @param key 键
* @param value 值
* @return true 存在 false不存在
*/
public boolean sIsMember(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
将数据放入set缓存
/**
* sAdd命令 将数据放入set缓存
*
* @param key 键
* @param values 值 可以是多个
* @return 成功个数
*/
public long sAdd(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
将set数据放入缓存
/**
* 将set数据放入缓存
*
* @param key 键
* @param time 时间(秒)
* @param values 值 可以是多个
* @return 成功个数
*/
public long sAddWithTime(String key, long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if (time > 0) {
expire(key, time);
}
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
获取set缓存的长度
/**
* sCard命令 获取set缓存的长度
*
* @param key 键
* @return long 个数
*/
public long sCard(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
移除值为value的
/**
* sRem 移除值为value的
*
* @param key 键
* @param values 值 可以是多个
* @return 移除的个数
*/
public long sRem(String key, Object... values) {
try {
return redisTemplate.opsForSet().remove(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
获取key1与key2的差集
/**
* sDiff命令 获取key1与key2的差集
*
* @param key1 键
* @param key2 键
* @return 差集
*/
public Set<Object> sDiff(String key1, String key2) {
try {
return redisTemplate.opsForSet().difference(key1, key2);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
获取key1与key2的交集
/**
* sInter命令 获取key1与key2的交集
*
* @param key1 键
* @param key2 键
* @return 交集
*/
public Set<Object> sinter(String key1, String key2) {
try {
return redisTemplate.opsForSet().intersect(key1, key2);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
获取key1与key2的并集
/**
* sUnion命令 获取key1与key2的并集
*
* @param key1 键
* @param key2 键
* @return 并集
*/
public Set<Object> sUnion(String key1, String key2) {
try {
return redisTemplate.opsForSet().union(key1, key2);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
获取key1与key2的交集存入destKey中
/**
* sDiffStore命令 获取key1与key2的交集存入destKey中
*
* @param key1 键1
* @param key2 键2
* @param destKey 目标键
* @return 差集存入个数
*/
public Long sDiffStore(String key1, String key2, String destKey) {
try {
return redisTemplate.opsForSet().differenceAndStore(key1, key2, destKey);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
获取key1与key2的交集存入destKey中
/**
* sInterStore命令 获取key1与key2的交集存入destKey中
*
* @param key1 键1
* @param key2 键2
* @param destKey 目标键
* @return 交集存入个数
*/
public Long sInterStore(String key1, String key2, String destKey) {
try {
return redisTemplate.opsForSet().intersectAndStore(key1, key2, destKey);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
获取key1与key2的并集存入destKey中
/**
* sUnionStore命令 获取key1与key2的并集存入destKey中
*
* @param key1 键1
* @param key2 键2
* @param destKey 目标键
* @return 并集存入个数
*/
public Long sUnionStore(String key1, String key2, String destKey) {
try {
return redisTemplate.opsForSet().unionAndStore(key1, key2, destKey);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
从集合中随机取出一个成员
/**
* sRandMember命令 从集合中随机取出一个成员
*
* @param key 键
* @return 随机取出的成员
*/
public Object sRandMember(String key) {
try {
return redisTemplate.opsForSet().randomMember(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
从集合中随机取出count个成员(可重复)
/**
* 从集合中随机取出count个成员(可重复)
*
* @param key 键
* @param count 个数
* @return list 可重复集合
*/
public List<Object> sRandMembers(String key, long count) {
try {
return redisTemplate.opsForSet().randomMembers(key, count);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
从集合中随机取出count个成员(不重复)
/**
* 从集合中随机取出count个成员(不重复)
*
* @param key 键
* @param count 数量
* @return set 不重复集合
*/
public Set<Object> distinctRandomMembers(String key, long count) {
try {
return redisTemplate.opsForSet().distinctRandomMembers(key, count);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
移除并返回集合中的一个随机元素
/**
* sPop命令 移除并返回集合中的一个随机元素
*
* @param key 键
* @return 随机元素
*/
public Object sPop(String key) {
try {
return redisTemplate.opsForSet().pop(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
移除并返回集合中的count个随机元素
/**
* sPop命令 移除并返回集合中的count个随机元素
*
* @param key 键
* @param count 个数
* @return 随机元素
*/
public List<Object> sPop(String key, long count) {
try {
return redisTemplate.opsForSet().pop(key, count);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}