Operation of redis in java client

Redis has the characteristics of high performance, high speed and high efficiency, and it is a good choice to be used as a cache server. (Similar to memcache)
Redis operation steps on the client side:

1. Redis stand-alone operation

1.1 Operation through Jedis objects

(1) Pass the ip address of the server where the redis service is installed and the port number of redis to Jedis as construction parameters to create a Jedis object

  Jedis jedis  = new Jedis(ip,port);

(2) Through the jedis object created in the first step, operate the five 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 the jedis connection after the operation is completed

  jedis.close();

This method needs to create and close the connection every time, which is a waste of resources. So use the following jedisPool connection pool to operate the stand-alone version of redis

 

Go directly to the code:

     // Create a jedis object 
        Jedis jedis = new Jedis("ip", 6379 );
         // Operate the string data type 
        jedis.set("username", "helloworld" );
         // Get the corresponding value according to the key 
        String value = jedis .get("username" );
         // Value output 
        System.out.println(value);
         // Close the connection 
        jedis.close();

 

1.2 Operating jedis through jedisPool

(1) Create a JedisPool connection pool

  JedisPool pool = new JedisPool(ip,port);

(2) Obtain the jedis object through the connection pool

  Jedis jedis = pool.getResource();

(3) After obtaining the jedis object, you can directly operate on the redis data type

(4) After the operation is completed, the jedis object is returned to the connection pool, and the resource is recovered

(5) Close the connection pool when it is not in use

    // Create connection pool 
        JedisPool pool = new JedisPool("ip address", 6379 );
         // Get connection object 
        Jedis jedis = pool.getResource();
         // Operate hash type 
        Map <String, String> hash = new HashMap<> ();
        hash.put("name", "tom");
        hash.put("age", "23");
        hash.put("address", "长江路");
        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 the connection, the connection pool recycles resources 
        jedis.close();
         // Close the connection pool 
        pool.close();

 

 

2. Operate the cluster redis-cluster

(1) Create a collection list to store each redis instance in the cluster

Set<HostAndPort> nodes = new Hash<HostAndPort>();

nodes.add(new HostAndPort(ip.port));

.. and so on, add each redis instance

(2) After obtaining the collection list in the first step, create a redis cluster instance object

JedisCluster jedisCluster = new JedisCluster(nodes);//The node list of redis needs to be passed as a construction parameter to create a cluster object

(3) Each redis object in the cluster is distinguished by data slots (there are a total of 0--16384 slots in redis-3.0.0), so after obtaining the cluster object, you can operate the cluster, each redis The probability of being accessed is 00.

jedisCluster.set(String key,String value);

String value = jedisCluster.get(key);

(4) Before the system shuts down, close the jedisCluster object.

jedisCluster.close();

Code:

@Test
     public  void testRedis_cluster() {
         // Create a set set 
        Set<HostAndPort> nodes = new HashSet<> ();
         // Add the ip address and port number of each node to the set 
        nodes.add( new HostAndPort(" 192.168.xx.xxx", port ));
        nodes.add(new HostAndPort("192.168.xx.xxx", port));
        nodes.add(new HostAndPort("192.168.xx.xxx", port));
        nodes.add(new HostAndPort("192.168.xx.xxx", port));
        nodes.add(new HostAndPort("192.168.xx.xxx", port));
        nodes.add( new HostAndPort("192.168.xx.xxx", port ));
         // To create a jedisCluster object, you need to pass the created redis collection list as a parameter 
        JedisCluster jedisCluster = new JedisCluster(nodes);
         // Operate through the cluster object redis 
        jedisCluster.set("address", "Chang'an Street, Beijing" );
        String value = jedisCluster.get("address" );
         // Print the result 
        System.out.println(value);
         // Before the system shuts down, close the jedisCluster object 
        jedisCluster.close();
    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324771185&siteId=291194637