精华内容
下载资源
问答
  • RedisUtil

    2019-08-20 19:08:03
    en …今天分享个珍藏多年的RedisUtil吧 /** * Redis工具类 * * @author ds */ public class RedisUtil { //默认缓存时间 private static final int EXPIRE = 60000; private static RedisUtil instance; ...

    en …今天分享个珍藏多年的RedisUtil吧

    /**
     * Redis工具类
     *
     * @author ds
     */
    public class RedisUtil {
    
        //默认缓存时间
        private static final int EXPIRE = 60000;
    
        private static RedisUtil instance;
    
        private static JedisPool jedisPool;
    
        private static ReentrantLock lock = new ReentrantLock();
    
        private RedisUtil() {
        }
    
        /**
         * 返回redis操作实例
         *
         */
        public static RedisUtil getInstance() {
            if (instance == null) {
                lock.lock();
                if (instance == null) {
                    instance = new RedisUtil();
                }
                lock.unlock();
            }
            return instance;
        }
    
    
        /**
         * 初始化JedisPool
         */
        private void initJedisPool() {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxIdle(Integer.valueOf(Util.getProperty("redis.pool.maxIdle")));
            jedisPool = new JedisPool(config, Util.getProperty("redis.ip"), Integer.valueOf(Util.getProperty("redis.port")));
        }
    
        /**
         * 通用方法:从JedisPool中获取Jedis
         *
    
         */
        private Jedis getJedis() {
            if (jedisPool == null) {
                lock.lock();    //防止吃初始化时多线程竞争问题
                initJedisPool();
                lock.unlock();
            }
            return jedisPool.getResource();
        }
    
        /**
         * 通用方法:释放Jedis
         *
         * @param jedis
         */
        private void closeJedis(Jedis jedis) {
            jedis.close();
        }
    
    //===========================================================
        /**
         * 对Keys,以及存储结构为String、List、Set、HashMap类型的操作
         */
        private final Keys keys = new Keys();
        private final Strings strings = new Strings();
        private final Lists lists = new Lists();
        private final Sets sets = new Sets();
        private final Hash hash = new Hash();
        private final SortSet sortset = new SortSet();
    
        public Keys keys() {
            return keys;
        }
    
        public Strings strings() {
            return strings;
        }
    
        public Lists lists() {
            return lists;
        }
    
        public Sets sets() {
            return sets;
        }
    
        public Hash hash() {
            return hash;
        }
    
        public SortSet sortSet() {
            return sortset;
        }
        //===========================================================
    
        //*******************************************Keys*******************************************//
        public class Keys {
    
            /**
             * 设置过期时间
             *
             * @return 返回影响的记录数
             */
            public long expire(String key, int seconds) {
                if (seconds <= 0) {
                    return -1L;
                }
                Jedis jedis = getJedis();
                long result = jedis.expire(key, seconds);
                closeJedis(jedis);
                return result;
            }
    
            /**
             * 设置过期时间,默认值为60000seconds
             *
             * @param key
             */
            public long expire(String key) {
                return expire(key, EXPIRE);
            }
    
            /**
             * 设置key的过期时间,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00,格里高利历)的偏移量。
             *
             * @param timestamp 秒
             * @return 影响的记录数
             */
            public long expireAt(String key, long timestamp) {
                Jedis jedis = getJedis();
                long count = jedis.expireAt(key, timestamp);
                closeJedis(jedis);
                return count;
            }
    
            /**
             * 查询key的过期时间
             *
             * @return 以秒为单位的时间表示
             */
            public long ttl(String key) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                long len = sjedis.ttl(key);
                closeJedis(sjedis);
                return len;
            }
    
            /**
             * 取消对key过期时间的设置
             *
             * @return 影响的记录数
             */
            public long persist(String key) {
                Jedis jedis = getJedis();
                long count = jedis.persist(key);
                closeJedis(jedis);
                return count;
            }
    
            /**
             * 清空所有key
             *
             * @return
             */
            public String flushAll() {
                Jedis jedis = getJedis();
                String stata = jedis.flushAll();
                closeJedis(jedis);
                return stata;
            }
    
            /**
             * 判断key是否存在
             *
             * @return boolean
             */
            public boolean exists(String key) {
                Jedis sjedis = getJedis();
                boolean exis = sjedis.exists(key);
                closeJedis(sjedis);
                return exis;
            }
    
            /**
             * 更改key
             */
            public String rename(String oldKey, String newKey) {
                return rename(SafeEncoder.encode(oldKey),
                        SafeEncoder.encode(newKey));
            }
    
            /**
             * 更改key,仅当新key不存在时才执行
             *
             * @return 状态码
             */
            public long renamenx(String oldKey, String newKey) {
                Jedis jedis = getJedis();
                long status = jedis.renamenx(oldKey, newKey);
                closeJedis(jedis);
                return status;
            }
    
            /**
             * 更改key
             */
            public String rename(byte[] oldKey, byte[] newKey) {
                Jedis jedis = getJedis();
                String status = jedis.rename(oldKey, newKey);
                closeJedis(jedis);
                return status;
            }
    
    
            /**
             * 删除keys对应的记录,可以是多个key
             *
             * @return 删除的记录数
             */
            public long del(String... keys) {
                Jedis jedis = getJedis();
                long count = jedis.del(keys);
                closeJedis(jedis);
                return count;
            }
    
            /**
             * 删除keys对应的记录,可以是多个key
             *
             * @return 删除的记录数
             */
            public long del(byte[]... keys) {
                Jedis jedis = getJedis();
                long count = jedis.del(keys);
                closeJedis(jedis);
                return count;
            }
    
    
            /**
             * 对List,Set,SortSet进行排序,如果集合数据较大应避免使用这个方法
             *
             * @return List<String> 集合的全部记录
             **/
            public List<String> sort(String key) {
                Jedis sjedis = getJedis();
                List<String> list = sjedis.sort(key);
                closeJedis(sjedis);
                return list;
            }
    
            /**
             * 对List,Set,SortSet进行排序或limit
             *
             * @param parame 定义排序类型或limit的起止位置.
             * @return List<String> 全部或部分记录
             **/
            public List<String> sort(String key, SortingParams parame) {
                Jedis jedis = getJedis();
                List<String> list = jedis.sort(key, parame);
                closeJedis(jedis);
                return list;
            }
    
            /**
             * 返回指定key存储的类型
             *
             * @return String string|list|set|zset|hash
             **/
            public String type(String key) {
                Jedis sjedis = getJedis();
                String type = sjedis.type(key);
                closeJedis(sjedis);
                return type;
            }
    
            /**
             * 查找所有匹配给定的模式的键
             *
             * @param pattern 的表达式,*表示多个,?表示一个
             */
            public Set<String> keys(String pattern) {
                Jedis jedis = getJedis();
                Set<String> set = jedis.keys(pattern);
                closeJedis(jedis);
                return set;
            }
        }
    
        //*******************************************Sets*******************************************//
        public class Sets {
    
            /**
             * 向Set添加一条记录,如果member已存在返回0,否则返回1
             *
             * @return 操作码, 0或1
             */
            public long sadd(String key, String member) {
                Jedis jedis = getJedis();
                long s = jedis.sadd(key, member);
                closeJedis(jedis);
                return s;
            }
    
            public long sadd(byte[] key, byte[] member) {
                Jedis jedis = getJedis();
                long s = jedis.sadd(key, member);
                closeJedis(jedis);
                return s;
            }
    
            /**
             * 获取给定key中元素个数
             *
             * @return 元素个数
             */
            public long scard(String key) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                long len = sjedis.scard(key);
                closeJedis(sjedis);
                return len;
            }
    
            /**
             * 返回从第一组和所有的给定集合之间的差异的成员
             *
             * @return 差异的成员集合
             */
            public Set<String> sdiff(String... keys) {
                Jedis jedis = getJedis();
                Set<String> set = jedis.sdiff(keys);
                closeJedis(jedis);
                return set;
            }
    
            /**
             * 这个命令等于sdiff,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。
             *
             * @param newKey 新结果集的key
             * @param keys   比较的集合
             * @return 新集合中的记录数
             **/
            public long sdiffstore(String newKey, String... keys) {
                Jedis jedis = getJedis();
                long s = jedis.sdiffstore(newKey, keys);
                closeJedis(jedis);
                return s;
            }
    
            /**
             * 返回给定集合交集的成员,如果其中一个集合为不存在或为空,则返回空Set
             *
             * @return 交集成员的集合
             **/
            public Set<String> sinter(String... keys) {
                Jedis jedis = getJedis();
                Set<String> set = jedis.sinter(keys);
                closeJedis(jedis);
                return set;
            }
    
            /**
             * 这个命令等于sinter,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。
             *
             * @param newKey 新结果集的key
             * @param keys   比较的集合
             * @return 新集合中的记录数
             **/
            public long sinterstore(String newKey, String... keys) {
                Jedis jedis = getJedis();
                long s = jedis.sinterstore(newKey, keys);
                closeJedis(jedis);
                return s;
            }
    
            /**
             * 确定一个给定的值是否存在
             *
             * @param key
             * @param member 要判断的值
             * @return 存在返回1,不存在返回0
             **/
            public boolean sismember(String key, String member) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                boolean s = sjedis.sismember(key, member);
                closeJedis(sjedis);
                return s;
            }
    
            /**
             * 返回集合中的所有成员
             *
             * @return 成员集合
             */
            public Set<String> smembers(String key) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                Set<String> set = sjedis.smembers(key);
                closeJedis(sjedis);
                return set;
            }
    
            public Set<byte[]> smembers(byte[] key) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                Set<byte[]> set = sjedis.smembers(key);
                closeJedis(sjedis);
                return set;
            }
    
            /**
             * 将成员从源集合移出放入目标集合 <br/>
             * 如果源集合不存在或不包哈指定成员,不进行任何操作,返回0<br/>
             * 否则该成员从源集合上删除,并添加到目标集合,如果目标集合中成员已存在,则只在源集合进行删除
             *
             * @param srckey 源集合
             * @param dstkey 目标集合
             * @param member 源集合中的成员
             * @return 状态码,1成功,0失败
             */
            public long smove(String srckey, String dstkey, String member) {
                Jedis jedis = getJedis();
                long s = jedis.smove(srckey, dstkey, member);
                closeJedis(jedis);
                return s;
            }
    
            /**
             * 从集合中删除成员
             *
             * @return 被删除的成员
             */
            public String spop(String key) {
                Jedis jedis = getJedis();
                String s = jedis.spop(key);
                closeJedis(jedis);
                return s;
            }
    
            /**
             * 从集合中删除指定成员
             *
             * @param member 要删除的成员
             * @return 状态码,成功返回1,成员不存在返回0
             */
            public long srem(String key, String member) {
                Jedis jedis = getJedis();
                long s = jedis.srem(key, member);
                closeJedis(jedis);
                return s;
            }
    
            /**
             * 合并多个集合并返回合并后的结果,合并后的结果集合并不保存<br/>
             *
             * @return 合并后的结果集合
             */
            public Set<String> sunion(String... keys) {
                Jedis jedis = getJedis();
                Set<String> set = jedis.sunion(keys);
                closeJedis(jedis);
                return set;
            }
    
            /**
             * 合并多个集合并将合并后的结果集保存在指定的新集合中,如果新集合已经存在则覆盖
             *
             * @param newKey 新集合的key
             * @param keys   要合并的集合
             **/
            public long sunionstore(String newKey, String... keys) {
                Jedis jedis = getJedis();
                long s = jedis.sunionstore(newKey, keys);
                closeJedis(jedis);
                return s;
            }
        }
    
        //*******************************************SortSet*******************************************//
        public class SortSet {
    
            /**
             * 向集合中增加一条记录,如果这个值已存在,这个值对应的权重将被置为新的权重
             *
             * @param score  权重
             * @param member 要加入的值,
             * @return 状态码 1成功,0已存在member的值
             */
            public long zadd(String key, double score, String member) {
                Jedis jedis = getJedis();
                long s = jedis.zadd(key, score, member);
                closeJedis(jedis);
                return s;
            }
    
            /**
             * 获取集合中元素的数量
             *
             * @return 如果返回0则集合不存在
             */
            public long zcard(String key) {
                Jedis sjedis = getJedis();
                long len = sjedis.zcard(key);
                closeJedis(sjedis);
                return len;
            }
    
            /**
             * 获取指定权重区间内集合的数量
             *
             * @param min 最小排序位置
             * @param max 最大排序位置
             */
            public long zcount(String key, double min, double max) {
                Jedis sjedis = getJedis();
                long len = sjedis.zcount(key, min, max);
                closeJedis(sjedis);
                return len;
            }
    
            /**
             * 获得set的长度
             *
             */
            public long zlength(String key) {
                long len = 0;
                Set<String> set = zrange(key, 0, -1);
                len = set.size();
                return len;
            }
    
            /**
             * 权重增加给定值,如果给定的member已存在
             *
             * @param score  要增的权重
             * @param member 要插入的值
             * @return 增后的权重
             */
            public double zincrby(String key, double score, String member) {
                Jedis jedis = getJedis();
                double s = jedis.zincrby(key, score, member);
                closeJedis(jedis);
                return s;
            }
    
            /**
             * 返回指定位置的集合元素,0为第一个元素,-1为最后一个元素
             *
             * @param start 开始位置(包含)
             * @param end   结束位置(包含)
             * @return Set<String>
             */
            public Set<String> zrange(String key, int start, int end) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                Set<String> set = sjedis.zrange(key, start, end);
                closeJedis(sjedis);
                return set;
            }
    
            /**
             * 返回指定权重区间的元素集合
             *
             * @param min 上限权重
             * @param max 下限权重
             * @return Set<String>
             */
            public Set<String> zrangeByScore(String key, double min, double max) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                Set<String> set = sjedis.zrangeByScore(key, min, max);
                closeJedis(sjedis);
                return set;
            }
    
            /**
             * 获取指定值在集合中的位置,集合排序从低到高
             *
             * @return long 位置
             */
            public long zrank(String key, String member) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                long index = sjedis.zrank(key, member);
                closeJedis(sjedis);
                return index;
            }
    
            /**
             * 获取指定值在集合中的位置,集合排序从高到低
             *
             * @return long 位置
             */
            public long zrevrank(String key, String member) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                long index = sjedis.zrevrank(key, member);
                closeJedis(sjedis);
                return index;
            }
    
            /**
             * 从集合中删除成员
             *
             * @return 返回1成功
             */
            public long zrem(String key, String member) {
                Jedis jedis = getJedis();
                long s = jedis.zrem(key, member);
                closeJedis(jedis);
                return s;
            }
    
            /**
             * 删除
             *
             */
            public long zrem(String key) {
                Jedis jedis = getJedis();
                long s = jedis.del(key);
                closeJedis(jedis);
                return s;
            }
    
            /**
             * 删除给定位置区间的元素
             *
             * @param start 开始区间,从0开始(包含)
             * @param end   结束区间,-1为最后一个元素(包含)
             * @return 删除的数量
             */
            public long zremrangeByRank(String key, int start, int end) {
                Jedis jedis = getJedis();
                long s = jedis.zremrangeByRank(key, start, end);
                closeJedis(jedis);
                return s;
            }
    
            /**
             * 删除给定权重区间的元素
             *
             * @param min 下限权重(包含)
             * @param max 上限权重(包含)
             * @return 删除的数量
             */
            public long zremrangeByScore(String key, double min, double max) {
                Jedis jedis = getJedis();
                long s = jedis.zremrangeByScore(key, min, max);
                closeJedis(jedis);
                return s;
            }
    
            /**
             * 获取给定区间的元素,原始按照权重由高到低排序
             *
             * @return Set<String>
             */
            public Set<String> zrevrange(String key, int start, int end) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                Set<String> set = sjedis.zrevrange(key, start, end);
                closeJedis(sjedis);
                return set;
            }
    
            /**
             * 获取给定值在集合中的权重
             *
             * @return double 权重
             */
            public double zscore(String key, String memebr) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                Double score = sjedis.zscore(key, memebr);
                closeJedis(sjedis);
                if (score != null)
                    return score;
                return 0;
            }
        }
    
        //*******************************************Hash*******************************************//
        public class Hash {
    
            /**
             * 从hash中删除指定的存储
             *
             * @param fieid 存储的名字
             * @return 状态码,1成功,0失败
             */
            public long hdel(String key, String fieid) {
                Jedis jedis = getJedis();
                long s = jedis.hdel(key, fieid);
                closeJedis(jedis);
                return s;
            }
    
            public long hdel(String key) {
                Jedis jedis = getJedis();
                long s = jedis.del(key);
                closeJedis(jedis);
                return s;
            }
    
            /**
             * 测试hash中指定的存储是否存在
             *
             * @param fieid 存储的名字
             * @return 1存在,0不存在
             */
            public boolean hexists(String key, String fieid) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                boolean s = sjedis.hexists(key, fieid);
                closeJedis(sjedis);
                return s;
            }
    
            /**
             * 返回hash中指定存储位置的值
             *
             * @param fieid 存储的名字
             * @return 存储对应的值
             */
            public String hget(String key, String fieid) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                String s = sjedis.hget(key, fieid);
                closeJedis(sjedis);
                return s;
            }
    
            public byte[] hget(byte[] key, byte[] fieid) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                byte[] s = sjedis.hget(key, fieid);
                closeJedis(sjedis);
                return s;
            }
    
            /**
             * 以Map的形式返回hash中的存储和值
             *
             * @return Map<Strinig                                                               ,                                                               String>
             */
            public Map<String, String> hgetAll(String key) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                Map<String, String> map = sjedis.hgetAll(key);
                closeJedis(sjedis);
                return map;
            }
    
            /**
             * 添加一个对应关系
             *
             * @return 状态码 1成功,0失败,fieid已存在将更新,也返回0
             **/
            public long hset(String key, String fieid, String value) {
                Jedis jedis = getJedis();
                long s = jedis.hset(key, fieid, value);
                closeJedis(jedis);
                return s;
            }
    
            public long hset(String key, String fieid, byte[] value) {
                Jedis jedis = getJedis();
                long s = jedis.hset(key.getBytes(), fieid.getBytes(), value);
                closeJedis(jedis);
                return s;
            }
    
            /**
             * 添加对应关系,只有在fieid不存在时才执行
             *
             * @return 状态码 1成功,0失败fieid已存
             **/
            public long hsetnx(String key, String fieid, String value) {
                Jedis jedis = getJedis();
                long s = jedis.hsetnx(key, fieid, value);
                closeJedis(jedis);
                return s;
            }
    
            /**
             * 获取hash中value的集合
             *
             * @return List<String>
             */
            public List<String> hvals(String key) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                List<String> list = sjedis.hvals(key);
                closeJedis(sjedis);
                return list;
            }
    
            /**
             * 在指定的存储位置加上指定的数字,存储位置的值必须可转为数字类型
             *
             * @param fieid 存储位置
             * @param value 要增加的值,可以是负数
             * @return 增加指定数字后,存储位置的值
             */
            public long hincrby(String key, String fieid, long value) {
                Jedis jedis = getJedis();
                long s = jedis.hincrBy(key, fieid, value);
                closeJedis(jedis);
                return s;
            }
    
            /**
             * 返回指定hash中的所有存储名字,类似Map中的keySet方法
             *
             * @return Set<String> 存储名称的集合
             */
            public Set<String> hkeys(String key) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                Set<String> set = sjedis.hkeys(key);
                closeJedis(sjedis);
                return set;
            }
    
            /**
             * 获取hash中存储的个数,类似Map中size方法
             *
             * @param key
             * @return long 存储的个数
             */
            public long hlen(String key) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                long len = sjedis.hlen(key);
                closeJedis(sjedis);
                return len;
            }
    
            /**
             * 根据多个key,获取对应的value,返回List,如果指定的key不存在,List对应位置为null
             *
             * @param key
             * @param fieids 存储位置
             * @return List<String>
             */
            public List<String> hmget(String key, String... fieids) {
                Jedis sjedis = getJedis();
                List<String> list = sjedis.hmget(key, fieids);
                closeJedis(sjedis);
                return list;
            }
    
            public List<byte[]> hmget(byte[] key, byte[]... fieids) {
                Jedis sjedis = getJedis();
                List<byte[]> list = sjedis.hmget(key, fieids);
                closeJedis(sjedis);
                return list;
            }
    
            /**
             * 添加对应关系,如果对应关系已存在,则覆盖
             *
             * @param key
             * @param map 对应关系
             * @return 状态,成功返回OK
             */
            public String hmset(String key, Map<String, String> map) {
                Jedis jedis = getJedis();
                String s = jedis.hmset(key, map);
                closeJedis(jedis);
                return s;
            }
    
            /**
             * 添加对应关系,如果对应关系已存在,则覆盖
             *
             * @param key
             * @param map 对应关系
             * @return 状态,成功返回OK
             */
            public String hmset(byte[] key, Map<byte[], byte[]> map) {
                Jedis jedis = getJedis();
                String s = jedis.hmset(key, map);
                closeJedis(jedis);
                return s;
            }
    
        }
    
    
        //*******************************************Strings*******************************************//
        public class Strings {
            /**
             * 根据key获取记录
             *
             * @param key
             * @return 值
             */
            public String get(String key) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                String value = sjedis.get(key);
                closeJedis(sjedis);
                return value;
            }
    
            /**
             * 根据key获取记录
             *
             * @param key
             * @return 值
             */
            public byte[] get(byte[] key) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                byte[] value = sjedis.get(key);
                closeJedis(sjedis);
                return value;
            }
    
            /**
             * 添加有过期时间的记录
             *
             * @param key
             * @param seconds 过期时间,以秒为单位
             * @param value
             * @return String 操作状态
             */
            public String setEx(String key, int seconds, String value) {
                Jedis jedis = getJedis();
                String str = jedis.setex(key, seconds, value);
                closeJedis(jedis);
                return str;
            }
    
            /**
             * 添加有过期时间的记录
             *
             * @param key
             * @param seconds 过期时间,以秒为单位
             * @param value
             * @return String 操作状态
             */
            public String setEx(byte[] key, int seconds, byte[] value) {
                Jedis jedis = getJedis();
                String str = jedis.setex(key, seconds, value);
                closeJedis(jedis);
                return str;
            }
    
            /**
             * 添加一条记录,仅当给定的key不存在时才插入
             *
             * @param key
             * @param value
             * @return long 状态码,1插入成功且key不存在,0未插入,key存在
             */
            public long setnx(String key, String value) {
                Jedis jedis = getJedis();
                long str = jedis.setnx(key, value);
                closeJedis(jedis);
                return str;
            }
    
            /**
             * 添加记录,如果记录已存在将覆盖原有的value
             *
             * @param key
             * @param value
             * @return 状态码
             */
            public String set(String key, String value) {
                return set(SafeEncoder.encode(key), SafeEncoder.encode(value));
            }
    
            /**
             * 添加记录,如果记录已存在将覆盖原有的value
             *
             * @param key
             * @param value
             * @return 状态码
             */
            public String set(String key, byte[] value) {
                return set(SafeEncoder.encode(key), value);
            }
    
            /**
             * 添加记录,如果记录已存在将覆盖原有的value
             *
             * @param key
             * @param value
             * @return 状态码
             */
            public String set(byte[] key, byte[] value) {
                Jedis jedis = getJedis();
                String status = jedis.set(key, value);
                closeJedis(jedis);
                return status;
            }
    
            /**
             * 从指定位置开始插入数据,插入的数据会覆盖指定位置以后的数据<br/>
             * 例:String str1="123456789";<br/>
             * 对str1操作后setRange(key,4,0000),str1="123400009";
             *
             * @param key
             * @param offset
             * @param value
             * @return long value的长度
             */
            public long setRange(String key, long offset, String value) {
                Jedis jedis = getJedis();
                long len = jedis.setrange(key, offset, value);
                closeJedis(jedis);
                return len;
            }
    
            /**
             * 在指定的key中追加value
             *
             * @param key
             * @param value
             * @return long 追加后value的长度
             **/
            public long append(String key, String value) {
                Jedis jedis = getJedis();
                long len = jedis.append(key, value);
                closeJedis(jedis);
                return len;
            }
    
            /**
             * 将key对应的value减去指定的值,只有value可以转为数字时该方法才可用
             *
             * @param key
             * @param number 要减去的值
             * @return long 减指定值后的值
             */
            public long decrBy(String key, long number) {
                Jedis jedis = getJedis();
                long len = jedis.decrBy(key, number);
                closeJedis(jedis);
                return len;
            }
    
            /**
             * <b>可以作为获取唯一id的方法</b><br/>
             * 将key对应的value加上指定的值,只有value可以转为数字时该方法才可用
             *
             * @param key
             * @param number 要减去的值
             * @return long 相加后的值
             */
            public long incrBy(String key, long number) {
                Jedis jedis = getJedis();
                long len = jedis.incrBy(key, number);
                closeJedis(jedis);
                return len;
            }
    
            /**
             * 对指定key对应的value进行截取
             *
             * @param key
             * @param startOffset 开始位置(包含)
             * @param endOffset   结束位置(包含)
             * @return String 截取的值
             */
            public String getrange(String key, long startOffset, long endOffset) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                String value = sjedis.getrange(key, startOffset, endOffset);
                closeJedis(sjedis);
                return value;
            }
    
            /**
             * 获取并设置指定key对应的value<br/>
             * 如果key存在返回之前的value,否则返回null
             *
             * @param key
             * @param value
             * @return String 原始value或null
             */
            public String getSet(String key, String value) {
                Jedis jedis = getJedis();
                String str = jedis.getSet(key, value);
                closeJedis(jedis);
                return str;
            }
    
            /**
             * 批量获取记录,如果指定的key不存在返回List的对应位置将是null
             *
             * @param keys
             * @return List<String> 值得集合
             */
            public List<String> mget(String... keys) {
                Jedis jedis = getJedis();
                List<String> str = jedis.mget(keys);
                closeJedis(jedis);
                return str;
            }
    
            /**
             * 批量存储记录
             *
             * @param keysvalues 例:keysvalues="key1","value1","key2","value2";
             * @return String 状态码
             */
            public String mset(String... keysvalues) {
                Jedis jedis = getJedis();
                String str = jedis.mset(keysvalues);
                closeJedis(jedis);
                return str;
            }
    
            /**
             * 获取key对应的值的长度
             *
             * @param key
             * @return value值得长度
             */
            public long strlen(String key) {
                Jedis jedis = getJedis();
                long len = jedis.strlen(key);
                closeJedis(jedis);
                return len;
            }
        }
    
    
        //*******************************************Lists*******************************************//
        public class Lists {
            /**
             * List长度
             *
             * @param key
             * @return 长度
             */
            public long llen(String key) {
                return llen(SafeEncoder.encode(key));
            }
    
            /**
             * List长度
             *
             * @param key
             * @return 长度
             */
            public long llen(byte[] key) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                long count = sjedis.llen(key);
                closeJedis(sjedis);
                return count;
            }
    
            /**
             * 覆盖操作,将覆盖List中指定位置的值
             *
             * @param key
             * @param index 位置
             * @param value 值
             * @return 状态码
             */
            public String lset(byte[] key, int index, byte[] value) {
                Jedis jedis = getJedis();
                String status = jedis.lset(key, index, value);
                closeJedis(jedis);
                return status;
            }
    
            /**
             * 覆盖操作,将覆盖List中指定位置的值
             *
             * @param
             * @param index 位置
             * @param value 值
             * @return 状态码
             */
            public String lset(String key, int index, String value) {
                return lset(SafeEncoder.encode(key), index,
                        SafeEncoder.encode(value));
            }
    
            /**
             * 获取List中指定位置的值
             *
             * @param key
             * @param index 位置
             * @return 值
             **/
            public String lindex(String key, int index) {
                return SafeEncoder.encode(lindex(SafeEncoder.encode(key), index));
            }
    
            /**
             * 获取List中指定位置的值
             *
             * @param key
             * @param index 位置
             * @return 值
             **/
            public byte[] lindex(byte[] key, int index) {
                Jedis sjedis = getJedis();
                byte[] value = sjedis.lindex(key, index);
                closeJedis(sjedis);
                return value;
            }
    
            /**
             * 将List中的第一条记录移出List
             *
             * @param key
             * @return 移出的记录
             */
            public String lpop(String key) {
                return SafeEncoder.encode(lpop(SafeEncoder.encode(key)));
            }
    
            /**
             * 将List中的第一条记录移出List
             *
             * @param key
             * @return 移出的记录
             */
            public byte[] lpop(byte[] key) {
                Jedis jedis = getJedis();
                byte[] value = jedis.lpop(key);
                closeJedis(jedis);
                return value;
            }
    
            /**
             * 将List中最后第一条记录移出List
             *
             * @param key
             * @return 移出的记录
             */
            public String rpop(String key) {
                Jedis jedis = getJedis();
                String value = jedis.rpop(key);
                closeJedis(jedis);
                return value;
            }
    
            /**
             * 向List尾部追加记录
             *
             * @param key
             * @param value
             * @return 记录总数
             */
            public long lpush(String key, String value) {
                return lpush(SafeEncoder.encode(key), SafeEncoder.encode(value));
            }
    
            /**
             * 向List头部追加记录
             *
             * @param key
             * @param value
             * @return 记录总数
             */
            public long rpush(String key, String value) {
                Jedis jedis = getJedis();
                long count = jedis.rpush(key, value);
                closeJedis(jedis);
                return count;
            }
    
            /**
             * 向List头部追加记录
             *
             * @param key
             * @param value
             * @return 记录总数
             */
            public long rpush(byte[] key, byte[] value) {
                Jedis jedis = getJedis();
                long count = jedis.rpush(key, value);
                closeJedis(jedis);
                return count;
            }
    
            /**
             * 向List中追加记录
             *
             * @param key
             * @param value
             * @return 记录总数
             */
            public long lpush(byte[] key, byte[] value) {
                Jedis jedis = getJedis();
                long count = jedis.lpush(key, value);
                closeJedis(jedis);
                return count;
            }
    
            /**
             * 获取指定范围的记录,可以做为分页使用
             *
             * @param key
             * @param start
             * @param end
             * @return List
             */
            public List<String> lrange(String key, long start, long end) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                List<String> list = sjedis.lrange(key, start, end);
                closeJedis(sjedis);
                return list;
            }
    
            /**
             * 获取指定范围的记录,可以做为分页使用
             *
             * @param key
             * @param start
             * @param end   如果为负数,则尾部开始计算
             * @return List
             */
            public List<byte[]> lrange(byte[] key, int start, int end) {
                //ShardedJedis sjedis = getShardedJedis();
                Jedis sjedis = getJedis();
                List<byte[]> list = sjedis.lrange(key, start, end);
                closeJedis(sjedis);
                return list;
            }
    
            /**
             * 删除List中c条记录,被删除的记录值为value
             *
             * @param key
             * @param c     要删除的数量,如果为负数则从List的尾部检查并删除符合的记录
             * @param value 要匹配的值
             * @return 删除后的List中的记录数
             */
            public long lrem(byte[] key, int c, byte[] value) {
                Jedis jedis = getJedis();
                long count = jedis.lrem(key, c, value);
                closeJedis(jedis);
                return count;
            }
    
            /**
             * 删除List中c条记录,被删除的记录值为value
             *
             * @param key
             * @param c     要删除的数量,如果为负数则从List的尾部检查并删除符合的记录
             * @param value 要匹配的值
             * @return 删除后的List中的记录数
             */
            public long lrem(String key, int c, String value) {
                return lrem(SafeEncoder.encode(key), c, SafeEncoder.encode(value));
            }
    
            /**
             * 算是删除吧,只保留start与end之间的记录
             *
             * @param key
             * @param start 记录的开始位置(0表示第一条记录)
             * @param end   记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)
             * @return 执行状态码
             */
            public String ltrim(byte[] key, int start, int end) {
                Jedis jedis = getJedis();
                String str = jedis.ltrim(key, start, end);
                closeJedis(jedis);
                return str;
            }
    
            /**
             * 算是删除吧,只保留start与end之间的记录
             *
             * @param key
             * @param start 记录的开始位置(0表示第一条记录)
             * @param end   记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)
             * @return 执行状态码
             */
            public String ltrim(String key, int start, int end) {
                return ltrim(SafeEncoder.encode(key), start, end);
            }
        }
    
    }
    

    注意

    	/**
         * 初始化JedisPool
         */
        private void initJedisPool() {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxIdle(Integer.valueOf(Util.getProperty("redis.pool.maxIdle")));
            jedisPool = new JedisPool(config, Util.getProperty("redis.ip"), Integer.valueOf(Util.getProperty("redis.port")));
        }
    

    其中getProperTy(“redis.pool.maxIdle”)方法如下

        /**
         * @param key 键
         * @return 值
         */
        public static String getProperty(String key) {
            Properties resources = getResources("config.properties");
    //        Properties resources = getResources("config_dev.properties");
            return resources.getProperty(key);
        }
    

    config.properties如下
    项目配置文件
    调用

    @Test
    public void fun(){
    	RedisUtil instance = RedisUtil.getInstance();
    	instance.strings().set("key", "adsddfsf");
        instance.keys().expire(token, 1800 * 2);
    }
    
    展开全文
  • redisUtil

    2020-03-03 15:53:10
    public class RedisUtil { @Autowired private StringRedisTemplate stringRedisTemplate; /** * 获取指定key的值 * * @param key * @return * @author albert.ding */ public String get(String key) {...

    @Component
    public class RedisUtil {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    
    /**
     * 获取指定key的值
     *
     * @param key
     * @return
     * @author albert.ding
     */
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }
    
    /**
     * 模糊匹配获取key
     *
     * @param key
     * @return
     * @author nan_wu
     */
    public Set<String> getKeys(String key) {
        return stringRedisTemplate.keys("*" + key + "*");
    }
    
    /**
     * 设置指定key的值
     *
     * @param key
     * @param value
     * @author albert.ding
     */
    public void set(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }
    
    /**
     * 将值value关联到key,并将key的过期时间设为seconds(以秒为单位)
     *
     * @param key
     * @param value
     * @param timeout (seconds)
     * @author albert.ding
     */
    public void setex(String key, String value, int timeout) {
        stringRedisTemplate.opsForValue().set(key, value);
        stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);
    }
    
    /**
     * 检查给定key是否存在
     *
     * @param key
     * @return
     * @author albert.ding
     */
    public boolean exists(String key) {
        return stringRedisTemplate.hasKey(key);
    }
    
    /**
     * key存在时删除key
     *
     * @param key
     * @author albert.ding
     */
    public void del(String key) {
        stringRedisTemplate.delete(key);
    }
    
    /**
     * 获取存储在哈希表中指定字段的值
     *
     * @param key
     * @param field
     * @return
     * @author albert.ding
     */
    public String hget(String key, String field) {
        return (String) stringRedisTemplate.opsForHash().get(key, field);
    }
    
    /**
     * 将哈希表key中的字段field的值设为value
     *
     * @param key
     * @param field
     * @param value
     * @author albert.ding
     */
    public void hset(String key, String field, String value) {
        stringRedisTemplate.opsForHash().put(key, field, value);
    }
    
    /**
     * 获取在哈希表中指定key的所有字段和值
     *
     * @param key
     * @return
     * @author albert.ding
     */
    public Map<String, String> hgetall(String key) {
        return stringRedisTemplate.<String, String>opsForHash().entries(key);
    }
    
    /**
     * 获取哈希表中所有值
     *
     * @param key
     * @return
     * @author albert.ding
     */
    public List<String> hvals(String key) {
        return stringRedisTemplate.<String, String>opsForHash().values(key);
    }
    
    /**
     * 获取所有哈希表中的字段
     *
     * @param key
     * @return
     * @author albert.ding
     */
    public Set<String> hkeys(String key) {
        return stringRedisTemplate.<String, String>opsForHash().keys(key);
    }
    
    /**
     * 获取哈希表中字段的数量
     *
     * @param key
     * @return
     * @author albert.ding
     */
    public Long hlen(String key) {
        return stringRedisTemplate.opsForHash().size(key);
    }
    
    /**
     * 查看哈希表key中,指定的字段是否存在
     *
     * @param key
     * @return
     * @author albert.ding
     */
    public boolean hexists(String key, String field) {
        return stringRedisTemplate.opsForHash().hasKey(key, field);
    }
    
    /**
     * 删除一个哈希表字段
     *
     * @param key
     * @author albert.ding
     */
    public void hdel(String key, String field) {
        stringRedisTemplate.opsForHash().delete(key, field);
    }
    
    /**
     * 向list右边添加元素值
     *
     * @param key
     * @param val
     * @author nan_wu
     */
    public void lSet(String key, String val) {
        stringRedisTemplate.opsForList().rightPush(key, val);
    }
    
    /**
     * 获取list指定区间的值
     *
     * @param key
     * @author nan_wu
     */
    public List<String> lGet(String key, long start, long end) {
        return stringRedisTemplate.opsForList().range(key, start, end);
    }
    
    /**
     * 删除list中元素
     *
     * @param key
     * @param l
     * @param value
     * @author nan_wu
     */
    public void lRemove(String key, long l, Object value) {
        stringRedisTemplate.opsForList().remove(key, l, value);
    }
    
    /**
     * 向set集合添加元素
     *
     * @param key
     * @param value
     * @author nan_wu
     */
    public void sAdd(String key, String... value) {
        stringRedisTemplate.opsForSet().add(key, value);
    }
    
    /**
     * 根据key获取set集合
     *
     * @param key
     * @return
     * @author nan_wu
     */
    public Set<String> sGet(String key) {
        return stringRedisTemplate.opsForSet().members(key);
    }
    
    /**
     * 根据key查看set集合中是否存在指定数据
     *
     * @param key
     * @param val
     * @return
     * @author nan_wu
     */
    public Boolean sIsMember(String key, String val) {
        return stringRedisTemplate.opsForSet().isMember(key, val);
    }
    
    /**
     * 根据key删除set集合指定的value
     *
     * @param key
     * @param val
     * @author nan_wu
     */
    public void sDel(String key, Object... val) {
        stringRedisTemplate.opsForSet().remove(key, val);
    }
    
    /**
     * 根据key获取set集合长度
     *
     * @param key
     * @author nan_wu
     */
    public Long sLen(String key) {
        return stringRedisTemplate.opsForSet().size(key);
    }
    

    }

    展开全文
  • redisutil

    2020-07-17 19:40:17
    import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger;... EsMember es2 = JSONObject.parseObject(redisUtil.getCacheObject("b").toString(),EsMember.class); System.out.println(es2); return "hello"; }
    package com.lsh.product.config.redis;
    
    import lombok.extern.slf4j.Slf4j;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    /**
     * Description :〈jedis配置类〉
     *
     * @author white
     * @create 2020/6/18
     * @since 1.0.0
     */
    @Slf4j
    @Configuration
    public class JedisPoolFactory {
        private Logger logger =  LoggerFactory.getLogger(JedisPoolFactory.class);
    
        @Value("${spring.redis.host}")
        private String host;
    
        @Value("${spring.redis.port}")
        private int port;
    
        @Value("${spring.redis.password}")
        private String password;
    
        @Value("${spring.redis.timeout}")
        private int timeout;
    
        @Value("${spring.redis.jedis.pool.max-active}")
        private int maxActive;
    
        @Value("${spring.redis.jedis.pool.max-idle}")
        private int maxIdle;
    
        @Value("${spring.redis.jedis.pool.min-idle}")
        private int minIdle;
    
        @Value("${spring.redis.jedis.pool.max-wait}")
        private long maxWaitMillis;
    
        @Bean
        public JedisPool generateJedisPoolFactory() {
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            poolConfig.setMaxTotal(maxActive);
            poolConfig.setMaxIdle(maxIdle);
            poolConfig.setMinIdle(minIdle);
            poolConfig.setMaxWaitMillis(maxWaitMillis);
            JedisPool jedisPool = new JedisPool(poolConfig, host, port, timeout, password);
    
            logger.info("redis地址:" + host + ":" + port);
            return jedisPool;
        }
    
    
        /**
         * 重写Redis序列化方式,使用Json方式:
         * 当我们的数据存储到Redis的时候,我们的键(key)和值(value)都是通过Spring提供的Serializer序列化到数据库的。RedisTemplate默认使用的是JdkSerializationRedisSerializer,StringRedisTemplate默认使用的是StringRedisSerializer。
         * Spring Data JPA为我们提供了下面的Serializer:
         * GenericToStringSerializer、Jackson2JsonRedisSerializer、JacksonJsonRedisSerializer、JdkSerializationRedisSerializer、OxmSerializer、StringRedisSerializer。
         * 在此我们将自己配置RedisTemplate并定义Serializer。
         *
         * @param redisConnectionFactory
         * @return
         */
        @Bean
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
            redisTemplate.setConnectionFactory(redisConnectionFactory);
    //        JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
            GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
            // 设置值(value)的序列化采用FastJsonRedisSerializer。
            redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
    //        redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
            // 设置键(key)的序列化采用StringRedisSerializer。
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.afterPropertiesSet();
            return redisTemplate;
        }
    
    }
    
    

     

     

     

     

    package com.lsh.product.utils;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.BoundSetOperations;
    import org.springframework.data.redis.core.ListOperations;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.ValueOperations;
    import org.springframework.data.redis.core.script.DefaultRedisScript;
    import org.springframework.stereotype.Component;
    
    import java.time.Duration;
    import java.util.*;
    import java.util.concurrent.TimeUnit;
    
    @Component
    public class RedisUtil {
    
        @Autowired
        private RedisTemplate redisTemplate;
    
        /**
         * 在字符串后边追加 字符串
         * @param key
         * @param appendValue
         * @return
         */
        public <T> ValueOperations<String, T> append(String key, String appendValue) {
            ValueOperations<String, T> operations = this.redisTemplate.opsForValue();
            operations.append(key, appendValue);
            return operations;
        }
    
        /**
         * 缓存基本的对象,Integer、String、实体类等
         * @param key      缓存的健值
         * @param value    缓存的值
         * @return         缓存的对象
         */
        public <T> ValueOperations<String, T> setCacheObject(String key, T value) {
            ValueOperations<String, T> operations = this.redisTemplate.opsForValue();
            operations.set(key, value);
            return operations;
        }
    
        /**
         * 缓存基本的对象,Integer、String、实体类等
         * @param key      缓存的健值
         * @param value    缓存的值
         * @param timeout  时间
         * @param timeUnit 时间颗粒度
         * @return         缓存的对象
         */
        public <T> ValueOperations<String, T> setCacheObject(String key, T value, Integer timeout, TimeUnit timeUnit) {
            ValueOperations<String, T> operations = this.redisTemplate.opsForValue();
            operations.set(key, value, timeout, timeUnit);
            return operations;
        }
    
        /**
         * 缓存基本的对象,Integer、String、实体类等
         * @param key      缓存的健值
         * @param value    缓存的值
         * @param timeout  时间
         * @return         缓存的对象
         */
        public <T> ValueOperations<String, T> setCacheObject(String key, T value, Duration timeout) {
            ValueOperations<String, T> operations = this.redisTemplate.opsForValue();
            operations.set(key, value, timeout);
            return operations;
        }
    
        /**
         * 获得缓存的基本对象。
         *
         * @param key 缓存键值
         * @return 缓存键值对应的数据
         */
        public <T> T getCacheObject(String key) {
            ValueOperations<String, T> operation = this.redisTemplate.opsForValue();
            return operation.get(key);
        }
    
        /**
         * 设置过期时间
         * @param key           key值
         * @param timeout       时间
         * @param timeUnit      时间单位
         * @return
         */
        public Boolean setExpire(String key, Long timeout, TimeUnit timeUnit) {
            return this.redisTemplate.expire(key, timeout, timeUnit);
        }
    
        /**
         * 删除单个对象
         * @param key
         */
        public void deleteObject(String key) {
            this.redisTemplate.delete(key);
        }
    
        /**
         * 删除集合对象
         * @param collection
         */
        public void deleteObject(Collection collection) {
            this.redisTemplate.delete(collection);
        }
    
        /**
         * 缓存List数据
         *
         * @param key      缓存的键值
         * @param dataList 待缓存的List数据
         * @return         缓存的对象
         */
        public <T> ListOperations<String, T> setCacheList(String key, List<T> dataList) {
            ListOperations listOperations = this.redisTemplate.opsForList();
            if (null != listOperations) {
                int size = dataList.size();
                for (int i = 0; i < size; i++) {
                    listOperations.leftPush(key, dataList.get(i));
                }
            }
            return listOperations;
        }
    
        /**
         * 获得缓存的list对象
         *
         * @param key 缓存的键值
         * @return 缓存键值对应的数据
         */
        public <T> List<T> getCacheList(String key) {
            List<T> dataList = new ArrayList<>();
            ListOperations<String, T> listOperations = this.redisTemplate.opsForList();
            Long size = listOperations.size(key);
            for (long i = 0; i < size; i++) {
                dataList.add(listOperations.index(key, i));
            }
            return dataList;
        }
    
        /**
         * 缓存Set
         *
         * @param key     缓存键值
         * @param dataSet 缓存的数据
         * @return 缓存数据的对象
         */
        public <T> BoundSetOperations<String, T> setCacheSet(String key, Set<T> dataSet) {
            BoundSetOperations<String, T> setOperations = this.redisTemplate.boundSetOps(key);
            Iterator<T> iterable = dataSet.iterator();
            while (iterable.hasNext()) {
                setOperations.add(iterable.next());
            }
            return setOperations;
        }
    
        /**
         * 获得缓存的set
         *
         * @param key
         * @return
         */
        public <T> Set<T> getCacheSet(String key) {
            Set<T> dataSet = new HashSet<>();
            BoundSetOperations<String, T> setOperations = this.redisTemplate.boundSetOps(key);
            Long size = setOperations.size();
            for (long i = 0; i < size; i++) {
                dataSet.add(setOperations.pop());
            }
            return dataSet;
        }
    
        /**
         * 缓存Map
         *
         * @param key
         * @param dataMap
         * @return
         */
        public void hashPutAll(String key, Map<String, Object> dataMap) {
            this.redisTemplate.opsForHash().putAll(key, dataMap);
        }
    
        /**
         * 在hash中新增一个值
         * @param key
         * @param field
         * @param value
         */
        public void hashPut(String key, String field, Object value) {
            this.redisTemplate.opsForHash().put(key, field, value);
        }
    
        /**
         * 当field不存在时put
         * @param key
         * @param field
         * @param value
         */
        public void hashPutIfAbsent(String key, String field, Object value) {
            this.redisTemplate.opsForHash().putIfAbsent(key, field, value);
        }
    
        /**
         * 获得缓存的Map
         *
         * @param key
         */
        public Map<String, Object> hashGetAll(String key) {
            return this.redisTemplate.opsForHash().entries(key);
        }
    
        /**
         * 获取键值为key的hash中的field字段的值
         * @param key
         * @param field
         */
        public Object hashGet(String key, String field) {
            return this.redisTemplate.opsForHash().get(key, field);
        }
    
        /**
         * 获取键值为key的hash表中所有字段
         * @param key
         */
        public Set<String> hashKeys(String key) {
            return this.redisTemplate.opsForHash().keys(key);
        }
    
        /**
         * 获取键值为key的hash表中所有value
         * @param key
         */
        public List<Object> hashValues(String key) {
            return this.redisTemplate.opsForHash().values(key);
        }
    
        /**
         * 给hash表中指定字段(整形)增加increment
         */
        public Long hashIncrement(String key, String field, long increment) {
            return this.redisTemplate.opsForHash().increment(key, field, increment);
        }
    
        /**
         * 给hash表中指定字段(Double)增加increment
         */
        public Double hashIncrement(String key, String field, double increment) {
            return this.redisTemplate.opsForHash().increment(key, field, increment);
        }
    
        /**
         * 判断hashKey是否存在
         * @param key
         * @param hashKey
         * @return 存在返回true,不存在返回false
         */
        public Boolean hasKey(String key, String hashKey) {
            return this.redisTemplate.opsForHash().hasKey(key, hashKey);
        }
    
        /**
         * 根据key删除一个或多个字段
         * @param key
         * @param fields
         */
        public void hashDelete(String key, String... fields) {
            this.redisTemplate.opsForHash().delete(key, fields);
        }
    
        /**
         * 获得缓存的基本对象列表
         *
         * @param pattern 字符串前缀
         * @return 对象列表
         */
        public Collection<String> keys(String pattern) {
            return this.redisTemplate.keys(pattern);
        }
    
        /**
         * 自增
         * @param key
         * @return     自增后的值
         */
        public Long increment(String key) {
            return this.redisTemplate.opsForValue().increment(key);
        }
    
        /**
         * 自增 num
         * @param key
         * @return   自增后的值
         */
        public Long increment(String key, long num) {
            return this.redisTemplate.opsForValue().increment(key, num);
        }
    
        /**
         * 返回RedisTemplate
         *
         * @return RedisTemplate
         */
        public RedisTemplate getRedisTemplate() {
            return this.redisTemplate;
        }
    
        /**
         * 执行lua脚本,返回执行结果
         * @param redisScript
         * @param key
         * @param value
         * @return
         */
        public Object execute(DefaultRedisScript redisScript, String key, Object value) {
            return this.redisTemplate.execute(redisScript, Arrays.asList(key), value);
        }
    
    }
    

     

     

    使用

        public String hello(){
      
           // stringRedisTemplate.opsForValue().set("b","cccccc");
            EsMember es = new EsMember();
            es.setId(1);
            es.setEmail("918556@qq.com");
            redisUtil.setCacheObject("b",JSONObject.toJSONString(es));
            System.out.println("------"+redisUtil.getCacheObject("b").toString());
            EsMember es2  = JSONObject.parseObject(redisUtil.getCacheObject("b").toString(),EsMember.class);
            System.out.println(es2);
            return "hello";
        }

     

    展开全文
  • RedisUtil工具类

    2019-01-04 08:57:10
    RedisUtil .
  • RedisUtil.rar

    2021-01-10 01:12:23
    RedisUtil redis的工具类
  • RedisUtil.java

    2020-06-25 16:04:17
    RedisUtil工具类直接使用, 减少不必要的重复代码 达到快速开发
  • redisUtil工具

    2020-07-17 11:14:55
    public class RedisUtil { private final StringRedisTemplate stringRedisTemplate; public RedisUtil(StringRedisTemplate stringRedisTemplate) { this.stringRedisTemplate = stringRedisTemplate; } //...

    @Component
    @Slf4j
    public class RedisUtil {

        private final StringRedisTemplate stringRedisTemplate;

        public RedisUtil(StringRedisTemplate stringRedisTemplate) {
            this.stringRedisTemplate = stringRedisTemplate;
        }

        //判断缓存是否存在
        public Boolean hasCache(String key) {
            return stringRedisTemplate.hasKey(key);
        }

        //设置缓存
        public void setCache(String key, String value, Long timeout, TimeUnit timeoutType) {
            stringRedisTemplate.opsForValue().set(key, value, timeout, timeoutType);
        }
        //获取缓存
        public String getCache(String key) {
            return stringRedisTemplate.opsForValue().get(key);
        }

    }

     

    展开全文

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 710
精华内容 284
关键字:

redisutil

redis 订阅