Redis's Javaclientjedis Simple package

Source: Internet
Author: User

After a discussion by our team, we finally decided to use Redis for our business cache. Redis caches data in memory and performs very quickly. Asynchronously writes data to disk at the same time. to persist.

and Redis support master-slave synchronization, support distributed deployment, support n multi-data structures, which is of great appeal to us.

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

The focus of our team discussion is on the recovery of the Redis disaster.

Because the persistence of Redis is asynchronous. There is always a time when the data in memory is out of sync with the disk data (of course, it can be set to full synchronization.) So the meaning of redis is greatly compromised. Although no perfect solution was found. Just a few online ways of dealing with it can be accepted.

See: http://blog.csdn.net/xiangliangyu/article/details/8165644
I then used Javaclientjedis to do some simple encapsulation of the Redis-related operations in a few systems. Uses a distributed connection pool of Jedis, which facilitates redis scale-out and adds high availability. Packaging ideas: First define the connection pool class, encapsulating the connection pool acquisition method, external interface. The interface defines the Redis general operation, the method is basically consistent with Redis, and the interface is easy to extend, implement the interface, I encapsulate the implementation class only use the distributed way (that is, Shardedjedis), but also can use other implementation way, can expand itself.


1. Get the Connection pool:

Simple Configuration 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 Shardedjedispool (Connection pool): Redis.clients.jedis.ShardedJedisPool.ShardedJedisPool (genericobjectpoolconfig Poolconfig, list<jedisshardinfo> shards,Hashing algo)

This gives you a connection pool.

2. Define the interface, which is simple:

Public interface Jedisclientinterface {/** * writes a generic string type of value *  * @param key * @param value */void put (String key, String value);/** * Read value of String type *  * @param key * @return */string get (string key);}

3. Complete the Implementation class:

public class Jedisclientshardimpl implements Jedisclientinterface {@Overridepublic void put (string key, String value) {Bo Olean 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);        }} @Overridepubl IC 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{if (flag) {                Pool.returnresource (Shardedjedis);        }else{    &nbsp                 ;       pool.returnbrokenresource (shardedjedis);               }} return null; }}

At this point, the entire encapsulation process has gone through.

To be able to scale down at random.

Redis's Javaclientjedis Simple package

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.