Redis installation and the use of Java clients (Jedis) __java

Source: Internet
Author: User
Tags data structures delete key lua redis redis version install redis

NOTE: Reprint please indicate the source, thank you O (∩_∩) o~ Overview

Summary content excerpt from the official website:

Redis is an open source (BSD licensed), memory data structure storage system that can be used as a database, caching, and messaging middleware. It supports multiple types of data structures, such as strings (strings), hashes (hashes), lists (lists), collections (sets), ordered sets (sorted sets) and range queries, bitmaps, Hyperloglogs, and geo-space (GEOSP atial) index RADIUS query. Redis has built-in replication (replication), LUA scripts (Lua scripting), LRU driver events (LRU eviction), transaction (transactions) and different levels of disk persistence (persistence), and through R Edis Sentinel (Sentinel) and automatic partitioning (Cluster) provide high availability (higher availability).

You can perform atomic operations on these types, such as the Append command for a string (strings); Hash (hashes) of the Hincrby command; List (lists) of the Lpush command; The set (sets) computes the intersection sinter command, calculates the union command and calculates the difference set SDIFF command; Or get the highest rank Zrangebyscore command for a member in an ordered set (sorted sets).

To achieve its superior performance, Redis uses the data set that runs in memory to work. Depending on your usage, you can export the dataset to disk at certain times or append to the command log. You can also turn off the persistence feature and use Redis as a caching data feature for an efficient network.

Redis also supports master-slave replication (automatic resynchronization for automatic reconnect and network disconnect), and the first synchronization is a fast non-blocking test synchronization. Other features include: Transaction (transactions) subscription distribution (PUB/SUB) Lua script (LUA scripting) expired Auto Delete key memory recycle automatic failover

You can use Redis in most programming languages.

Redis is written using ANSI C and can run on a large Linux system, based on the BSD protocol, with no external dependencies on OS X. We support the development and testing of both Linux and OS x systems, and we recommend using Linux deployments. Redis can run in the Solaris system like Smartos, but we will support it with maximum strength. The official does not support the WinDOS version of Redis, but Microsoft develops and maintains a redis version that supports win-64

OK, the above is said the field, the following start combat. installation and operation of Redis

Server version of Redis installation please move: Install Redis to Ubuntu-14.04.1-server

It is recommended that the Redis,windows version of Linux be used, but not officially maintained, to be maintained and supported by Microsoft's technical team (and, of course, authoritative as the official one). )。 Here's how to install it under Ubuntu, and the other Linux versions are actually similar.

The latest version of the current Redis is 3.0.7, specific updates and other content please move to the official website to view, enter the following command:

   
   
    
    $ wget http://download.redis.io/releases/redis-3.0.7.tar.gz
    
    $ tar xzf redis-3.0.7.tar.gz
    
    $ cd redis-3.0.7
    
    $ make
   
   

The first command is to download the package, the second thing to decompress, the third is to enter the Redis directory, the fourth is compiled (c developed do not want to use Java, different platform to compile a slight difference, so will not be able to run directly to the file, of course, also give (; ' O´) O).

Run Redis with the following command:

   
   
    
    Redis-server
   
   

After a few steps, the Redis service is already running, where the configuration-free startup method is used, and the following commands are used to start with configuration:

   
   
    
    Redis-server redis.conf
   
   

Configuration of specific parameters can be modified by editing redis.conf, as to how to modify, the official website also has related documents, here only sit a simple demo, do not say more.

command line to run the command to start the service will enter the Redis start of the successful interface, we can add the "&" symbol behind the command to allow the command to run in the background for easier operation (of course, if you use Ubuntu with the desktop, you can open a terminal to do the following)

Run the client to start using Redis, enter the following command to enter the client (also check the Redis is working correctly):

   
   
    
    $redis-CLI
   
   

If you have a password to log in, you need to use the following command:

   
   
    
    Redis-cli-a Password//Connect other host Add Parameters-H host-p Port
   
   

The command line enters the ping test.

You can then add data to it, and add a string below:

   
   
    
    Set name anxpp
   
   

The successful operation command line prompts "OK" (as the Java client's set function returns), and then gets the value of the setting:

   
   
    
    Get Name
   
   

Of course, the command line output just set in the value of "anxpp", this part of the temporary introduction so much, if you need to know more, also please move to the official website O (∩_∩) o~.

Close REDIS:REDIS-CLI shutdown

Exit client: Quit using Java operations Redis

related Jar pack support

The following is the latest version of the current, click to download.

The Commons-pool2-2.4.2.jar:commons pool component provides a set of frameworks for implementing object pooling, as well as a number of distinct object pool implementations that can effectively reduce the workload of processing object pooling and leave more effort and time for other important tasks.

The Jedis-2.8.1.jar:redis client supports jar packs.

Connect and get the action object

   
   
    
    public class Redisdemo {
    
        Jedispool pool;
    
        Jedis Jedis;
    
        Public Redisdemo () {
    
            pool = new Jedispool (new Jedispoolconfig (), "192.168.40.129");
    
            Jedis = Pool.getresource ();      Jedis.auth ("password");//If password is configured
    
        }
    
    }
   
   

The following methods are added to the Redisdemo class above, and some instructions are written to the annotation, so there is no additional explanation.

Redis Stores the primary string

/** * Add a new value, if the key already exists, replace the previous value * @param key value of key * @param value to add
    
            * @return return "OK" when added successfully, otherwise return the error message */public string setstring (string key,string value) {
    
        Return Jedis.set (key, value); /** * Get value * @param key to get the value of the corresponding key * @return If the key exists, return the corresponding value, if not present, return
    
        Null/public string getString (string key) {return jedis.get (key);
    
         /** * Modify Data * @param key * For the value to be modified @param appendstr what to add
    
        * @return return the current key corresponding to the length of the string * If key does not exist, the new value added, if it exists, add to the back of the existing string * *
    
            Public long appendstring (String key,string appendstr) {Jedis.del (key); Return
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.