Installation and use of Redis under Linux-CentOs7

table of Contents

Introduction

installation

Mirror download

Unzip

 Compile

 installation

start up

Console

Close service

Using Redis in Java

Connect Redis service

Data types and basic commands supported by Redis

Redis extension article

    ​


 

Introduction

Redis :

       REmote DIctionary Server (Redis) is a key-value storage system written by Salvatore Sanfilippo. Redis is an open source log-based, Key-Value database that is written in ANSI C language, complies with the BSD protocol, supports the network, and can be memory-based or persistent, and provides APIs in multiple languages. It is usually called a data structure server because the value can be of string (String), hash (Hash), list (list), set (sets) and sorted sets (sorted sets) and other types.

This blog post is for professional program developers. Through the series of blogs, you can learn about the application of Redis step by step. (The series of blog posts are at the end)

 

installation

Mirror download

cd /usr/local/  #直接切换到想要安装的目录下

(Pay attention to switch to the root directory to facilitate subsequent use operations and avoid switching directories back and forth)

wget http://download.redis.io/releases/redis-4.0.2.tar.gz

(Replace yourself with the required version, this example uses redis-4.0.2)

Wait for the download to complete

 

Unzip

tar -zvxf redis-4.0.2.tar.gz

 (Correspond to be modified to your version number)

 Here you can see that it has been unzipped to the redis-4.0.2 folder in the current directory

rm redis-4.0.2.tar.gz #这里我习惯将压缩包清理掉,避免占用过多磁盘

 Compile

make

 installation

make install

(If the command is not executed, it will cause) 

start up

./bin/redis-server  #通过文件启动redis服务
./redis.conf  #通过配置文件启动redis

 

Console

redis-cli   # 切入脚本控制台

   

exit   #退出控制台

(Exit the script console)

Close service

shutdown  #在redis控制台中输入

Using Redis in Java

Connect Redis service

import redis.clients.jedis.Jedis;
 
public class RedisJava {
    public static void main(String[] args) {
        //连接本地的 Redis 服务
        Jedis jedis = new Jedis("localhost");
        // 如果 Redis 服务设置来密码,需要下面这行,没有就不需要
        // jedis.auth("123456"); 
        System.out.println("Connect Succeed!");
        //查看服务是否运行
        System.out.println("服务正在运行: "+jedis.ping());
    }
}

Data types and basic commands supported by Redis

Different data types have different redis commands when storing and reading. You only need to understand than master the actual application.

  • Redis string (String)
redis 127.0.0.1:6379> SET Adam redis
OK
redis 127.0.0.1:6379> GET Adam 
"redis"

 Basic commands:

  • SET key value Set the value of the specified key
  • GET key Get the value of the specified key.
  • GETRANGE key start end returns the subcharacter of the string value in the key
  • GETSET key value sets the value of the given key to value and returns the old value of the key.
  • GETBIT key offset obtains the bit at the specified offset for the string value stored in the key.
  • MGET key1 [key2..] Get all (one or more) values ​​of a given key.
  • SETBIT key offset value sets or clears the bit at the specified offset to the string value stored in the key.
  • SETEX key seconds value associates the value value with the key, and sets the expiration time of the key to seconds (in seconds).
  • SETNX key value can only set the value of the key when the key does not exist.
  • SETRANGE key offset value Overwrites the string value stored in the given key with the value parameter, starting from the offset offset.
  • STRLEN key returns the length of the string value stored in the key.
  • MSET key value [key value ...] Set one or more key-value pairs at the same time.
  • MSETNX key value [key value ...] Set one or more key-value pairs at the same time, if and only if all the given keys do not exist.
  • PSETEX key milliseconds value This command is similar to the SETEX command, but it sets the lifetime of the key in milliseconds instead of seconds as the SETEX command.
  • INCR key increments the numeric value stored in the key by one.
  • INCRBY key increment adds the value stored in the key to the given increment value (increment).
  • INCRBYFLOAT key increment adds the value stored in the key to the given floating point increment value (increment).
  • DECR key reduces the numeric value stored in the key by one.
  • DECRBY key decrement key The stored value minus the given decrement value (decrement).
  • APPEND key value If the key already exists and is a string, the APPEND command will append the specified value to the end of the original value of the key.
  • Redis hash (Hash)

Each hash in Redis can store 232-1 key-value pairs (more than 4 billion), suitable for storing objects.

127.0.0.1:6379>  HMSET Adam  "sex:man"  "age:25" 
OK
127.0.0.1:6379>  HGETALL Adam  
1) "sex:man"
2) "age:25" 

Basic commands:

  • HDEL key field1 [field2] Delete one or more hash table fields
  • HEXISTS key field Check whether the specified field exists in the hash table key.
  • HGET key field Gets the value of the specified field stored in the hash table.
  • HGETALL key Get all the fields and values ​​of the specified key in the hash table
  • HINCRBY key field increment is the integer value of the specified field in the hash table key plus the increment increment.
  • HINCRBYFLOAT key field increment is the floating point value of the specified field in the hash table key plus the increment increment.
  • HKEYS key Get all fields in the hash table
  • HLEN key Get the number of fields in the hash table
  • HMGET key field1 [field2] Get all the values ​​of a given field
  • HMSET key field1 value1 [field2 value2] Simultaneously set multiple field-value (domain-value) pairs into the hash table key.
  • HSET key field value Sets the value of the field field in the hash table key to value.
  • HSETNX key field value Set the value of the hash table field only when the field field does not exist.
  • HVALS key Get all the values ​​in the hash table.
  • HSCAN key cursor [MATCH pattern] [COUNT count] Iterate over the key-value pairs in the hash table.
  • Redis list (List)

The Redis list is a simple list of strings, sorted in the order of insertion. You can add an element to the head (left) or tail (right) of the list. A list can contain up to 2^32-1 element 

redis 127.0.0.1:6379> LPUSH Adamkey redis
(integer) 1
redis 127.0.0.1:6379> LPUSH Adamkey  mongodb
(integer) 2
redis 127.0.0.1:6379> LPUSH Adamkey mysql
(integer) 3
redis 127.0.0.1:6379> LRANGE Adamkey 0 5

1) "mysql"
2) "mongodb"
3) "redis"

 Basic commands:

  • BLPOP key1 [key2] timeout Move out and get the first element of the list. If there are no elements in the list, the list will be blocked until the waiting timeout or a pop-up element is found.
  • BRPOP key1 [key2] timeout Move out and get the last element of the list. If there are no elements in the list, the list will be blocked until the waiting timeout or an element that can be popped is found.
  • BRPOPLPUSH source destination timeout pops a value from the list, inserts the popped element into another list and returns it; if there are no elements in the list, the list will be blocked until the waiting timeout or a pop-up element is found.
  • LINDEX key index Get the elements in the list by index
  • LINSERT key BEFORE|AFTER pivot value Insert elements before or after the elements of the list
  • LLEN key Get the length of the list
  • LPOP key moves out and gets the first element of the list
  • LPUSH key value1 [value2] Insert one or more values ​​into the head of the list
  • LPUSHX key value inserts a value into the head of an existing list
  • LRANGE key start stop Get the elements in the specified range of the list
  • LREM key count value Remove list elements
  • LSET key index value Set the value of the list element by index
  • LTRIM key start stop trims a list, that is, lets the list retain only the elements in the specified interval, and the elements not in the specified interval will be deleted.
  • RPOP key removes the last element of the list, and the return value is the removed element.
  • RPOPLPUSH source destination removes the last element of the list, and adds the element to another list and returns
  • RPUSH key value1 [value2] Add one or more values ​​to the list
  • RPUSHX key value adds value to an existing list
  • Redis collection (Set)

Redis's Set is an unordered collection of String type. Set members are unique, and Redis set is implemented through a hash table, so the complexity of adding, deleting, and searching is O(1).

redis 127.0.0.1:6379> SADD Adamkey redis
(integer) 1
redis 127.0.0.1:6379> SADD Adamkey mongodb
(integer) 1
redis 127.0.0.1:6379> SADD Adamkey mysql
(integer) 1
redis 127.0.0.1:6379> SADD Adamkey mysql
(integer) 0
redis 127.0.0.1:6379> SMEMBERS Adamkey 

1) "mysql"
2) "mongodb"
3) "redis"

Basic commands:

  • SADD key member1 [member2] Add one or more members to the collection
  • SCARD key to get the number of members of the collection
  • SDIFF key1 [key2] returns the difference between the first set and the other sets.
  • SDIFFSTORE destination key1 [key2] Returns the difference of all the given sets and stores them in the destination
  • SINTER key1 [key2] returns the intersection of all given sets
  • SINTERSTORE destination key1 [key2] Returns the intersection of all the given sets and stores them in the destination
  • SISMEMBER key member determines whether the member element is a member of the set key
  • SMEMBERS key returns all members in the collection
  • SMOVE source destination member moves the member element from the source collection to the destination collection
  • SPOP key removes and returns a random element in the set
  • SRANDMEMBER key [count] Returns one or more random numbers in the set
  • SREM key member1 [member2] Remove one or more members from the set
  • SUNION key1 [key2] returns the union of all given sets
  • SUNIONSTORE destination key1 [key2] The union of all given sets is stored in the destination set
  • SSCAN key cursor [MATCH pattern] [COUNT count] Iterate over the elements in the collection
  • Redis ordered set (SortSet)

Redis ordered collections are also collections of string type elements like collections, and duplicate members are not allowed. The difference is that each element is associated with a double type score. Redis uses scores to sort the members of the set from small to large. The members of the ordered set are unique, but the score can be repeated.

The collection is implemented through a hash table, so the complexity of adding, deleting, and searching is O(1). The maximum number of members in the set is 232-1 (4294967295, each set can store more than 4 billion members).

redis 127.0.0.1:6379> ZADD Adamkey 1 redis
(integer) 1
redis 127.0.0.1:6379> ZADD Adamkey 2 mongodb
(integer) 1
redis 127.0.0.1:6379> ZADD Adamkey  3 mysql
(integer) 1
redis 127.0.0.1:6379> ZADD Adamkey 3 mysql
(integer) 0
redis 127.0.0.1:6379> ZADD Adamkey 4 mysql
(integer) 0
redis 127.0.0.1:6379> ZRANGE runoobkey 0 10 WITHSCORES

1) "redis"
2) "1"
3) "mongodb"
4) "2"
5) "mysql"
6) "4"
  • ZADD key score1 member1 [score2 member2] Add one or more members to an ordered set, or update the scores of existing members
  • ZCARD key Get the number of members in an ordered set
  • ZCOUNT key min max Calculates the number of members with a specified interval score in an ordered set
  • ZINCRBY key increment member The score of the specified member in the ordered set adds the increment increment
  • ZINTERSTORE destination numkeys key [key ...] Calculate the intersection of one or more given ordered sets and store the result set in a new ordered set destination
  • ZLEXCOUNT key min max calculates the number of members in the specified dictionary interval in an ordered set
  • ZRANGE key start stop [WITHSCORES] Return the members in the specified range of the ordered set through the index range
  • ZRANGEBYLEX key min max [LIMIT offset count] Returns the members of an ordered set through a dictionary interval
  • ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] Returns the members in the specified interval of the ordered set by score
  • ZRANK key member returns the index of the specified member in the ordered set
  • ZREM key member [member ...] Remove one or more members from an ordered set
  • ZREMRANGEBYLEX key min max removes all members of the given dictionary range in the ordered set
  • ZREMRANGEBYRANK key start stop Remove all members of the given ranking range in the ordered set
  • ZREMRANGEBYSCORE key min max removes all members of the given score interval in the ordered set
  • ZREVRANGE key start stop [WITHSCORES] Returns the members in the specified interval in the ordered set, and the scores are from high to low by index
  • ZREVRANGEBYSCORE key max min [WITHSCORES] Returns the members in the specified score range in an ordered set, sorted from high to low
  • ZREVRANK key member returns the ranking of the specified member in the ordered set, the members of the ordered set are sorted by decreasing points (from large to small)
  • ZSCORE key member returns the score value of the member in an ordered set
  • ZUNIONSTORE destination numkeys key [key ...] Calculate the union of the given one or more ordered sets and store it in the new key
  • ZSCAN key cursor [MATCH pattern] [COUNT count] Iterate over elements in an ordered set (including element members and element scores)

 

Redis extension article

A blog of Redis, an interviewer who is no longer imaginary after reading it (super full detailed graphic interpretation)

Super detailed explanation of Redis (8 types) data elimination strategy

Still checking Redis cache avalanche & cache penetration & cache breakdown (super detailed graphic interpretation)

Redis: The perfect solution for Serializable exceptions

 

 

Any restrictions start from the heart!

When you no longer endure, no longer restraint, you will truly mature!

When doing anything, you must have firm and clear goals, and keep your goals in mind!

 

    

 

The editor needs your attention! Your praise is my greatest encouragement! Collection code live! Live!

Guess you like

Origin blog.csdn.net/l_mloveforever/article/details/111823209