Operation of Redis on Java client

Source: Internet
Author: User
Tags key string redis cluster

Redis high-performance, fast, high-efficiency features, used as a cache server is a good choice. (similar to Memcache)
Steps for Redis on the client:

1.redis Single version operation

1.1 Working with Jedis objects

(1) Pass the IP address of the server where the Redis service is installed and the Redis port number as a construction parameter to Jedis, which is used to create a Jedis object

Jedis Jedis = new Jedis (ip,port);

(2) The Jedis object created by the first step, manipulating the 5 large data types of Redis (hash type, string type, list type, set type, Zset type, ordered)

Jedis.set (string key,string value);

Jedis.get (string key);

(3) Close Jedis Connection when operation is complete

Jedis.close ();

This method requires each time you create a connection, close the connection, and compare wasted resources. Therefore, use the following Jedispool connection pool to operate a single version of Redis

Directly on the code:

// Create a Jedis object        New Jedis ("IP", 6379);         // manipulating the string data type        Jedis.set ("username", "HelloWorld");         // remove the corresponding value value        according to Key String value = Jedis.get ("username");         // Value Output         System.out.println (value);         // Close Connection        Jedis.close ();

1.2 Operation via Jedispool Jedis

(1) Create Jedispool connection pool

Jedispool pool = new Jedispool (ip,port);

(2) Get Jedis objects through connection pooling

Jedis Jedis = Pool.getresource ();

(3) After acquiring the Jedis object, you can directly manipulate the Redis data type

(4) The Jedis object is returned to the connection pool after the operation is completed, and the resource is recycled

(5) Close when the connection pool is not used

//Create a connection poolJedispool pool =NewJedispool ("IP address", 6379); //Get the Connection objectJedis Jedis =Pool.getresource (); //Operation Hash TypeMap<string, string> hash =NewHashmap<>(); Hash.put ("Name", "Tom"); Hash.put ("Age", "23"); Hash.put ("Address", "Changjiang Road"); Jedis.hmset ("Student", hash); Map<string, string> all = Jedis.hgetall ("Student"); Set<entry<string, string>> entryset =All.entryset ();  for(Entry<string, string>Entry:entryset) {String key=Entry.getkey (); String value=Entry.getvalue (); SYSTEM.OUT.PRINTLN (Key+ ":" +value); }//close connection, connect Pool recycle resourceJedis.close (); //Close Connection PoolPool.close ();

2. Operation Cluster Redis-cluster

(1) Create a list of collections to hold each Redis instance in the cluster

set

Nodes.Add (New Hostandport (Ip.port));

.. And so on, add each of the Redis instances

(2) The first step is to create a Redis cluster instance object after you get the list of collections

Jediscluster jediscluster = new Jediscluster (nodes);//You need to pass the Redis node list as a construction parameter to create a cluster object

(3) Each Redis object in the cluster is differentiated by a data slot (there is a total of 0--16384 slots in the redis-3.0.0), so after acquiring the cluster object, the cluster can be manipulated, and each redis is given a 00 chance of being accessed.

Jediscluster.set (String key,string value);

String value = Jediscluster.get (key);

(4) Close the Jediscluster object before the system shuts down.

Jediscluster.close ();

Code:

@Test Public voidTestredis_cluster () {//Create a set collectionsetNewHashset<>(); //Add the IP address and port number of each node to the collectionNodes.Add (NewHostandport ("192.168.xx.xxx", port)); Nodes.Add (NewHostandport ("192.168.xx.xxx", port)); Nodes.Add (NewHostandport ("192.168.xx.xxx", port)); Nodes.Add (NewHostandport ("192.168.xx.xxx", port)); Nodes.Add (NewHostandport ("192.168.xx.xxx", port)); Nodes.Add (NewHostandport ("192.168.xx.xxx", port)); //To Create a Jediscluster object, you need to pass the list of Redis collections created as parametersJediscluster Jediscluster =Newjediscluster (nodes); //manipulating Redis with cluster objectsJediscluster.set ("Address", "Changan Street, Beijing"); String value= Jediscluster.get ("Address"); //Print ResultsSystem.out.println (value); //close the Jediscluster object before the system shuts downJediscluster.close (); }

Operation of Redis on Java client

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.