jedis针对三种redis工作模式的连接方式

来源:互联网 发布:stc89c52单片机简介 编辑:程序博客网 时间:2024/05/23 13:20

这里说的三种工作模式是指:

    1、单机模式

    2、分片模式

    3、集群模式(since 3.0)

说明图详见以下:


使用jedis连接模式一:    

//初始化连接private Jedis jedis=new Jedis("192.168.0.100"6379);

jedis.set("a","b");

jedis.hmset(key, hash);

……

使用jedis连接模式二:   

        GenericObjectPoolConfig config=new GenericObjectPoolConfig();

        config.setMaxIdle(32); 

        config.setMinIdle(12);

        config.setTestOnBorrow(true); 

        config.setTestOnReturn(rtrue);

        config.setTestWhileIdle(true);

        List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); 

        for (int i = 0; i < shareds.size(); i++) {

         shards.add(new JedisShardInfo("192.168.0.100"6379200)); 

}

        // 构造池 

        ShardedJedisPool shardedJedisPool= new ShardedJedisPool(config, shards);

        ShardedJedis jedis=shardedJedisPool.getResource();

        jedis.set("a","b");

        jedis.hmset(key, hash);

        ……

使用jedis连接模式三:

                Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();

for (int i = 0; i < shareds.size(); i++) {

jedisClusterNodes.add(new HostAndPort("192.168.0.100",6379));

}

// 构造池

BinaryJedisCluster cluster= new BinaryJedisCluster(jedisClusterNodes);

cluster.set("a","b");

                cluster.hmset(key, hash);

                ……

BinaryJedisCluster.java是为了让jedis支持byte数组形式value重写的一个类,参考网上文档,记不得来源了。以下是代码(部分):

public class BinaryJedisCluster extends JedisCluster {

 

    public String set(final String key, final byte[] value) {

        return new JedisClusterCommand<String>(connectionHandler, timeout,

                maxRedirections) {

 

            public String execute(Jedis connection) {

                return connection.set(SafeEncoder.encode(key), value);

            }

        }.run(key);

    }

 

    public byte[] getBytes(final String key) {

        return new JedisClusterCommand<byte[]>(connectionHandler, timeout,

                maxRedirections) {

 

            public byte[] execute(Jedis connection) {

                return connection.get(SafeEncoder.encode(key));

            }

        }.run(key);

    }

 

 

    public Set<byte[]> zrevrangeByScoreBytes(final String key, final double max,

                                        final double min, final int offset, final int count) {

        return new JedisClusterCommand<Set<byte[]>>(connectionHandler, timeout,

                maxRedirections) {

 

            public Set<byte[]> execute(Jedis connection) {

                return connection.zrevrangeByScore(SafeEncoder.encode(key),

                        max, min, offset, count);

            }

        }.run(key);

    }

 

 

    public Set<byte[]> zrevrangeByScoreBytes(final String key, final String max,

                                        final String min, final int offset, final int count) {

        return new JedisClusterCommand<Set<byte[]>>(connectionHandler, timeout,

                maxRedirections) {

 

            public Set<byte[]> execute(Jedis connection) {

                return connection.zrevrangeByScore(SafeEncoder.encode(key),

                        SafeEncoder.encode(max), SafeEncoder.encode(min), offset, count);

            }

        }.run(key);

    }

 

 

    public Long linsert(final String key, final LIST_POSITION where,

                        final byte[] pivot, final byte[] value) {

        return new JedisClusterCommand<Long>(connectionHandler, timeout,

                maxRedirections) {

 

            public Long execute(Jedis connection) {

                return connection.linsert(SafeEncoder.encode(key), where,

                        pivot, value);

            }

        }.run(key);

    }

 

 

    public Long lpushx(final String key, final byte[]... string) {

        return new JedisClusterCommand<Long>(connectionHandler, timeout,

                maxRedirections) {

 

            public Long execute(Jedis connection) {

                return connection.lpushx(SafeEncoder.encode(key), string);

            }

        }.run(key);

    }

 

 

    public Long rpushx(final String key, final byte[]... string) {

        return new JedisClusterCommand<Long>(connectionHandler, timeout,

                maxRedirections) {

 

            public Long execute(Jedis connection) {

                return connection.rpushx(SafeEncoder.encode(key), string);

            }

        }.run(key);

    }

 

 

    public List<byte[]> blpopBytes(final String arg) {

        return new JedisClusterCommand<List<byte[]>>(connectionHandler,

                timeout, maxRedirections) {

 

            public List<byte[]> execute(Jedis connection) {

                return connection.blpop(SafeEncoder.encode(arg));

            }

        }.run(null);

    }

 

 

    public List<byte[]> brpopBytes(final String arg) {

        return new JedisClusterCommand<List<byte[]>>(connectionHandler,

                timeout, maxRedirections) {

 

            public List<byte[]> execute(Jedis connection) {

                return connection.brpop(SafeEncoder.encode(arg));

            }

        }.run(null);

    }

 

}

编译后jedis jar包下载地址:http://pan.baidu.com/s/1mgIPYDa

源码地址:http://pan.baidu.com/s/1jGIobBo

 


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 一个多月宝宝鼻子有鼻屎怎么办 三个月婴儿感冒发烧怎么办 小孩感冒发烧流鼻涕怎么办 小孩感冒发烧反反复复怎么办 宝宝反复发烧39怎么办 一岁婴儿流鼻涕怎么办 四岁宝宝发烧怎么办 小孩流清鼻涕怎么办? 5宝宝光流清鼻涕怎么办 孩子一直流鼻子怎么办 10岁天天流鼻涕怎么办 喉咙痛又痒咳嗽怎么办 60天宝宝流鼻涕怎么办 宝宝流鼻子严重怎么办 鼻炎鼻涕多鼻塞怎么办 夏天老人感冒流鼻涕怎么办 鼻窦炎流清鼻涕怎么办 鼻子有脓鼻涕怎么办 宝宝有脓鼻涕怎么办 小孩脓鼻涕咳嗽怎么办 哺乳期流黄鼻涕怎么办 宝宝鼻塞流脓涕怎么办 喉咙痛浓痰咳嗽怎么办 哺乳期留清鼻涕怎么办 哺乳期就清鼻涕怎么办 小金毛流脓鼻涕怎么办 鼻炎有浓鼻涕怎么办 宝宝流稠鼻涕怎么办 小孩流粘鼻涕怎么办 小孩流黄鼻涕怎么办 鼻炎有脓鼻涕怎么办 小孩鼻子有鼻涕怎么办 小儿浓鼻涕咳嗽怎么办 宝宝冻着流鼻涕怎么办 打喷嚏流清鼻涕怎么办 鼻子老想打喷嚏怎么办 冻着了流鼻涕怎么办 狗狗鼻塞流鼻涕怎么办 感冒总爱打喷嚏怎么办 一直流鼻涕怎么回事该怎么办 感冒一直流鼻涕该怎么办