01Redis6-5 multiple data structure

1, Redis installation

(1) Install GCC first

yum install gcc

(2) Unzip, - C /opt/module / and install it in the model directory. It's ok if you don't create the model directory

tar -zxvf redis-6.2.1.tar.gz -C /opt/module/

(3) Enter the decompressed redis-6.2.1 directory and execute the command: make() is to compile the current redis file

(4) Then execute: make install. If you are an ordinary user, use: sudo make install

(5) It is installed in the / uer/local/bin directory by default. The detailed files are as follows:

  • Redis server: redis server startup command
  • Redis cli: client operation
  • Redis benchmark: a performance testing tool that can be run locally to see the performance
  • Redis check AOF: fix the problematic AOF file
  • redis-check-rdb
  • Redis sentinel: redis cluster

(6) Start

  • Start the foreground and run it directly: redis server (rarely used)
  • Background startup (common)
    • Step 1: back up redis.conf, which is in the directory of your custom installation redis-6.2.1
    • Step 2: copy here to sudo cp redis.conf /etc/redis.conf
    • Step 3: vi redis.conf set the daemonized no to yes
    • Step 4: start redis with the file just modified, enter the default installation location, and execute the startup command under / uer/local/bin: redis server / etc / redis.conf
    • Step 5: view the process ps -ef | grep redis

(7) Shut down

Single instance shutdown: redis cli shutdown

Directly in the terminal: shutdown

Close multiple instances and specify the port: redis cli - P 6379 shutdown

(8) Set profile

  • For network related configuration, comment out this line: #bind = 127.0.0.1 (by default, it can only be accessed locally, not remotely)
  • Set protected mode No
  • To set the password, remove # requirepass foobared. Foobared is the password. You can set 123456
  • After you start redis again and enter the client, you need to verify: auth "the password you set"
  • Configure log: touch /user/local/bin/redis-log.log, and change the permission to sudo chmod 666 redis-log.log

(9) rdm tool connection

2, Redis -- five basic data structures

Redis has five common data structures: String * *, Hash, List, Set and * * SortedSet

Redis key

  • View all keys: Keys*
  • Judge whether a key exists: exists [name of key]
  • Set expiration time for key: expire key [seconds]
  • Check how much time the key has to expire: ttl [key], - 1 means it will never expire, and - 2 means it has expired
  • Check the type of key: type key
  • Delete key command: del [key to be deleted]
  • Empty database: flushdb

String

1. Introduction

String type is the most basic data structure of Redis. Keys and values are of string type, and several other data structures are built on the basis of string type, so it's almost easy to learn string type well. String can understand as like as two peas of Memcached, and a key corresponds to a value. The string type is binary safe and can contain simple strings, complex strings, JSON, XML and numbers, but the string value can be 512M at most.

2. Common commands

(1)set/get/append/strlen

(2) Incr/decr/Incrb/decrby: it must be a number to add or subtract

  • The value of the key is incremented by + 1. The command format is incr K1 (the key that needs to be incremented by itself)
  • The value of the key is minus - 1. The command format is decr K1 (the key that needs to be minus)
  • The value of the key is incremented by + n, and the command format is incrby K1 2 (for the key that needs to be incremented, 2 is incremented by 2 each time)
  • The value of the key is minus-n, and the command format is decrby K1 2 (for the key that needs to be added automatically, 2 is minus 2 each time)

(3)getrange/setrange

  • Get the value of the specified interval, similar to the relationship between... and: getrange key 0 2 (0-2 means I want to get the value between 0-2, which can be obtained at both ends)
  • Set the value of the specified interval range in the format of setrange key value (for example, setrange k3 0 888 means that it is set to 888888 from bit 0, accounting for 3 bits)

(4)setex(set with expire)/setnx(set if not exist)

  • Key second value, meaning adding a k1=23, which is now set to 20 and becomes invalid: setex k1 20 23 (as long as it exceeds 20 seconds, all keys and values become invalid)

(5)mset/mget/msetnx

  • mset: set multiple keys and values at the same time. The common set error is as follows

  • mget: get multiple values

    127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3
    OK
    127.0.0.1:6379> keys *
    1) "k1"
    2) "k2"
    3) "k3"
    127.0.0.1:6379> set k1 v1 k2 v2 k3 v3
    (error) ERR syntax error
    # Get multiple values
    127.0.0.1:6379> mget k1 k2 k3
    1) "v1"
    2) "v2"
    3) "v3"
    

Summary: String operation and usage scenarios

Common string operations:

Set key value / / store a string key value pair

Mset key1 value1 key1 value1... / / batch store string key value pairs

Setnx key value / / store a nonexistent string key value pair

GET key / / get a string key value pair

MGET key1 key2... / / get string key value pairs in batch

DEL key1 / / delete a key

Expire key seconds / / sets a key expiration time (seconds), such as expire key 10

Atomic addition and subtraction

INCR key / / add the numeric value stored in the key + 1

DECR key / / set the numeric value stored in the key to - 1

INCRBY key increment / / add increment to all stored values of key

Decrby key increment / / subtract increment from all stored values of key

List

(1)Lpush/rpush/Lrange

  • Lpush basic usage: lpush list 0 1 2 3 4 5
  • Lrange basic usage: lrange list 0 -1

(2)Lpop/rpop

  • Lpop: first in and last out (lpop list is taken out one by one)
  • rpop: take out is first in first out

(3) lindex, get elements according to index subscript

(4) Len get length: len list

(5)rpoplpush

127.0.0.1:6379> lrange list 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
5) "0"
6) "-1"
127.0.0.1:6379> lrange list2 0 -1
1) "10"
2) "9"
3) "8"
127.0.0.1:6379> rpoplpush list list2
"-1"
127.0.0.1:6379> lrange list2 0 -1
1) "-1"
2) "10"
3) "9"
4) "8"

(6) insert key before/after value 1 value 2; Insert a value at a location

Set

(1)sadd/smembers/sismember

127.0.0.1:6379> sadd set0 1 2 3 4 5
127.0.0.1:6379> smembers set0
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379>
// Returns 1 if there is one, and 0 if there is no one
127.0.0.1:6379> sismember set0 x
(integer) 0
127.0.0.1:6379> sismember set0 3
(integer) 1
127.0.0.1:6379>

(2) scard, get the number of elements in the collection

127.0.0.1:6379> scard set0
(integer) 5

(3) srem key value delete collection elements

127.0.0.1:6379> srem set0 4
(integer) 1
127.0.0.1:6379> sismember set0 4
(integer) 0

(4) srandmember key n (if n=3, then 3 numbers are randomly selected)

127.0.0.1:6379> sadd set1 1 2 3 4 5 6 7 8 9 10 11
(integer) 11
127.0.0.1:6379> srandmember set1 5
1) "6"
2) "7"
3) "1"
4) "8"
5) "5"
127.0.0.1:6379> srandmember set1 2
1) "1"
2) "9"
127.0.0.1:6379>

(5) Pop key random stack

127.0.0.1:6379> spop set1
"11"

(6) smoke key1 key2 has a value in key1, which is used to assign a value in key1 to key2

(7) Mathematical set

  • Difference sets: sdiff
  • Intersection: sinter
  • Union: sunion

Hash

A little special, but commonly used: KV mode remains unchanged, but V is a key value pair

(1)hset/hget/hmset/hmget/hgetall/hdel

  • Hset/hget: set and get

    127.0.0.1:6379> hset user id 11
    (integer) 1
    127.0.0.1:6379> hget user id
    "11"
    
  • Hmset/hmget: set multiple and get multiple

    127.0.0.1:6379> hmset customer id 11 name 12 age 23
    OK
    127.0.0.1:6379> hmget customer id name  age
    1) "11"
    2) "12"
    3) "23"
    127.0.0.1:6379>
    
  • Hgetall is more convenient

    127.0.0.1:6379> hgetall customer
    1) "id"
    2) "11"
    3) "name"
    4) "12"
    5) "age"
    6) "23"
    
  • hdel delete

  • hlen get length

(2) exists key is the key of a value in the key

(3)hkeys / hvals

127.0.0.1:6379> hkeys customer
1) "id"
2) "name"
3) "age"
127.0.0.1:6379> hvals customer
1) "11"
2) "12"
3) "23"

(4)hincrby / hincrbyfloat

(5) hsetnx (if you have it, you can't plug it in, if you don't have it, you can)

Sorted set(ZSet)

On the basis of set, add a score value. Previously, set was k1 v1 k2 v2, and now zset is k1 score v1 score v2

(1) zadd/zrang / (add / get)

Zrange zset01 0 - 1 WithCores: Yes to get all

zrange zset01 0 -1: get only v1 ~ v5

127.0.0.1:6379> zadd zset01 10  v1 20 v2 30 v3 40 v4 50 v5 60 v6
(integer) 6
127.0.0.1:6379> zrange zset01 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
5) "v5"
6) "v6"
127.0.0.1:6379> zrange zset01 0 -1 withscores
 1) "v1"
 2) "10"
 3) "v2"
 4) "20"
 5) "v3"
 6) "30"
 7) "v4"
 8) "40"
 9) "v5"
10) "50"
11) "v6"
12) "60"

(2) zrangebyscore key start score end score

Obtain a value between 60 and 90

127.0.0.1:6379> zrangebyscore zset01 60 90
1) "v1"
2) "v2"
3) "v3"
4) "v4"

Plus (not included)

127.0.0.1:6379> zrangebyscore zset01 (60 (90
1) "v2"
2) "v3"

limit get part

127.0.0.1:6379> zrangebyscore zset01 60 90 limit 2 2
1) "v3"
2) "v4"

(3) zrem key is the value value corresponding to a score. It is used to delete elements

127.0.0.1:6379> zrem zset01 v5
(integer) 1

(4) zcard / zcount key score interval / zrank key values is used to obtain the corresponding value of subscript value / zscore key

127.0.0.1:6379> zrange zset01 0 -1 withscores
 1) "v1"
 2) "60"
 3) "v2"
 4) "70"
 5) "v3"
 6) "80"
 7) "v4"
 8) "90"
 9) "v5"
10) "100"
# zcard statistics all
127.0.0.1:6379> zcard zset01
(integer) 4
# How many people are there in 60 ~ 80
127.0.0.1:6379> zcount zset01 60 80
(integer) 3
# zrank get subscript
127.0.0.1:6379> zrank zset01 v4
(integer) 3
127.0.0.1:6379> zscore zset01 v3
"80"

(5) zrevrank key values is used to obtain subscript values in reverse order

127.0.0.1:6379> zrevrank zset01 v4
(integer) 0
127.0.0.1:6379> zrevrank zset01 v3
(integer) 1

(6)zrevrange

127.0.0.1:6379> zrevrange zset01 0 -1
1) "v4"
2) "v3"
3) "v2"
4) "v1"
127.0.0.1:6379>

(7) zrevrangebyscore key result score start score negation

127.0.0.1:6379> zrevrangebyscore zset01 90 60
1) "v4"
2) "v3"
3) "v2"
4) "v1"
127.0.0.1:6379>

More articles have been included by Github: https://github.com/niutongg/JavaLeague

Tags: Database Redis data structure

Posted on Tue, 21 Sep 2021 23:07:42 -0400 by BDabrowski