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

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

 

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

蕃薯耀 2016年9月27日 10:25:19 星期二

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多亿个成员)。

 

注:下面的代码只是方法封装,缺少一部分,因为是【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 = jedisPool.getResource();
		Long value = jedis.sadd(key, members);
		jedis.close();
		return value;
	}
	
	/**
	 * 获取集合的成员数
	 * @param key
	 * @return
	 */
	public static Long scard(String key){
		Jedis jedis = jedisPool.getResource();
		Long value = jedis.scard(key);
		jedis.close();
		return value;
	}
	
	/**
	 * 返回集合中的所有成员
	 * @param key
	 * @return Set<String>
	 */
	public static Set<String> smembers(String key){
		Jedis jedis = jedisPool.getResource();
		Set<String> values = jedis.smembers(key);
		jedis.close();
		return values;
	}
	
	/**
	 * 判断 member 元素是否是集合 key 的成员,在集合中返回True
	 * @param key
	 * @param member
	 * @return Boolean
	 */
	public static Boolean sIsMember(String key, String member){
		Jedis jedis = jedisPool.getResource();
		Boolean value = jedis.sismember(key, member);
		jedis.close();
		return value;
	}
	
	/**
	 * 返回给定所有集合的差集(获取第一个key中与其它key不相同的值,当只有一个key时,就返回这个key的所有值)
	 * @param keys
	 * @return Set<String>
	 */
	public static Set<String> sdiff(String... keys){
		Jedis jedis = jedisPool.getResource();
		Set<String> values = jedis.sdiff(keys);
		jedis.close();
		return values;
	}
	
	/**
	 * 返回给定所有集合的差集并存储在 targetKey中,类似sdiff,只是该方法把返回的差集保存到targetKey中
	 * <li>当有差集时,返回true</li>
	 * <li>当没有差集时,返回false</li>
	 * @param targetKey
	 * @param keys
	 * @return
	 */
	public static boolean sdiffStore(String targetKey, String... keys){
		Jedis jedis = jedisPool.getResource();
		Long statusCode = jedis.sdiffstore(targetKey, keys);
		jedis.close();
		if(SUCCESS_STATUS_LONG == statusCode){
			return true;
		}
		return false;
	}
	
	/**
	 * 返回给定所有集合的交集(获取第一个key中与其它key相同的值,要求所有key都要有相同的值,如果没有相同,返回Null。当只有一个key时,就返回这个key的所有值)
	 * @param keys
	 * @return Set<String>
	 */
	public static Set<String> sinter(String... keys){
		Jedis jedis = jedisPool.getResource();
		Set<String> values = jedis.sinter(keys);
		jedis.close();
		return values;
	}
	
	/**
	 * 返回给定所有集合的交集并存储在 targetKey中,类似sinter
	 * @param targetKey
	 * @param keys
	 * @return boolean
	 */
	public static boolean sinterStore(String targetKey, String... keys){
		Jedis jedis = jedisPool.getResource();
		Long statusCode = jedis.sinterstore(targetKey, keys);
		jedis.close();
		if(SUCCESS_STATUS_LONG == statusCode){
			return true;
		}
		return false;
	}
	
	/**
	 * 将 member 元素从 sourceKey 集合移动到 targetKey 集合
	 * <li>成功返回true</li>
	 * <li>当member不存在于sourceKey时,返回false</li>
	 * <li>当sourceKey不存在时,也返回false</li>
	 * @param sourceKey
	 * @param targetKey
	 * @param member
	 * @return boolean
	 */
	public static boolean smove(String sourceKey, String targetKey, String member){
		Jedis jedis = jedisPool.getResource();
		Long value = jedis.smove(sourceKey, targetKey, member);
		jedis.close();
		if(value > 0){
			return true;
		}
		return false;
	}
	
	/**
	 * 移除并返回集合中的一个随机元素
	 * <li>当set为空或者不存在时,返回Null</li>
	 * @param key
	 * @return String
	 */
	public static String spop(String key){
		Jedis jedis = jedisPool.getResource();
		String value = jedis.spop(key);
		jedis.close();
		return value;
	}
	
	/**
	 * 返回集合中一个或多个随机数
	 * <li>当count大于set的长度时,set所有值返回,不会抛错。</li>
	 * <li>当count等于0时,返回[]</li>
	 * <li>当count小于0时,也能返回。如-1返回一个,-2返回两个</li>
	 * @param key
	 * @param count
	 * @return List<String>
	 */
	public static List<String> srandMember(String key, int count){
		Jedis jedis = jedisPool.getResource();
		List<String> values = jedis.srandmember(key, count);
		jedis.close();
		return values;
	}
	
	/**
	 * 移除集合中一个或多个成员
	 * @param key
	 * @param members
	 * @return
	 */
	public static boolean srem(String key, String... members){
		Jedis 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);
		jedis.close();
		if(value > 0){
			return true;
		}
		return false;
	}
	
	/**
	 * 返回所有给定集合的并集,相同的只会返回一个
	 * @param keys
	 * @return
	 */
	public static Set<String> sunion(String... keys){
		Jedis jedis = jedisPool.getResource();
		Set<String> values = jedis.sunion(keys);
		jedis.close();
		return values;
	}
	
	/**
	 * 所有给定集合的并集存储在targetKey集合中
	 * <li>注:合并时,只会把keys中的集合返回,不包括targetKey本身</li>
	 * <li>如果targetKey本身是有值的,合并后原来的值是没有的,因为把keys的集合重新赋值给targetKey</li>
	 * <li>要想保留targetKey本身的值,keys要包含原来的targetKey</li>
	 * @param targetKey
	 * @param keys
	 * @return
	 */
	public static boolean sunionStore(String targetKey, String... keys){
		Jedis jedis = jedisPool.getResource();
		//返回合并后的长度
		Long statusCode = jedis.sunionstore(targetKey, keys);
		System.out.println("statusCode="+statusCode);
		jedis.close();
		if(statusCode > 0){
			return true;
		}
		return false;
	}
	
	/**************************** redis 集合Set end***************************/

 

 

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

蕃薯耀 2016年9月27日 10:25:19 星期二

http://fanshuyao.iteye.com/

原文地址:https://www.cnblogs.com/fanshuyao/p/6227094.html