Redis+jedis Installation and use (i)

Source: Internet
Author: User
Tags redis server

steps:
1. Download window version of Redis, extract to Hard drive, start Redis server and client
Download Address: HTTP://PAN.BAIDU.COM/S/1PLRNHC3

Double-click Redis-server.exe to start the Redis server,
Double-click Redis-cli.exe to open the Redis client, which can be used to execute commands such as storage.
When the Redis server is started, the number of client connections is displayed, and the following two clients are opened for testing:

2 Download Jedis
Jedis is a Redis client based on Java, and the version I downloaded is jedis-2.7.2.jar and it's convenient to use MAVEN to manage these jar packs in real-world projects.
Download Address: HTTP://PAN.BAIDU.COM/S/1GF865L5

The following is a brief description of how to use Redis in Java
1. Direct use of Jedis
Introduce the Jedis jar package that was downloaded above in the project.

Package Com.test.jedis;
Import Redis.clients.jedis.Jedis;
public class Jedistest {public

    static void Main (string[] args) {

         Jedis jedis=new Jedis ("127.0.0.1");
         Jedis.set ("Name7", "KXL7");
         SYSTEM.OUT.PRINTLN ("Fetch data:" +jedis.get ("Name7"));
         Jedis.del ("Name7");
         System.out.println ("Delete data:" +jedis.get ("Name7"));
         Jedis.close ();

    }

Output results:

Fetching data: KXL7
Delete and fetch data: null

2. Use Jedis connection pool
Jedis use Commons-pool to complete the pooling implementation, so we need to introduce the Commons-pool jar package, I introduced the Commons-pool2-2.4.2.jar

Package com.test.jedisPool;
Import Redis.clients.jedis.Jedis;
Import Redis.clients.jedis.JedisPool;

Import Redis.clients.jedis.JedisPoolConfig;
        public class Jedispoolutil {//redis server IP private static String address = "127.0.0.1";
        Port number private static int port = 6379;
        The number of Jedis instances of the idle state in the pool pools, and the default value is 8.
        private static int max_idle = 100; When there is no return object in the pool, the maximum wait time (in milliseconds), and the default value is-1, which means never timing out.
        Timeout throw jedisconnectionexception exception private static long max_wait = 10000;
        private static int time_out = 10000;
        Maximum number of assigned connections private static int max_total=200;
        Whether validation checks are performed when the Borrow object method is invoked; private static Boolean test_on_borrow = true;
        Whether to check for validity when calling the return object method private static Boolean test_on_return=true;
        private static Jedispool Jedispool = null; /** * Initialize Redis connection pool/Static {try {jedispoolconfig config = new Jed Ispoolconfig ();
                Config.setmaxidle (Max_idle);
                Config.setmaxtotal (max_total);
                Config.setmaxwaitmillis (max_wait);
                Config.settestonborrow (Test_on_borrow);
                Config.settestonreturn (Test_on_return);
            Jedispool = new Jedispool (config, address, PORT, time_out);
            catch (Exception e) {e.printstacktrace ();
                \/** * Get Jedis instance/public static Jedis Getjedis () {try {
                    if (Jedispool!= null) {Jedis jedisresource = Jedispool.getresource ();
                return jedisresource;
                else {return null;
                } catch (Exception e) {e.printstacktrace ();
            return null;
            }/** * Free Jedis resource/public static void Close (final Jedis Jedis) {if (Jedis!= null) {jedis.close (); }
        }
}

Use:

public class Testjedispool {public
   static void Main (string[] args) {
       Jedis jedis=jedispoolutil.getjedis ();
       Jedis.set ("Name4", "Kxl4");
       SYSTEM.OUT.PRINTLN ("Fetch data:" +jedis.get ("Name4"));
  }

The static properties of the Jedispoolutil class above can be stored in a configuration file for ease of administration, and then read the configuration file in code to complete the pooling.

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.