Learning Redis

Learning Redis

1. Redis installation configuration

a. Download address: Redis Download
b. In the folder saving location, open a cmd window, use the cd command to switch the directory to I:\Software\Redis-x64-3.2.100, run redis-server.exe redis.windows.conf
redis-server.exe redis.conf
c. Another cmd window is opened. Do not close it, or you will not be able to access the server.
Run redis-cli.exe -h 127.0.0.1 -p 6379
redis-cli.exe -h 127.0.0.1 -p 6379

2. Redis common commands

a. CONFIG get requirepass -- Query Redis password
b. CONFIG set requirepass "123456" -- set Redis password
c. AUTH "123456" - client connection to redis
d. config get maxclients -- query the maximum number of connections when the current service is started
e. Redis server – maxclients 100000 -- set the maximum number of connections required

3. JAVA uses Redis

a. Download the driver package: Download jedis.jar
b. Include the driver package in your classpath

(1) Connect to Redis service
import redis.clients.jedis.Jedis;
public class RedisJava {
   public static void main(String[] args) {
      //Connect to the local Redis service
      Jedis jedis = new Jedis("localhost");
      System.out.println("Successful connection");
      //See if the service is running
      System.out.println("server is running: "+jedis.ping());
 }
}
(2) Redis Java string (string)
import redis.clients.jedis.Jedis;

public class RedisStringJava {
   public static void main(String[] args) {
      //Connect to the local Redis service
      Jedis jedis = new Jedis("localhost");
      System.out.println("Successful connection");
      //Set redis string data
      jedis.set("w3ckey", "www.w3cschool.cn");
     // Get the stored data and output
     System.out.println("redis The stored string is: "+ jedis.get("w3ckey"));
 }
}
(3) Redis Java list
public class RedisListJava {
   public static void main(String[] args) {
      //Connect to the local Redis service
      Jedis jedis = new Jedis("localhost");
      System.out.println("Successful connection");
      //Store data in list
      jedis.lpush("tutorial-list", "Redis");
      jedis.lpush("tutorial-list", "Mongodb");
      jedis.lpush("tutorial-list", "Mysql");
     // Get the stored data and output
     List<String> list = jedis.lrange("tutorial-list", 0 ,2);
     for(int i=0; i<list.size(); i++) {        
       System.out.println("List item is: "+list.get(i));      
     }  
   } 
}
(4)Redis Java Keys
import java.util.Iterator;
import java.util.Set;
import redis.clients.jedis.Jedis; 

public class RedisKeyJava {
   public static void main(String[] args) {
      //Connect to the local Redis service
      Jedis jedis = new Jedis("localhost");
      System.out.println("Successful connection");

     // Get data and output
     Set<String> keys= jedis.keys("*");
     Iterator<String> it=keys.iterator();
     while(it.hasNext) {   
       String key=it.next();     
       System.out.println("key");     
     }    
   } 
} 

Tags: Redis Jedis Java Windows

Posted on Sun, 05 Apr 2020 17:04:01 -0700 by Smeep