Redis client Jedis using---connection pool

Source: Internet
Author: User
Tags auth connection pooling hash redis port number

Jedis is a Redis Java client that, through a period of time, Jedis basically implements all of Redis's capabilities, and Jedis Redis data sharding on the client, Redis itself has no data distribution capabilities. First, download Jedis code

Jedis Code Address: Https://github.com/xetorthio/jedis

Feel the power of open source again. Hehe, we have time to look at the source code. second, how to use Jedis in the project

Now we all like to use MAVEN as a project management tool, then I just explain how Maven uses Jedis, just add the following dependencies to it.

<dependency>
        <groupId>redis.clients</groupId>
        <artifactid>jedis</artifactid >
        <version>2.1.0</version>
    </dependency>

OK, it's really handy to manage the jar dependencies with maven. It is recommended that you use MAVEN to manage your project. third, establish a simple connection

public class Jedisutil {public

    static Jedis Createjedis () {
        Jedis Jedis = new Jedis ("127.0.0.1");
        return Jedis;
    }

    public static Jedis Createjedis (String host, int port) {
        Jedis Jedis = new Jedis (host, port);

        return Jedis;
    }

    public static Jedis Createjedis (string host, int port, string passwrod) {
        Jedis Jedis = new Jedis (host, port);

        if (! Stringutils.isnotblank (Passwrod))
            Jedis.auth (passwrod);
        
        return Jedis;
    }
}
To create a simple Jedis object, tell it the IP address and port number, and if Redis has a password, you need to call the Auth method to set the password when establishing the connection.
Of course, a simple connection is not suitable for real-world use and requires connection pooling support.
Iv. Establishment of Jedis connection pool

Create connection Jedis connection pool, Jedis provide Jedispool object, use more convenient. Jedis Pool is implemented based on the Apache common pool.

Package Cn.opensv.example.redis;
Import Redis.clients.jedis.Jedis;
Import Redis.clients.jedis.JedisPool;

Import Redis.clients.jedis.JedisPoolConfig;

    public class Jedispoolutils {private static Jedispool pool;
 /** * Set up a connection pool real environment, generally the configuration parameters are extracted out. * */private static void Createjedispool () {//Establish connection pool configuration parameter jedispoolconfig config = new Jedispoolcon

        Fig ();

        Set maximum number of connections config.setmaxactive (100);

        Set the maximum blocking time, remembering the number of milliseconds milliseconds config.setmaxwait (1000);

        Set the spatial connection Config.setmaxidle (10);

    Create connection Pooling pool = new Jedispool (config, "127.0.0.1", 6379); /** * Synchronous initialization in multithreaded environment */private static synchronized void Poolinit () {if (pool = = null) Crea
    Tejedispool ();
            }/** * Gets a Jedis object * * @return */public static Jedis Getjedis () {if (pool = = null)
        Poolinit ();
    return Pool.getresource ();
/** * Return a connection * * @param Jedis */    public static void Returnres (Jedis Jedis) {pool.returnresource (Jedis); }

}
v. Jedis implementation of Sharding

The Jedis shard uses the hash algorithm and the key pattern matching based on. Jedis define a hash interface, if you feel uncomfortable with the self, you can implement a hash algorithm. Jedis's own hash algorithm is MurmurHash 2.0.

MurmurHash algorithm: High computational performance, low collision rate, founded in 2008 by Austin Appleby, is now applied to Hadoop, libstdc++, Nginx, libmemcached and other open source systems. Appleby was hired by Google in 2011, then Google introduced its variant of the Cityhash algorithm.

Official website: https://sites.google.com/site/murmurhash/

MurmurHash algorithm, claiming to be super fast hash algorithm, is the FNV 4-5 times.

/**
 * Create Shard Object
 * @return */
 
    
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.