- package com.component.jedis;
- import java.util.Set;
- import redis.clients.jedis.Jedis;
- public class JedisSet extends JedisCommon{
-
- private static final String OK_CODE = "OK";
- private static final String OK_MULTI_CODE = "+OK";
-
- /**
- * 把一个或多个元素添加到指定集合
- *
- * @param key
- * @param members
- * @return
- */
- public static Long sadd(String key, String members){
- Jedis jedis = JedisManager.getJedis();
- Long result ;
- try {
- result = jedis.sadd(key, members);
- }finally{
- JedisManager.returnResource(jedis);
- }
- return result;
- }
-
- /**
- * 返回多个集合的差集
- * @param keys
- * @return
- */
- public static Set<String> sdiff(String... keys){
- Jedis jedis = JedisManager.getJedis();
- Set<String> result ;
- try {
- result = jedis.sdiff(keys);
- }finally{
- JedisManager.returnResource(jedis);
- }
- return result;
- }
-
- /**
- * 返回多个集合的交集
- *
- * @param keys
- * @return
- */
- public static Set<String> sinter(String... keys){
- Jedis jedis = JedisManager.getJedis();
- Set<String> result ;
- try {
- result = jedis.sinter(keys);
- }finally{
- JedisManager.returnResource(jedis);
- }
- return result;
- }
-
- /**
- * 判断元素是否是集合成员
- *
- * @param key
- * @param member
- * @return
- */
- public static boolean sismember(String key, String member){
- Jedis jedis = JedisManager.getJedis();
- boolean result ;
- try {
- result = jedis.sismember(key, member);
- }finally{
- JedisManager.returnResource(jedis);
- }
- return result;
- }
-
- /**
- * 返回集合所有成员
- *
- * @param key
- * @return
- */
- public static Set<String> smembers(String key){
- Jedis jedis = JedisManager.getJedis();
- Set<String> result ;
- try {
- result = jedis.smembers(key);
- }finally{
- JedisManager.returnResource(jedis);
- }
- return result;
- }
-
- /**
- * 把指定成员从一个集合移动到目标集合,
- * 指定成员不存在,不执行任何操作
- * @param srckey
- * @param dstkey
- * @param member
- * @return
- */
- public static Long smove(String srckey, String dstkey, String member){
- Jedis jedis = JedisManager.getJedis();
- Long result ;
- try {
- result = jedis.smove(srckey, dstkey, member);
- }finally{
- JedisManager.returnResource(jedis);
- }
- return result;
- }
-
- /**
- * 随机移除并返回一个元素
- *
- * @param key
- * @return
- */
- public static String spop(String key){
- Jedis jedis = JedisManager.getJedis();
- String result ;
- try {
- result = jedis.spop(key);
- }finally{
- JedisManager.returnResource(jedis);
- }
- return result;
- }
-
- /**
- * 移除一个或多个元素,不存在的 元素会被忽略
- *
- * @param key
- * @param members
- * @return
- */
- public static Long srem(String key, String... members){
- Jedis jedis = JedisManager.getJedis();
- Long result ;
- try {
- result = jedis.srem(key, members);
- }finally{
- JedisManager.returnResource(jedis);
- }
- return result;
- }
-
- /**
- * 返回指定集合的并集
- *
- * @param keys
- * @return
- */
- public static Set<String> sunion(String... keys){
- Jedis jedis = JedisManager.getJedis();
- Set<String> result ;
- try {
- result = jedis.sunion(keys);
- }finally{
- JedisManager.returnResource(jedis);
- }
- return result;
- }
-
- /**
- * 当key不存在时,才放入值,超时时间单位为秒
- * @param jedis
- * @param key
- * @param value
- * @return
- */
- public static boolean set(String key, String value, Long timeOut){
-
- Jedis jedis = null;
- String resp = null;
- try{
- jedis = JedisManager.getJedis();
- resp = jedis.set(key, value, "NX", "EX", timeOut == null ? 1L : timeOut);
- } finally{
- JedisManager.returnResource(jedis);
- }
- return isStatusOk(resp);
- }
-
- /**
- * 判断 返回值是否ok.
- */
- public static boolean isStatusOk(String status) {
- return (status != null) && (OK_CODE.equals(status) || OK_MULTI_CODE.equals(status));
- }
- }
本文作者:zhaohui520013
本文来自云栖社区合作伙伴rediscn,了解相关信息可以关注redis.cn网站。