Jedis connection for three Redis working modes

Source: Internet
Author: User

Here are the three types of working patterns that are referred to as:

1. Stand-alone mode

2. Shard Mode

3. Cluster mode (since 3.0)

The illustration is detailed below:

Using Jedis connection mode one:

Initialize connection private Jedis jedis=new Jedis ("192.168.0.100", 6379); Jedis.set ("A", "B"); Jedis.hmset (key, hash);

Using Jedis connection mode two:

        GenericObjectPoolConfig config=new  Genericobjectpoolconfig ();         config.setmaxidle (+);          config.setminidle (;       )  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",  6379, 200)); }         //  Construction Pool    &nbsP;     shardedjedispool shardedjedispool= new shardedjedispool (config,  shards);         shardedjedis jedis= Shardedjedispool.getresource ();         jedis.set ("A", "B");         jedis.hmset (Key, hash);         

Use Jedis connection mode three:

set
Binaryjediscluster.java is a class that allows Jedis to support byte array form value rewrite, referring to the online documentation and not being sourced. Here is the code (part):
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,                              &nbsP;           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);             } &Nbsp;      }.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)  {      &nbsP;          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,   &nbsP;            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&nbSp 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);    }} 

Post-compilation Jedis jar package: Http://pan.baidu.com/s/1mgIPYDa

Source Address: Http://pan.baidu.com/s/1jGIobBo

Jedis connection for three Redis working modes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.