First of all, a brief introduction to Jedis, in fact, a word can be summed up, is a Java operation of the Redis API. We know that Redis provides basically all the common programming languages clients, you can go to http://redis.io/clients above to see, including c,c++,c# and so on.
1, download Jedis Source: https://github.com/xetorthio/jedis/releases/tag/jedis-2.1.0, Jedis uses the git escrow, this side uses is 2.1.0 version;
2, decompression open can see, Jedis use is Maven construction project, so our development tools best can support Maven project, about MAVEN project support, this is not introduced, we can go online to find, or see I wrote about Maven blog, The reason to choose Maven Project, is to better to see the source of Jedis.
3, Eclipse import Jedis Project, in the package Exploer right click Import, select Maven Project
Click Finish to complete the import work, the project structure is as follows:
4. Complete basic Connection operation
Modify the Pom.xml file to change localhost here to its own Redis server IP
<properties> <redis-hosts>192.168.2.105:6379,192.168.2.105:6380 </redis-hosts></properties>
Create the Redisclient class: (I am accustomed to the usual packaging)
Package com.enson.redis.client;Import com.enson.redis.common.Constant;Import Redis.clients.jedis.Jedis;public class redisclient {public static Jedis Jedis = null; Public static Jedis getclient () { if (Jedis = = null) { Jedis = New Jedis (Constant.host, Constant.port); } return Jedis;} }
Constant class:
Package Com.enson.redis.common; Constant {//redis host IP address "192.168.2.105"; Redis host Port 6379;}
Create a new JUnit test case:redistest below the Src/test/java
Package Test.enson.redis;Import Org.junit.AfterClass;Import Org.junit.BeforeClass;Import Org.junit.Test;Import com.enson.redis.client.RedisClient;PublicClassredistest { @BeforeClass Publicstatic void setupbeforeclass () throws Exception { } @AfterClass public static void teardownafterclass () throws Exception {} @ Test public void test () { Span class= "indent" > System.out.println (Redisclient.getclient (). Set ( "123456"); System.out.println (Redisclient.getclient (). Get ( "Key")); } /span>
At this moment, the test code has been written, open Redis-server
JUnit mode to run the test method:
Using Jedis to operate Redis