Tutorial on using Redis non-relational database

Table of contents

1. Non-relational database

1.1. What is redis

1.2. Characteristics of redis

1.3. Understand the configuration file

1.4. Redis also provides a client interface

2. Commonly used commands in redis

2.1. Commands for key operations

2.2. Commands about the library

2.3. Redis supports common commands in data types

2.3.1. String type

2.3.2. Hash data type

2.3.3. List (list/queue) data type

2.3.4. Set data type

2.3.4, sort set data type

3. Actual development application scenarios of redis

1. Non-relational database

NoSql: [Not Only Sql is not just sql]. It is a general term for all non-relational databases. Non-relational databases do not have any relationship between them. Its data structure: key-value mode--and the data can also be persisted to disk.

Common types of non-relational databases:

  • redis: typical non-relational database
  • mogodb: It is between relational and non-relational.
  • hbase: database [massive data]

1.1. What is redis

Redis is an open source (BSD licensed) in-memory data structure store used as a database, cache, and message broker. Redis provides data structures such as strings, hashes, lists, sets, sorted collections with range queries, bitmaps, hyperlogs, geospatial indexes, and streams. Redis has built-in replication, Lua scripts, LRU eviction, transactions and different levels of disk durability, and provides high availability through Redis Sentinel and Redis Cluster automatic partitioning.

Redis is an open source database and cache server. It can store data of string, hash, list, and set data types. Its calculation is based on memory. And the data can also be persisted to disk. And it also improves sentinel mode and cluster mode to improve the availability of redis.

1.2. Characteristics of redis

  1. The reading speed of Redis is 110,000 times/s and the writing speed is 81,000 times/s.
  2. atom. All operations of Redis are atomic, and Redis also supports atomic execution of several operations after they are fully merged.
    ---Factors affecting redis performance: memory rather than multi-threading.
  3. Supports multiple data structures: string (string); list (list); hash (hash), set (set); zset (ordered set).
  4. Persistence--disk--prevents data loss.
  5. It does not officially support window systems, but there are third-party versions. linux system.

1.3. Understand the configuration file

Enter the redis.conf configuration file and you can see:

Line 259 daemonize no: Set whether the redis service is a daemon process when starting. Line
98 port 6379: Modify the port number of the redis service. Line
75 bind * -::* : Set which IPs are allowed to connect to the redis server.

1.4. Redis also provides a client interface

redisplus download address: Link: http:// https://pan.baidu.com/s/1GjNFwbmksA1JhguHgcAcSQ extraction code: 3pcm or directly download the file at the top of the directory

Note: Remember to change line 94 in the redis.conf configuration file to peotected-mode no and line 75 to bind * -::* to set the redis server to allow any IP connection.

2. Commonly used commands in redis

Specific reference: redis command manual

                 Redis command reference — Redis command reference

2.1. Commands for key operations

  • keys * : View all keys in redis
  • exists key : Determine whether the specified key exists. Returns 1 if exists, otherwise returns 0
  • del key : delete the specified key
  • expire key seconds : Set the expiration time for the specified key.

2.2. Commands about the library

There are 16 libraries in redis by default.

  • select n : Select library n=0~15
  • flushdb : Clear the current database
  • flushall : Clear all database data  

2.3. Redis supports common commands in data types

There are many data types stored in redis, and the ones we use the most are: string type, Hash type, List queue type, Set collection type, SortSet ordered collection type.

2.3.1. String type

The format for redis to store data: key--value format. The keys here are all string types, and the types we are talking about are supported by redis. Refers to the data type of value. The storage size is 512M.

Commonly used commands are as follows:

  • set key value : stores the value of string type.
  • get key : Get the corresponding value string value based on key.
  • mset key value key value ... : stores multiple string type values.
  • mget key key key... : Get the value string values ​​corresponding to multiple keys.
  • setnx key value : If the specified key exists, it will not be saved. If it does not exist, it will be saved.
  • incr key : Increment the value of the specified key. The value corresponding to the key must be an integer string.
  • decr key : Decrement the value of the specified key.
  • incrby key number : increment according to the number value.

2.3.2. Hash data type

The value it stores is a hash type, and the data structure of the hash type is in the form of key value. Generally used to store object data.

Commonly used commands are as follows:

  • hset key field value [field value] : Set the value of the field field in the hash table key to value
  • hget key field : Get the value of the specified field stored in the hash table.
  • hgetall key : Get all fields and values ​​of the specified key in the hash table
  • hkeys key : Get all fields in the hash table
  • hvals key : Get all values ​​in the hash table
  • hdel key field : delete one or more hash table fields

2.3.3. List (list/queue) data type

The value it stores is a List data type. The value can be multiple, ordered, and repeatable.

Commonly used commands are as follows:

  • lpush key element [element...] : Add one or more values ​​to the list
  • Lindex key index : Get the element at the specified index in the list.
  • lrange key start end : Get a certain range of elements. The first one is 0 and the last one is -1
  • lpop key : Remove the first element on the left
  • lset key index element : Replace the element content at the specified position

2.3.4. Set data type

It is similar to the list type, except that its values ​​are not allowed to be repeated and are unordered.

Commonly used commands are as follows:

sadd key element[element....] : Add one or more values ​​to the collection
smembers key : Get all elements in the collection.
sinter key1 key2 : Returns the intersection of all given sets
sdiff key1 key2 : Returns the difference of all given sets

2.3.4, sort set data type

It is similar to set. When adding elements, it specifies a value and sorts by value. Used in rankings, etc.

Commonly used commands are as follows:

  •  zadd key score element [score element ...] : Add ordered set elements
  •  zrange key start end [withscopes] : Get the elements in the collection from small to large
  •  zrevrange key start end [withscopes] : Get the elements in the collection from large to small
  •  zrem k1 element [element] : Remove one or more elements from the collection

3. Actual development application scenarios of redis

1. Caching of hot data: Reduce the frequency of access to the database and reduce the pressure on the database.
2. Application of limited-time services: flash sale of products, storage of user information of logged-in users, storage of SMS verification codes
3. Issues related to counters: number of likes, number of favorites, number of plays.
4. Questions related to rankings: sort set 
5. Distributed lock: ---Synchronization lock: 
6. Limited flash sale: ---decr key:

Guess you like

Origin blog.csdn.net/weixin_54065960/article/details/131482497