Redis操作Set工具类封装,Java Redis Set命令封装


http://fanshuyao.iteye.com/
Redis操作字符串工具类封装:http://fanshuyao.iteye.com/blog/2326221
Redis操作Hash工具类封装:http://fanshuyao.iteye.com/blog/2327134
Redis操作List工具类封装:http://fanshuyao.iteye.com/blog/2327137
Redis操作Set工具类封装:http://fanshuyao.iteye.com/blog/2327228
Redis的Set是string类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。
Redis 中 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。
集合中最大的成员数为 232 - 1 (4294967295, 每个集合可存储40多亿个成员)。
2017-07-24对代码进行了优化,采用try catch finally包装了一层,避免Redis获取、使用时发生错误没有回收资源。
注:下面的代码只是方法封装,缺少一部分,因为是【Redis操作字符串工具类封装:http://fanshuyao.iteye.com/blog/2326221】的延续,把下面的代码增加到之前代码后面就可以了。
/**************************** redis 集合Set start***************************/
/**Redis的Set是string类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。**/
/**
* 向集合添加一个或多个成员,返回添加成功的数量
* @param key
* @param members
* @return Long
*/
public static Long sadd(String key, String... members){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.sadd(key, members);
}catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
jedis.close();
}
}
return null;
}
/**
* 获取集合的成员数
* @param key
* @return
*/
public static Long scard(String key){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.scard(key);
}catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
jedis.close();
}
}
return null;
}
/**
* 返回集合中的所有成员
* @param key
* @return Set
*/
public static Set smembers(String key){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.smembers(key);
}catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
jedis.close();
}
}
return null;
}
/**
* 判断 member 元素是否是集合 key 的成员,在集合中返回True
* @param key
* @param member
* @return Boolean
*/
public static Boolean sIsMember(String key, String member){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.sismember(key, member);
}catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
jedis.close();
}
}
return null;
}
/**
* 返回给定所有集合的差集(获取第一个key中与其它key不相同的值,当只有一个key时,就返回这个key的所有值)
* @param keys
* @return Set
*/
public static Set sdiff(String... keys){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.sdiff(keys);
}catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
jedis.close();
}
}
return null;
}
/**
* 返回给定所有集合的差集并存储在 targetKey中,类似sdiff,只是该方法把返回的差集保存到targetKey中
* 
当有差集时,返回true 
 
* 
当没有差集时,返回false 
 
* @param targetKey
* @param keys
* @return
*/
public static boolean sdiffStore(String targetKey, String... keys){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
Long statusCode = jedis.sdiffstore(targetKey, keys);
if(SUCCESS_STATUS_LONG == statusCode){
return true;
}
}catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
jedis.close();
}
}
return false;
}
/**
* 返回给定所有集合的交集(获取第一个key中与其它key相同的值,要求所有key都要有相同的值,如果没有相同,返回Null。当只有一个key时,就返回这个key的所有值)
* @param keys
* @return Set
*/
public static Set sinter(String... keys){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.sinter(keys);
}catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
jedis.close();
}
}
return null;
}
/**
* 返回给定所有集合的交集并存储在 targetKey中,类似sinter
* @param targetKey
* @param keys
* @return boolean
*/
public static boolean sinterStore(String targetKey, String... keys){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
Long statusCode = jedis.sinterstore(targetKey, keys);
if(SUCCESS_STATUS_LONG == statusCode){
return true;
}
}catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
jedis.close();
}
}
return false;
}
/**
* 将 member 元素从 sourceKey 集合移动到 targetKey 集合
* 
成功返回true 
 
* 
当member不存在于sourceKey时,返回false 
 
* 
当sourceKey不存在时,也返回false 
 
* @param sourceKey
* @param targetKey
* @param member
* @return boolean
*/
public static boolean smove(String sourceKey, String targetKey, String member){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
Long value = jedis.smove(sourceKey, targetKey, member);
if(value > 0){
return true;
}
}catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
jedis.close();
}
}
return false;
}
/**
* 移除并返回集合中的一个随机元素
* 
当set为空或者不存在时,返回Null 
 
* @param key
* @return String
*/
public static String spop(String key){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.spop(key);
}catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
jedis.close();
}
}
return null;
}
/**
* 返回集合中一个或多个随机数
* 
当count大于set的长度时,set所有值返回,不会抛错。 
 
* 
当count等于0时,返回[] 
 
* 
当count小于0时,也能返回。如-1返回一个,-2返回两个 
 
* @param key
* @param count
* @return List
*/
public static List srandMember(String key, int count){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.srandmember(key, count);
}catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
jedis.close();
}
}
return null;
}
/**
* 移除集合中一个或多个成员
* @param key
* @param members
* @return
*/
public static boolean srem(String key, String... members){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//Integer reply, specifically: 1 if the new element was removed
//0 if the new element was not a member of the set
Long value = jedis.srem(key, members);
if(value > 0){
return true;
}
}catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
jedis.close();
}
}
return false;
}
/**
* 返回所有给定集合的并集,相同的只会返回一个
* @param keys
* @return
*/
public static Set sunion(String... keys){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.sunion(keys);
}catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
jedis.close();
}
}
return null;
}
/**
* 所有给定集合的并集存储在targetKey集合中
* 
注:合并时,只会把keys中的集合返回,不包括targetKey本身 
 
* 
如果targetKey本身是有值的,合并后原来的值是没有的,因为把keys的集合重新赋值给targetKey 
 
* 
要想保留targetKey本身的值,keys要包含原来的targetKey 
 
* @param targetKey
* @param keys
* @return
*/
public static boolean sunionStore(String targetKey, String... keys){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//返回合并后的长度
Long statusCode = jedis.sunionstore(targetKey, keys);
if(statusCode > 0){
return true;
}
}catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
jedis.close();
}
}
return false;
}

/**************************** redis 集合Set end***************************/

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>