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/
Redis操作Set工具类封装,Java Redis Set命令封装的更多相关文章
- Redis操作List工具类封装,Java Redis List命令封装
Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...
- Redis操作Hash工具类封装,Redis工具类封装
Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...
- Redis操作字符串工具类封装,Redis工具类封装
Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...
- SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 后端篇(五): 数据表设计、使用 jwt、redis、sms 工具类完善注册登录逻辑
(1) 相关博文地址: SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 前端篇(一):搭建基本环境:https://www.cnblogs.com/l-y-h/p ...
- 自己封装的poi操作Excel工具类
自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...
- Redis,JedisPool工具类
Redis,JedisPool工具类 1.JedisPool 详细配置解释代码 2.Jedis工具类 导入相关依赖: commons-pool2-2.3.jar jedis-2.7.0.jar 1.J ...
- java中文件操作的工具类
代码: package com.lky.pojo; import java.io.BufferedReader; import java.io.BufferedWriter; import java. ...
- Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie
Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie >>>>>>>>>>>>& ...
- [Google Guava] 2.3-强大的集合工具类:java.util.Collections中未包含的集合工具
原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collections包含的工具方法.G ...
随机推荐
- BZOJ_1625_ [Usaco2007_Dec]_宝石手镯_(01背包)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1625 01背包裸题. p.s.随便点开一道就是水题... 分析 ... #include &l ...
- Lua查找字符串注意
问题: 使用Lua写Wireshark插件时,经常匹配字符串.今天使用string.find()函数查找字符串”max-age”,没有找到. 分析: local index = string.find ...
- 【原】 Spark中Worker源码分析(二)
继续前一篇的内容.前一篇内容为: Spark中Worker源码分析(一)http://www.cnblogs.com/yourarebest/p/5300202.html 4.receive方法, r ...
- hdu5593--ZYB's Tree(树形dp)
问题描述 ZYB有一颗N个节点的树,现在他希望你对于每一个点,求出离每个点距离不超过KK的点的个数. 两个点(x,y)在树上的距离定义为两个点树上最短路径经过的边数, 为了节约读入和输出的时间,我们采 ...
- Xamarin开发Android时Visual Studio 2012没有智能提示解决办法
Most of the people who work with Xamarin’s Mono for Android in Visual Studio 2012 face a bug where I ...
- PHP字符串操作汇总
PHP开发中常用的字符串操作介绍 -- 简明现代魔法 PHP学习笔记之字符串的简单处理 - RuanJava的专栏 - 博客频道 - CSDN.NET PHP String 函数
- PC-用Windows XP自带的组策略加固操作系统
1.我的壁纸你别改==================================== 启动组策略时,单击“开始”按钮,选择“运行”命令,在“运行”文本框中输入“gpedit.msc”命令,即可启 ...
- IE-首页跳转到 q160的问题解决
IE首页跳转到 q160的问题解决 服了又中找了,IE快捷方式被 www.q160.com劫持 该死的这个网站什么也没有做,就是做了一个google搜索的连接. 进行了一次搜索 http: ...
- javascript闭包详解
以前写过一篇关于javascript闭包的随笔,javascript闭包,但是写的不够详细,也没有体现出闭包的强大之处.故作此篇. 众所周知,javascript没有块级作用域,只有函数作用域.那就意 ...
- Oracle数据库字符串连接方法
转至:http://database.51cto.com/art/201011/232267.htm 和其他数据库系统类似,Oracle字符串连接使用“||”进行字符串拼接,其使用方式和MSSQLSe ...