jedis操作redis中的set(封装)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36898043/article/details/82181793

一、由来

之前的redis pool的文章,讲解了如何封装一个redis pool以及操作redis中的字符串,博客地址如下:

https://blog.csdn.net/qq_36898043/article/details/82155202

还有一篇文章封装了一些 操作redis中的list 。。。。

https://blog.csdn.net/qq_36898043/article/details/82181654

关于 set相关操作命令可以参考runoob,连接地址如下:

http://www.runoob.com/redis/redis-commands.html

本文主要是封装一些 操作 redis中 set的相关方法。。。。。。

二、代码

package me.ele.redis;

import redis.clients.jedis.Jedis;

import java.util.Set;

/**
 * 操作redis中的set
 *
 * @author LZJ
 * @create 2018-08-29 12:45
 **/
public class SetRedisOperator {

    /**
     * 向set中 添加记录,如果已经存在,返回0 否则返回1
     * sadd 将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 value 元素将被忽略。
     *
     * @param redisPool
     * @param key
     * @param value
     * @return
     */
    public static long add(MyRedisPool redisPool, String key, String... value) {
        Jedis jedis = redisPool.borrowJedis();
        long status = jedis.sadd(key, value);
        redisPool.returnJedis(jedis);
        return status;
    }

    /**
     * 删除set中 指定的元素
     *
     * @param redisPool
     * @param key
     * @param member
     * @return
     */
    public static long delete(MyRedisPool redisPool, String key, String... member) {
        Jedis jedis = redisPool.borrowJedis();
        long returnStatus = jedis.srem(key, member);
        redisPool.returnJedis(jedis);
        return returnStatus;
    }

    /**
     * 返回set中 的元素数量
     * 如果set不存在,返回0
     *
     * @param redisPool
     * @param key
     * @return
     */
    public static long length(MyRedisPool redisPool, String key) {
        Jedis jedis = redisPool.borrowJedis();
        long length = jedis.scard(key);
        redisPool.returnJedis(jedis);
        return length;
    }

    /**
     * 判断元素是否存在
     *
     * @param redisPool
     * @param key
     * @param value
     * @return
     */
    public static boolean ifExist(MyRedisPool redisPool, String key, String value) {
        Jedis jedis = redisPool.borrowJedis();
        boolean result = jedis.sismember(key, value);
        redisPool.returnJedis(jedis);
        return result;
    }

    /**
     * 获取set 中的所有元素
     *
     * @param redisPool
     * @param key
     * @return
     */
    public static Set<String> getAllValues(MyRedisPool redisPool, String key) {
        Jedis jedis = redisPool.borrowJedis();
        Set<String> result = jedis.smembers(key);
        redisPool.returnJedis(jedis);
        return result;
    }


}

猜你喜欢

转载自blog.csdn.net/qq_36898043/article/details/82181793
今日推荐