Redis supports clients in many languages. A list of all supported Redis clients is available on the official website.

Since Java is usually used as the development language, this section describes how to connect and operate the Redis server through Java. In the official documentation, Java recommended Redis client is Jedis, here we also use this client to operate on the Redis server.


Introduction of depend on

First, we set up a Maven project and add Jedis dependencies to the project’s POM.xml file. Junit dependencies have also been added for testing purposes. The file content is as follows.

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < groupId > com.x9710.com mon < / groupId > < artifactId > redis - util < / artifactId > < version > 1.0 - the SNAPSHOT < / version > < dependencies > < the dependency > < groupId > Commons - logging < / groupId > <artifactId> Commons -logging</artifactId> </version> 1.1.1</version> </dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <groupId>junit</groupId> </dependencies> </dependencies> </dependencies> </dependencies> </dependencies> </dependencies> </dependencies>Copy the code

Creating a connection class

Redis connection class com.x9710.com mon. Redis. RedisConnection. The contents are as follows: package com.x9710.common.redis;

import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; Public class RedisConnection {/** * Redis connection pool configuration */ private JedisPoolConfig JedisPoolConfig; /** * redis server address */ private String IP; /** * redis server port */ private Integer port; /** * redis server password */ private String PWD; /** * redis server connection timeOut */ private Integer timeOut; Private String clientName = null; /** * redis clientName */ private String clientName = null; private JedisPool jedisPool; public void setJedisPoolConfig(JedisPoolConfig jedisPoolConfig) { this.jedisPoolConfig = jedisPoolConfig; } public void setIp(String ip) { this.ip = ip; } public void setPort(Integer port) { this.port = port; } public void setPwd(String pwd) { this.pwd = pwd; } public void setTimeOut(Integer timeOut) { this.timeOut = timeOut; } public void setClientName(String clientName) { this.clientName = clientName; } private void buildConnection() { if (jedisPool == null) { if (jedisPoolConfig == null) { jedisPool = new JedisPool(new  JedisPoolConfig(), ip, port, timeOut, pwd, 0, clientName); } else { jedisPool = new JedisPool(jedisPoolConfig, ip, port, timeOut, pwd, 0, clientName); } } } public Jedis getJedis() { buildConnection(); if (jedisPool ! = null) { return jedisPool.getResource(); } return null; }Copy the code

}


Write the test

With a test class com.x9710.com mon. Redis. Test. RedisConnectionTest to test the rdis connection.

package com.x9710.common.redis.test; import com.x9710.common.redis.RedisConnection; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPoolConfig; public class RedisConnectionTest { private RedisConnection redisConnection; @Before public void before() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); / / set a maximum number of connections for redis connection pool jedisPoolConfig. SetMaxTotal (50); / / set the maximum number of idle connections jedisPoolConfig redis connection pool. SetMaxIdle (10); / / set the minimum number of idle connections jedisPoolConfig redis connection pool. SetMinIdle (1); redisConnection = new RedisConnection(); 10.110.2.56 redisConnection. SetIp (" "); redisConnection.setPort(52981); redisConnection.setPwd("hhSbcpotThgWdnxJNhrzwstSP20DvYOldkjf"); redisConnection.setClientName(Thread.currentThread().getName()); redisConnection.setTimeOut(600); redisConnection.setJedisPoolConfig(jedisPoolConfig); } @Test public void testPutGet() { Jedis jedis = redisConnection.getJedis(); try { jedis.select(1); jedis.set("name","grace"); Assert.assertTrue("grace".equals(jedis.get("name"))); } finally { if (jedis ! = null) { jedis.close(); }}}}Copy the code

Executing the test case in the IDE environment results in the following.

Now we can connect to Redis in Java using the Jedit client and perform actions. The corresponding code is published on GitHub

The original was published in a brief book,The original link