Jedis Connection Pool Detailed (Redis)

Source: Internet
Author: User
Tags redis cluster redis server

Atomicity (atomicity):

A transaction is an indivisible minimum unit of work, and the operations included in the transaction are either done or not.

The execution of all Redis commands is atomic, which is related to its single-threaded mechanism;

The atomicity of REDIS commands makes it possible to use the atomic self-increment operation incr to realize the simple counter function without considering the concurrency problem.

stand-alone mode :

package com.ljq.utils;import redis.clients.jedis.jedis;import redis.clients.jedis.jedispool; Import redis.clients.jedis.jedispoolconfig;/**    * redis operator Interface  * *   @author   Lin Yi-chin  *  @version  1.0 2013-6-14  Morning 08:54:14    */ Public class redisapi {    private static jedispool pool  = null;        /**     *  Building a Redis connection pool      *      *  @param  ip      *  @param  port     *  @return  JedisPool      */    public static jedispool getpool ()  {         if  (pool == null)  {              jedispoolconfig config = new jedispoolconfig ();             //controls how many Jedis instances a pool can allocate, and pool.getresource () to get;             //If the assignment is-1, it means no limit, and if the pool already has maxactive Jedis instances assigned to it, The pool's state is exhausted (exhausted) at this time.             config.setmaxactive (+);             //controls the maximum number of Jedis instances in a pool that have an idle (idle) state.             config.setmaxidle (5);             //represents the maximum wait time when borrow (introduces) a Jedis instance, if it exceeds the waiting time, The jedisconnectionexception;             is thrown directly Config.setmaxwait (1000 * 100);             //Borrow a Jedis instance, do you want to advance ValIdate operation; If true, the resulting Jedis instances are available;             Config.settestonborrow (true);             pool  = new jedispool (config,  "192.168.2.191",  8888);         }        return pool;    }         /**     *  return to Connection pool       *      *  @param  pool      *   @param  redis     */    public static void  returnresource (Jedispool pool, jedis redis)  {         if  (redis != null)  {             pool.returnresourceObject (Redis);        }    }         /**     *  Get Data      *       *  @param  key     *  @return       */    public static string get (String key) {         String value = null;                 JedisPool pool = null;         Jedis jedis = null;         try {            pool  = getpool ();             jedis =  pool.getresource ();  &nbsP;          value = jedis.get (Key);         } catch  (exception e)  {             //Releasing Redis objects              pool.returnbrokenresource (Jedis);             e.printstacktrace ();         } finally {             //return to Connection pool              returnresource (Pool, jedis);         }                 return value;    }}

Reference article:

Http://www.cnblogs.com/linjiqin/archive/2013/06/14/3135248.html


Distributed mode


Shardedjedis is a distributed redis cluster client based on the consistent hash algorithm

Package com.jd.redis.client; import java.util.arraylist;import java.util.list; import  redis.clients.jedis.JedisPoolConfig;import redis.clients.jedis.JedisShardInfo;import  redis.clients.jedis.shardedjedis;import redis.clients.jedis.shardedjedispool;import  Redis.clients.util.hashing;import redis.clients.util.sharded; publicclass redisshardpooltest  {    static ShardedJedisPoolpool;    static{         jedispoolconfig config =new jedispoolconfig ();//jedis Pool Configuration         config.setmaxactive (500);//maximum number of active objects            config.setmaxidle (1000 * 60);//Object Max idle time            config.setmaxwait (1000 * 10);//Max wait time to get object            config.seTtestonborrow (True);        string hosta =  " 10.10.224.44 ";          int porta = 6379;           String hostB =  "10.10.224.48";           int portB = 6379;         List<JedisShardInfo> jdsInfoList =new ArrayList< Jedisshardinfo> (2);        jedisshardinfo infoa =  New jedisshardinfo (Hosta, porta);         infoa.setpassword (" Redis.360buy ");        jedisshardinfo infob = new  Jedisshardinfo (HOSTB,&NBSP;PORTB);         infob.setpassword (" Redis.360buy ");      &nbsP;  jdsinfolist.add (INFOA);         jdsinfolist.add (InfoB);                 pool =new  shardedjedispool (Config, jdsinfolist, hashing.murmur_hash,sharded.default_key_tag_pattern);     //Incoming connection pool configuration, distributed Redis Server host information, Shard rules (which Redis server to store to)}         /**     *  @param  args     */     publicstaticvoid main (String[] args)  {         for (int i=0; i<100; i++) {            string key =generatekey ();            //key +=  "{AAA}";            shardedjedis jds =null;           try {                jds =pool.getresource ();                system.out.println (key+ ":" +jds.getShard (Key) . Getclient (). GetHost ());                system.out.println (Jds.set (Key, "1111111111111111111111111111111"));            }catch  (exception e)  {                e.printstacktrace ();            }           finally{                pool.returnresourceobject ( JDS);            }        }     }     privatestaticintindex = 1;     Publicstatic string generatekey () {        return  String.valueof (Thread.CurrentThread (). GetId ()) + "_" + (index++);     }}

Reference article:

http://blog.csdn.net/lang_man_xing/article/details/38405269


This article is from the "Nothing-skywalker" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1684306

Jedis Connection Pool Detailed (Redis)

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.