Introduction to Jedis API Operating Redis Database

Keywords: Redis Jedis Database Java

public void testClient() {
		jedisCli = new Jedis("127.0.0.1", 6379); //New Jedis object
		jedisCli.select(2); //Select Redis database number
		jedisCli.set("firstJedis", "hello,Jedis"); //It is basically the same as the redis command line client operation.
		System.out.println(jedisCli.get("firstJedis"));
		System.out.println(jedisCli.exists("sysPassword"));
	}

Half way home vegetable farmer, naked job search confusion; lack of knowledge of interviews, home learning to record.

I watched the video about redis on csdn some time ago. Now I'm going to learn how to operate redis API, Jedis, in a java project. The code in this article imitates all the heroes on the Internet, and there is no bright spot. I aim to record every step I take in my study.

I. Operation of Redis's library and other basic information:
1. Introduction to Redis's libraries for various language operations on the official website: https://redis.io/clients (note that the officially recommended java library is Jedis).
2. Jedis API documents found online: http://tool.oschina.net/uploads/apidocs/(English version 2.1.0, Chinese I haven't found yet).
3.Jedis package: http://files.cnblogs.com/liuling/jedis-2.1.0.jar.zip
4.Redis connection pool package: http://files.cnblogs.com/liuling/commons-pool-1.5.4.jar.zip


2. Start redis (in the case of redis locally)


Import the Jedis package and Redis connection pool:

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

3. Generation of Jedis Objects

Jedis objects have a variety of basic operations on Redis, including basic addition, deletion and modification of String, List and other objects supported by Redis (basically the same as Redis operation commands). The code is tested with JUnit:

public void testClient() {
	jedisCli = new Jedis("127.0.0.1", 6379); //New Jedis object
	jedisCli.select(2); //Switching Redis database
	jedisCli.set("firstJedis", "hello,Jedis"); //Basically consistent with Redis command line operations
}

Take Jedis's Map operation as an example:
@Test
public void testMap() {
	jedisCli = new Jedis("127.0.0.1", 6379); 
	jedisCli.select(2);
	jedisCli.hset("family", "lbq", "65"); //With the hset operation on the Redis command line, if the set named family does not exist, create the set and place the element named lbq with a value of 65
	jedisCli.hset("family", "zjz", "62"); //Redis does not support the int type and will report errors if String is not passed.
	System.out.println(jedisCli.hmget("family","lbq","zjz"));
		
	Map testMap1 = new HashMap();
	testMap1.put("num1", "1"); //Here as above, you cannot pass non-String type
	testMap1.put("num2", "15");
	testMap1.put("num3", "606");
	testMap1.put("num4", "1024");
	jedisCli.hmset("testMap1", testMap1); //Enter the entire map object into redis
	System.out.println(jedisCli.hmget("testMap1", "num1","num2","num3","num4"));
}

4. Use Redis connection pool:

Like relational databases, the use of connection pools in large-scale projects can effectively manage the use of redis connections and avoid the waste of connection resources and the impact on the database caused by untimely recovery and reuse.

The code is as follows:

1. Create new thread pool private static variables and various configuration attributes in the test class:

	private Jedis jedisCli;
	private static JedisPool pool; //Thread pool object
	
	private static String ADDR = "127.0.0.1"; 	//The server address where redis is located (in this case, native)
	private static int PORT = 6379; 		//Port number
	private static String AUTH = "";		//Password (I didn't set it)
	private static int MAX_IDLE = 10;		//Maximum number of idle connections in connection pool (maximum retention)How many free connections are there)
	private static int MAX_ACTIVE = 50;		//Maximum number of active connections (how many connections are available)
	private static int MAX_WAIT = 100000;		//Maximum time to wait for available connections(Millisecond),Default value-1,It means never timeout. If the waiting time is exceeded, then throw JedisConnectionException
	private static int TIMEOUT = 10000;		//Timeout of link pool#When using a connection, check if the connection is successful
	private static boolean TEST_ON_BORROW = true;	//When using a connection, test whether the connection is available
	private static boolean TEST_ON_RETURN = true;			//When returning to the connection, test whether the connection is available

Static block loading connection properties:

static{
	JedisPoolConfig config = new JedisPoolConfig();
	config.setMaxIdle(MAX_IDLE);
	config.setMaxActive(MAX_ACTIVE);
	config.setMaxWait(MAX_WAIT);
	config.setTestOnBorrow(TEST_ON_BORROW);
	config.setTestOnReturn(TEST_ON_RETURN);
	pool = new JedisPool(config, ADDR, PORT, TIMEOUT); //New connection pool, such as password and final parameter
}

Method of obtaining connection resources and recovering used connections:

	// Getting Connection Method
	public static Jedis getJedisResource () {
		try {
			if (pool!=null) {
				Jedis resource = pool.getResource();
				return resource;
			} 
			return null;
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	//Recycling Connection Method	
	public static void returnResource (Jedis used) {
		
		if(pool!=null){
			pool.returnResource(used);
		}
		
	}
	
}
JUnit Test Method

@Test
	public void testPool() {
		Jedis resource = getJedisResource();
		resource.select(2);
		resource.lpush("Countries", "USA");	//Insert element operation of redis to list header
		resource.lpush("Countries", "UK");
		resource.lpush("Countries", "CHINA");
		System.out.println(resource.rpop("Countries"));	//Pop-up element at the end of list
		returnResource(resource);
	}




Posted by fullyloaded on Sun, 24 Mar 2019 15:03:28 -0700