• 周蓬安.blog的博客—强国博客—人民网 2019-05-10
  • 紫光阁中共中央国家机关工作委员会 2019-05-10
  • 感触名家笔下的端午文化吃香粽原来可以这样文艺 2019-05-09
  • 追梦夺冠游行嘲讽詹皇 百万人面前穿订制T恤羞辱他 2019-04-27
  • 《瘟疫传说》:黑死病恐怖 姐弟在绝望中求生 2019-04-10
  • 陕西国防工业职业技术学院百名大学生志愿者敬老院慰问孤寡老人陕西国防工业职业技术学院百名大学生志愿者敬老院慰问-陕西教育新闻 2019-04-08
  • 西藏拉萨:新家园 新生活 2019-04-08
  • 尊重和保障宗教信仰自由的中国实践 2019-04-06
  • 一敬泯恩仇 俄罗斯队主帅这个动作太暖了 2019-03-20
  • 四大名著剧组首次同台忆往事 经典影视剧如何铸就? 2018-12-07
  • “天眼”凝望 探秘宇宙 2018-12-07
  • 1

    Redis基础

    Posted by 撒得一地 on 2016年4月22日 in 杂谈
    上一篇:
    下一篇:
    国外稳定加速器推荐    Express | Vypr

    凡是用memcached的场景,麻烦都用Redis代替

    Redis String/List/Set/Sorted-Set/Hash/

    • Redis相比较memcached,能支持更多的数据结构,比如List,Set等
    • Redis的Key在命名的时候,用:分割
    • Redis同样支持二进制的存储Object byte[]
    • Redis有一个Jedis的Java Client,其中Jedis Pool是类似于c3p0的东西,使用了commons-pool
    • Jedis支持pipeline的操作,一般对多个Key操作,会使用pipeline

    一个Java Redis客户端,基于Jedis

    public class MyRedisClient {
        private static Logger logger = LoggerFactory.getLogger(MyRedisClient.class);
        private static final String CAT_NAME = "RedisCall";
    
        @Resource
        private JedisPool jedisPool;
    
        //************************** String ********************************//
        public List<Long> incr(final String... keys) {
            List<Long> emptyResult = new ArrayList<Long>();
            if (keys == null || keys.length == 0) {
                return emptyResult;
            }
    
            try {
                List<Long> results = JedisHelper.doJedisOperation(new JedisCallback<List<Long>>() {
                    @Override
                    public List<Long> doWithJedis(Jedis jedis) {
                        Pipeline pipelined = jedis.pipelined();
                        for (String key : keys) {
                            pipelined.incr(key);
                        }
                        List<Object> resultObjects = pipelined.syncAndReturnAll();
                        List<Long> resultList = new ArrayList<Long>();
                        for (Object resultObject : resultObjects) {
                            try {
                                resultList.add(Long.valueOf(resultObject.toString()));
                            } catch (Exception e) {
                                resultList.add(0L);
                                logger.error("redis incr result:convert object to long error", e);
                            }
                        }
                        return resultList;
                    }
                }, this.jedisPool);
                return results;
            } catch (Exception e) {
                logger.error("call incr error : ", e);
            }
            return emptyResult;
        }
    
        public List<Long> decr(final String... keys) {
            List<Long> emptyResult = new ArrayList<Long>();
            if (keys == null || keys.length == 0) {
                return emptyResult;
            }
    
            try {
                List<Long> results = JedisHelper.doJedisOperation(new JedisCallback<List<Long>>() {
                    @Override
                    public List<Long> doWithJedis(Jedis jedis) {
                        Pipeline pipelined = jedis.pipelined();
                        for (String key : keys) {
                            pipelined.decr(key);
                        }
                        List<Object> resultObjects = pipelined.syncAndReturnAll();
                        List<Long> resultList = new ArrayList<Long>();
                        for (Object resultObject : resultObjects) {
                            try {
                                resultList.add(Long.valueOf(resultObject.toString()));
                            } catch (Exception e) {
                                resultList.add(0L);
                                logger.error("redis decr result:convert object to long error", e);
                            }
                        }
                        return resultList;
                    }
                }, this.jedisPool);
                return results;
            } catch (Exception e) {
                logger.error("call decr error : ", e);
            }
            return emptyResult;
        }
    
        public Long expire(final String key, final int seconds) {
            if (StringUtils.isEmpty(key) || seconds <= 0) {
                return -1L;
            }
    
            try {
                Long result = JedisHelper.doJedisOperation(new JedisCallback<Long>() {
                    @Override
                    public Long doWithJedis(Jedis jedis) {
                        return jedis.expire(key, seconds);
                    }
                }, this.jedisPool);
                return result;
            } catch (Exception e) {
                logger.error("call expire error : ", e);
            }
            return -1L;
        }
    
        public List<Long> expire(final List<RedisExpireDTO> expires) {
            List<Long> emptyResult = new ArrayList<Long>();
            if (CollectionUtils.isEmpty(expires)) {
                return emptyResult;
            }
    
            try {
                List<Long> result = JedisHelper.doJedisOperation(new JedisCallback<List<Long>>() {
                    @Override
                    public List<Long> doWithJedis(Jedis jedis) {
                        Pipeline pipelined = jedis.pipelined();
                        for (RedisExpireDTO expire : expires) {
                            pipelined.expire(expire.getKey(), expire.getSeconds());
                        }
                        List<Object> resultObjects = pipelined.syncAndReturnAll();
                        List<Long> resultList = new ArrayList<Long>();
                        if (CollectionUtils.isNotEmpty(resultObjects)) {
                            for (Object resultObject : resultObjects) {
                                try {
                                    resultList.add(Long.valueOf(resultObject.toString()));
                                } catch (Exception e) {
                                    resultList.add(-1L);
                                    logger.error("redis expire result:convert object to long error", e);
                                }
                            }
                        }
                        return resultList;
                    }
                }, this.jedisPool);
                return result;
            } catch (Exception e) {
                logger.error("call expires error : ", e);
            }
            return emptyResult;
        }
    
        public List<String> get(final String... keys) {
            List<String> emptyResult = new ArrayList<String>();
            if (keys == null || keys.length == 0) {
                return emptyResult;
            }
    
            try {
                List<String> results = JedisHelper.doJedisOperation(new JedisCallback<List<String>>() {
                    @Override
                    public List<String> doWithJedis(Jedis jedis) {
                        return jedis.mget(keys);
                    }
                }, this.jedisPool);
                return results;
            } catch (Exception e) {
                logger.error("call get error : ", e);
            }
            return emptyResult;
        }
    
        public List<byte[]> get(final byte[]... keys) {
            List<byte[]> emptyResult = new ArrayList<byte[]>();
            if (keys == null || keys.length == 0) {
                return emptyResult;
            }
    
            try {
                List<byte[]> results = JedisHelper.doJedisOperation(new JedisCallback<List<byte[]>>() {
                    @Override
                    public List<byte[]> doWithJedis(Jedis jedis) {
                        return jedis.mget(keys);
                    }
                }, this.jedisPool);
                return results;
            } catch (Exception e) {
                logger.error("call get error : ", e);
            }
            return emptyResult;
        }
    
        //************************** List ********************************//
    
        //************************** Set ********************************//
        public Long sAdd(final String key, final String... members) {
            if (StringUtils.isEmpty(key) || members == null || members.length == 0) {
                return -1L;
            }
    
            try {
                Long result = JedisHelper.doJedisOperation(new JedisCallback<Long>() {
                    @Override
                    public Long doWithJedis(Jedis jedis) {
                        return jedis.sadd(key, members);
                    }
                }, this.jedisPool);
                return result;
            } catch (Exception e) {
                logger.error("call sadd error : ", e);
            }
            return -1L;
        }
    
        public boolean sIsMember(final String key, final String member) {
            if (StringUtils.isEmpty(key) || StringUtils.isEmpty(member)) {
                return false;
            }
            try {
                Boolean isMember = JedisHelper.doJedisOperation(new JedisCallback<Boolean>() {
                    @Override
                    public Boolean doWithJedis(Jedis jedis) {
                        return jedis.sismember(key, member);
                    }
                }, this.jedisPool);
                return isMember;
            } catch (Exception e) {
                logger.error("call sIsMember error : ", e);
            }
            return false;
        }
    
        public Set<String> sMembers(final String key) {
            Set<String> ms = null;
            if (StringUtils.isEmpty(key)) {
                return ms;
            }
            try {
                ms = JedisHelper.doJedisOperation(new JedisCallback<Set<String>>() {
                    @Override
                    public Set<String> doWithJedis(Jedis jedis) {
                        return jedis.smembers(key);
                    }
                }, this.jedisPool);
            } catch (Exception e) {
                logger.error("call smemebers error : ", e);
            }
            return ms;
        }
    
        //************************** Sorted Set ********************************//
    
        public List<Object> zAdd(final String key, final List<RedisTupleDTO> members) {
            List<Object> emptyResult = new LinkedList<Object>();
            if (StringUtils.isEmpty(key)) {
                return emptyResult;
            }
            if (CollectionUtils.isEmpty(members)) {
                return emptyResult;
            }
    
            String op = "zadd:pipeline:(RedisKey)";
            try {
                List<Object> results = JedisHelper.doJedisOperation(new JedisCallback<List<Object>>() {
                    @Override
                    public List<Object> doWithJedis(Jedis jedis) {
                        Pipeline pipelined = jedis.pipelined();
                        for (RedisTupleDTO member : members) {
                            pipelined.zadd(key, member.getScore(), member.getElement());
                        }
                        return pipelined.syncAndReturnAll();
                    }
                }, this.jedisPool);
                return results;
            } catch (Exception e) {
                logger.error("call zadd error : ", e);
            }
    
            return emptyResult;
        }
    
        public Double zIncrBy(final String key, final double score, final String member) {
            if (StringUtils.isEmpty(key) || StringUtils.isEmpty(member) ) {
                return null;
            }
    
            try {
                Double resultScore = JedisHelper.doJedisOperation(new JedisCallback<Double>() {
                    @Override
                    public Double doWithJedis(Jedis jedis) {
                        return jedis.zincrby(key, score, member);
                    }
                }, this.jedisPool);
                return resultScore;
            } catch (Exception e) {
                logger.error("call zIncrBy error : ", e);
            }
            return null;
        }
    
        public Double zScore(final String key,final String member) {
            if (StringUtils.isEmpty(key) || StringUtils.isEmpty(member)) {
                return null;
            }
            try {
                Double score = JedisHelper.doJedisOperation(new JedisCallback<Double>() {
                    @Override
                    public Double doWithJedis(Jedis jedis) {
                        return jedis.zscore(key, member);
                    }
                }, this.jedisPool);
                return score;
            } catch (Exception e) {
                logger.error("call zScore error : ", e);
            }
            return null;
        }
    
        public Set<RedisTupleDTO> zRevRangeWithScores(final String key, final long start, final long end) {
            Set<RedisTupleDTO> tupleResultSet = new LinkedHashSet<RedisTupleDTO>();
            if (StringUtils.isEmpty(key)) {
                return tupleResultSet;
            }
    
            try {
                Set<Tuple> tuples = JedisHelper.doJedisOperation(new JedisCallback<Set<Tuple>>() {
                    @Override
                    public Set<Tuple> doWithJedis(Jedis jedis) {
                        return jedis.zrevrangeWithScores(key, start, end);
                    }
                }, this.jedisPool);
    
                for (Tuple tuple : tuples) {
                    tupleResultSet.add(new RedisTupleDTO(tuple.getElement(),tuple.getScore()));
                }
            } catch (Exception e) {
                logger.error("call zRevRangeWithScores error : ", e);
            }
            return tupleResultSet;
        }
    
        public String hget(final String key, final String field) {
            String value = null;
    
            try {
                value = JedisHelper.doJedisOperation(new JedisCallback<String>() {
                    @Override
                    public String doWithJedis(Jedis jedis) {
                        return jedis.hget(key, field);
                    }
                }, this.jedisPool);
    
            } catch (Exception e) {
                logger.error("call hget error : ", e);
            }
    
            return value;
        }
    
        public Long hset(final String key, final String field, final String value) {
            Long ret = null;
    
            try {
                ret = JedisHelper.doJedisOperation(new JedisCallback<Long>() {
                    @Override
                    public Long doWithJedis(Jedis jedis) {
                        return jedis.hset(key, field, value);
                    }
                }, this.jedisPool);
    
            } catch (Exception e) {
                logger.error("call hset error : ", e);
            }
    
            return ret;
        }
    
        public Map<String, String> hgetall(final String key) {
            Map<String,String> ret = null;
    
            try {
                ret = JedisHelper.doJedisOperation(new JedisCallback<Map<String,String>>() {
                    @Override
                    public Map<String,String> doWithJedis(Jedis jedis) {
                        return jedis.hgetAll(key);
                    }
                }, this.jedisPool);
    
            } catch (Exception e) {
                logger.error("call hgetall error : ", e);
            }
    
            return ret;
        }
    
        public String set(final String key, final String value) {
            String ret = null;
            try {
                ret = JedisHelper.doJedisOperation(new JedisCallback<String>() {
                    @Override
                    public String doWithJedis(Jedis jedis) {
                        return jedis.set(key, value);
                    }
                }, this.jedisPool);
    
            } catch (Exception e) {
                logger.error("call set error : ", e);
                throw new RuntimeException(e.getMessage());
            }
    
            return ret;
        }
    
    
        public String setex(final String key, final int seconds, final String value) {
            String ret = null;
            try {
                ret = JedisHelper.doJedisOperation(new JedisCallback<String>() {
                    @Override
                    public String doWithJedis(Jedis jedis) {
                        return jedis.setex(key, seconds, value);
                    }
                }, this.jedisPool);
    
            } catch (Exception e) {
                logger.error("call setex error : ", e);
                throw new RuntimeException(e.getMessage());
            }
    
            return ret;
    
        }
    
        public String set(final byte[] key, final byte[] value) {
            String ret = null;
            try {
                ret = JedisHelper.doJedisOperation(new JedisCallback<String>() {
                    @Override
                    public String doWithJedis(Jedis jedis) {
                        return jedis.set(key, value);
                    }
                }, this.jedisPool);
    
            } catch (Exception e) {
                logger.error("call set error : ", e);
                throw new RuntimeException(e.getMessage());
            }
    
            return ret;
        }
    
        public String setex(final byte[] key, final int seconds, final byte[] value) {
            String ret = null;
            try {
                ret = JedisHelper.doJedisOperation(new JedisCallback<String>() {
                    @Override
                    public String doWithJedis(Jedis jedis) {
                        return jedis.setex(key, seconds, value);
                    }
                }, this.jedisPool);
    
            } catch (Exception e) {
                logger.error("call setex error : ", e);
                throw new RuntimeException(e.getMessage());
            }
    
            return ret;
        }
    
        public Long del(final String... keys){
            Long ret = null;
            try {
                ret = JedisHelper.doJedisOperation(new JedisCallback<Long>() {
                    @Override
                    public Long doWithJedis(Jedis jedis) {
                        return jedis.del(keys);
                    }
                }, jedisPool);
            }catch (Exception e){
                logger.error("call del error : ", e);
            }
            return ret;
        }
    }
    

    注意,其中用到的JedisHelper和JedisCallback

    关于Jedis的架构

    • JedisPool使用了commons-pool,记得去研究一下
    • BinaryJedisCommands, JedisCommands是两个interface,其中定义了二进制和String两种类型的Redis所支持的命令
    • Jedis, BinaryJedis实现了上面两个interface
    • Pipeline可以通过jedis.pipelined来获得,然后反复调用pipeline上的方法,最后pipelined.syncAndReturnAll();

    关于二进制存储

    • 可以用commons-lang包里面的SerializationUtils的serialize和deserialize方法进行序列化和反序列化

    Code

    [email protected]

    原文:https://blog.huachao.me/2015/6/redis基础/

    上一篇:

    下一篇:

    相关推荐

    1 Comment

    发表评论

    电子邮件地址不会被公开。 必填项已用*标注

    8 + 4 = ?

    网站地图|广东快乐10分开奖直播

    Copyright © 2015-2019 广东快乐10分开奖直播 All rights reserved.
    闽ICP备15015576号-1,版权所有?psz.

  • 周蓬安.blog的博客—强国博客—人民网 2019-05-10
  • 紫光阁中共中央国家机关工作委员会 2019-05-10
  • 感触名家笔下的端午文化吃香粽原来可以这样文艺 2019-05-09
  • 追梦夺冠游行嘲讽詹皇 百万人面前穿订制T恤羞辱他 2019-04-27
  • 《瘟疫传说》:黑死病恐怖 姐弟在绝望中求生 2019-04-10
  • 陕西国防工业职业技术学院百名大学生志愿者敬老院慰问孤寡老人陕西国防工业职业技术学院百名大学生志愿者敬老院慰问-陕西教育新闻 2019-04-08
  • 西藏拉萨:新家园 新生活 2019-04-08
  • 尊重和保障宗教信仰自由的中国实践 2019-04-06
  • 一敬泯恩仇 俄罗斯队主帅这个动作太暖了 2019-03-20
  • 四大名著剧组首次同台忆往事 经典影视剧如何铸就? 2018-12-07
  • “天眼”凝望 探秘宇宙 2018-12-07
  • 重庆时时彩票改成欢乐生肖吗 北京赛车七码公式规律 中国体彩超级大乐透 体彩超级大乐透玩法 高频彩的定义及分类 pk10赛车现场开奖结果 体彩20选5开奖结果走势图 体彩6+1彩票开奖结果 幸运赛车现场开 搜狐彩票合法吗 北京赛车提前开奖器 彩神通彩票软件 1十二生肖时时彩 北京赛车场地地图 七星彩玩法 天津时时彩几点开奖时间