3, simple to use Redis client

Simple to use Java-based presentation of Redis Client:

  1, the introduction Maven dependency:

<dependency>
    <groupId>redis.clients</>
    <artifactId>jedis</>
    <version>2.9.0</>
    <type>jar</>
    <scope>complie</>
</>

  2, the object generating Jedis:

    Jedis = Jedis new new Jedis (Host String, int Port, int connectionTimeout, int soTimeout); 
parameter list: 
    Host: machine where the Redis node IP 
    Port: the Redis node port 
    connectionTimeout: client connection timeout 
    soTimeout: Client write timeout 
performed SET / get the demo 
    jedis.set ( "the Hello", "world" ); 
    String value = jedis.get ( "the Hello");

Direct schematic Jedis:

  1, the object generating Jedis 2, Jedis Run 3, 4 returns the execution result, to close the connection Jedis


 Jedis schematic connection pool:

 

   1, by Jedis object from the resource pool 2, Jidis Run 3, 4 returns the execution result, the object returned to the pool Jedis


 Connection pooling is simple to use:

GenericObjectPoolConfig poolConfig = 
    new GenericObjectPoolConfig();
JedisPool jedisPool = new JedisPool(poolConfig,host,port);   
Jedis jedis = null;
try{
    jedis = jedisPool.getResource();
    jedis.set("hello","world");
} catch(Exception e){
    ...
}finally{
    if(jedis != null){
        jedis.close();
    }
}

 Comparison with direct connection pool:

  advantage:

  --- Direct: simple and convenient, suitable for a small amount of long-term connection scenarios

  --- Connection Pooling: Jedis generated in advance, reducing the cost by using the used form of protection and control resources connected pool

  Disadvantages:

  --- Direct: Every time there is a new on / off TCP overhead, resources can not be controlled, there may be a connection leak, Jedis objects thread-safe

  --- connection pool: direct use of relatively trouble, we need to have more parameters on resource management to ensure

 

Guess you like

Origin www.cnblogs.com/zhangbLearn/p/11598610.html