General development of Jedis client in "Redis" Redis cluster and non-clustered environment

Source: Internet
Author: User
Tags redis cluster

Under non-clustered environment

Package Com.chiwei.redis;import Java.util.arraylist;import Java.util.list;import org.junit.test;import Redis.clients.jedis.jedis;import Redis.clients.jedis.jedispool;import Redis.clients.jedis.jedispoolconfig;import Redis.clients.jedis.jedisshardinfo;import Redis.clients.jedis.shardedjedis;import Redis.clients.jedis.shardedjedispool;import Redis.clients.util.pool;public class Redissingleclient {// Non-fragmented client connection private Jedis jedis;//non-fragmented client connection pool private Jedispool jedispool;//Shard Client Connection Private Shardedjedis shardedjedis;// Shard Client Connection Pool private Shardedjedispool shardedjedispool;//jedispool configuration private jedispoolconfig config;public void Initjedispoolconfig () {config = new jedispoolconfig (); Config.setmaxtotal (+); Config.setmaxidle (10); Config.setmaxwaitmillis (1000L); Config.settestonborrow (true); Config.settestonreturn (true);} public void Initjedispool () {initjedispoolconfig (); jedispool = new Jedispool (config, "192.168.11.176", 7379);} public void Initshardedjedispool () {initjedispoolconfig (); List<jedisshardinfo> Shards = new arraylist<jedisshardinfo> (), Shards.add (New Jedisshardinfo ("192.168.11.176", 7379)); Shards.add ( New Jedisshardinfo ("192.168.11.177", 7379)); Shards.add (New Jedisshardinfo ("192.168.11.178", 7379)); Shardedjedispool = new Shardedjedispool (config,shards);} Public Jedis Getjedis () {Jedis = This.jedisPool.getResource (); return Jedis;} Public Shardedjedis Getshardedjedis () {Shardedjedis = This.shardedJedisPool.getResource (); return Shardedjedis;} @SuppressWarnings ({"Unchecked", "rawtypes"}) public void Returnresource (pool pool, Object Redis) {if (Redis! = null) {poo L.returnresourceobject (Redis);}} public void set (string key, String value) {try {Jedis = Getjedis (); Shardedjedis = Getshardedjedis (); Jedis.set (key, value); Shardedjedis.getshard ("Chiwei"). Set ("Chiwei", "lining");} catch (Exception e) {e.printstacktrace (); Jedispool.returnbrokenresource (Jedis); Shardedjedispool.returnbrokenresource (Shardedjedis);} finally {Returnresource (Jedispool, Jedis); Returnresource (Shardedjedispool, ShardEdjedis);}}          public string get (string key) {string value = null;            try {Jedis = Getjedis ();          Value = Jedis.get (key);              } catch (Exception e) {e.printstacktrace ();          Release Resources Jedispool.returnbrokenresource (Jedis);          } finally {Returnresource (Jedispool, Jedis);      } return value;          public string Getshardvalue (string key) {string value = null;            try {Shardedjedis = Getshardedjedis ();        Value = Shardedjedis.getshard (key). get (key);              } catch (Exception e) {e.printstacktrace ();        Release Resources Shardedjedispool.returnbrokenresource (Shardedjedis);        } finally {Returnresource (Shardedjedispool, Shardedjedis);      } return value; } @Testpublic void Test () {Initjedispool (); Initshardedjedispool (); Jedis = Getjedis (); Shardedjedis = Getshardedjedis (); Jedis.set ("Momo", "Nono");Shardedjedis.set ("Chiwei", "lining"); System.out.println (Jedis.get ("Momo"))//system.out.println (Shardedjedis.get ("Chiwei")); Shardedjedis.getshard (" Chiwei "). Set (" Chiwei "," lining "); System.out.println (Shardedjedis.getshard ("Chiwei"). Get ("Chiwei"));}}
The purpose of the Shard Shard is to distribute the data evenly across multiple Redis instances

The above test examples are simple key-value, try the complex key below

List

Shardedjedis.lpush ("Chiwei", "one"), Shardedjedis.lpush ("Chiwei", "one"), Shardedjedis.lpush ("Chiwei", "three"); Shardedjedis.lpush ("Chiwei", "three"); System.out.println (Shardedjedis.llen ("Chiwei")); String str = shardedjedis.lpop ("Chiwei"); System.out.println (str);

The list adds elements that can be repeated

Set cannot be duplicated

Shardedjedis.sadd ("Chiwei", "one"), Shardedjedis.sadd ("Chiwei", "one");


All other types, please try it yourself, http://www.redis.cn/commands.html

Cluster environment

Package Com.chiwei.redis;import Java.util.hashset;import Java.util.set;import org.junit.test;import Redis.clients.jedis.hostandport;import Redis.clients.jedis.jediscluster;public class RedisClusterClient {private String ServerInfo = " 172.30.129.145:7379,172.30.129.145:7380,172.30.129.146:7379,172.30.129.146:7380,172.30.129.147:7379,172.30.129.147:7380 ";p rivate set
Use the Jediscluster object to manipulate the Redis cache.

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


General development of Jedis client in "Redis" Redis cluster and non-clustered environment

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.