Redis java client Jedis simple Encapsulation

Source: Internet
Author: User

Redis java client Jedis simple Encapsulation

After some discussions by our team, we finally decided to use redis for our business cache. Redis caches data to the memory, and runs quickly. At the same time, data is asynchronously written to the disk for persistence.

Redis also supports master-slave synchronization, distributed deployment, and N multi-data structures, which is very attractive to us.

See http://blog.csdn.net/yichenlian/article/details/27207383

Our team focused on redis disaster recovery. Because redis persistence is asynchronous, there will always be a bit of time when the data in the memory is not synchronized with the disk data (of course, you can set it to full synchronization, so the significance of using redis is greatly reduced ). Although there is no perfect solution, some online processing methods are acceptable.

See http://blog.csdn.net/xiangliangyu/article/details/8165644
Then I used the java client Jedis to encapsulate redis-related operations in some simple systems. The jedis distributed connection pool is used to facilitate horizontal scaling of redis and increase high availability. Encapsulation Method: first, define the connection pool class and encapsulate the connection pool acquisition method. The interface defines general redis operations and the methods are basically the same as those in redis, and the interfaces are easy to expand; implementation interface. The implementation class I encapsulate only uses the distributed method (ShardedJedis). You can also use other implementation methods to expand it on your own.
1. Obtain the connection pool:

Simple configuration of JedisPoolConfig

JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();jedisPoolConfig.setMaxTotal(maxTotal);//the max number of connectionjedisPoolConfig.setMaxIdle(maxIdle);//the max number of freejedisPoolConfig.setMaxWaitMillis(maxWaitMillis);//the longest time of waiting

Set JedisShardInfo (redis. clients. jedis. JedisShardInfo. JedisShardInfo (String host, int port ))

Get the ShardedJedisPool: redis. clients. jedis. ShardedJedisPool. ShardedJedisPool (GenericObjectPoolConfig poolConfig, List <JedisShardInfo> shards, Hashing algo)

In this way, the connection pool is obtained.

2. Define the interface, simply:

Public interface JedisClientInterface {/*** write a common string type value ** @ param key * @ param value */void put (String key, String value ); /*** read value of the String type ** @ param key * @ return */String get (String key );}

3. Complete implementation class:

public class JedisClientShardImpl implements JedisClientInterface {@Overridepublic void put(String key, String value) {boolean flag = true;ShardedJedis shardedJedis = pool.getResource();try{if(shardedJedis!=null){shardedJedis.set(key, value);}}catch(Exception e){flag = false;e.printStackTrace();}finally{if (flag){                           pool.returnResource(shardedJedis);                        }else{                           pool.returnBrokenResource(shardedJedis);                        }}}@Overridepublic String get(String key) {boolean flag = true;ShardedJedis shardedJedis = pool.getResource();try{if(shardedJedis!=null){return shardedJedis.get(key);}}catch(Exception e){flag = false;e.printStackTrace();}finally{<pre name="code" class="java">                        if (flag){                           pool.returnResource(shardedJedis);                        }else{                           pool.returnBrokenResource(shardedJedis);                        }}return null;}}

So far, the entire encapsulation process has passed. It can be expanded down any time. 

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.