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 ...
随机推荐
- -_-#【JS 优化】把 JS 放到底部
一开始为以为Javascript脚本尽量放到页面底部加载,是指所有的JS脚本都要放到底部,后来才发现, 并不完全是这样,这里所指的脚本是指那些在加载过程中要执行的脚本,所以一般的处理办法还是页面头部引 ...
- myeclipse安装svn插件的多种方式
开发者服务评测征文 十万现金悬赏大神 方法一:在线安装 1.打开HELP->MyEclipse Configuration Center.切换到SoftWare标签页. 2.点击Ad ...
- Yet Another Scheme Introduction学习
Chapter 2 Function exact->inexact is to convert from fractional numbers to floating point numbers ...
- FZU2224 An exciting GCD problem 区间gcd预处理+树状数组
分析:(别人写的) 对于所有(l, r)区间,固定右区间,所有(li, r)一共最多只会有log个不同的gcd值, 可以nlogn预处理出所有不同的gcd区间,这样区间是nlogn个,然后对于询问离线 ...
- DataProvider 传递参数
package roger.testng; import org.testng.annotations.DataProvider; import org.testng.annotations.Test ...
- strtok
1. Int main(void) { char *tmp = NULL; Char *remotebuf=”0\r\n”; tmp = strtok(remotebuf, DELIM); ...
- Android webView 中loadData方法加载 带中文时出现乱码
WebView出现乱码用LoadData方法来解析html的,但是据说这是官方的一个BUG,不能用来解析中文. 采用loadDataWithBaseURL的方法,其中codeingType设置为utf ...
- 把普通的git库变成bare库
$ cd your_repo $ mv .git .. && rm -fr * $ mv ../.git . $ mv .git/* . $ rmdir .git $ git conf ...
- 数据中心基础设施自动化运维之puppet专项
http://forge.puppetlabs.com/treydock/yum_cron [puppet功能扩展forge] http://docs.puppetlabs.com/referenc ...
- console.read()读入的内容
今天写的特别简单的代码,大体是一个模式选择,从控制台读入一个数,然后做出相应的选择. 代码如下: using System; using System.Collections.Generic; usi ...