Java redis demo

package crc.platformFramework.module;


import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import redis.clients.jedis.Jedis;

/**
* Redis combined with a small demo of java
* Redis So come three java:
* commons-pool-1.6.jar
* jedis-2.1.0.jar,
*@author tanli45
*@time
*/
public class RedisJava {

    public static void main(String[] args) {
        // Connect to the local Redis service
        Jedis jedis = new Jedis("localhost");
        System.err.println(jedis);
        System.out.println("Connect to the local Redis service successfully!");
        // Check if the service is running
        System. out.println("Service is running: " + jedis.ping());
    }
    @Test
    //Redis Java String(string) instance
    public void TestRedisString(){
        //Connect to the local Redis service
          Jedis jedis = new Jedis("localhost");
          System.out.println("Connection to server sucessfully") ;
          //Set the redis string data
          jedis.set("runoobkey", "Redis tutorial");
// jedis.lpush("ttt","sfadsf");
// // Get the stored data and output
         System.out .println("Stored string in redis:: "+ jedis.get("runoobkey"));
         jedis.set("theName", "Test input");
         String name = jedis.get("theName");
         System. out.println(" Stored theName in redis:: "+ name);

         Map<String, String> map = new HashMap<String, String>();
         map.put("name", "xinxin");
         map.put("age", "22");
         map.put("qq ", "123456");
         jedis.hmset("user",map);
         jedis.hdel("user", "age"); //delete an element in the set
         jedis.del("ttt","sfadsf "); //Delete a row of data
         System.out.println(jedis.hmget("user", "age")); //Because it was deleted, it returns null
        
        
        
         jedis.disconnect();
        
        
        
        
       //Add 
         jedis .sadd("user1","liuling"); 
         jedis.sadd("user1","xinxin"); 
         jedis.sadd("  user1","ling"); 
         jedis.sadd("user1","zhangxinxin");
         jedis.sadd("user1","who"); 
         //移除noname 
         jedis.srem("user1","who"); 
        
         System.out.println(jedis.smembers("user1"));//Get all added values 
        
        
        ​​System.out.println(jedis.clusterNodes());
        
    }
   
   
}

Guess you like

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