Using Jedis to operate Redis

Source: Internet
Author: User
Tags redis server

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.