Redis study notes: The Basics

    A, NoSQL and relational database difference

    NoSQL non-relational databases: Redis, MongoDB, HBase like Key-Value based storage, using the operation command.

    Relational databases: Oracle, MySQL, DB2, SQL Server, etc., based on the table structure stored using SQL operations.

    Two, Redis Introduction

    Redis by Italian Salvatore Sanfilippo (screen name: antirez) the development of a memory cache database. Redis full name: Remote Dictionary Server (remote data services

Service), the software is written in C, it is typical of NoSQL database server. Redis is a Key-Value storage system that supports rich data types, such as String, Hash, List, Set,

Zset(Sorted Set)。

    Three, Redis Features

    advantage:

  • High performance: Redis can support 100,000 per second read and write frequency.
  • Support for rich data types include: String, Hash, List, Set, Zset (Sorted Set), and other data types.
  • Atomicity: all operations are atomic (succeed together or fail together).
  • Support persistence operations: synchronize the data memory into a data file.
  • It provides transaction, messaging, and other functions.

   Disadvantages:

  • Since the memory database, the amount of data stored in a single machine, depending on the memory size of the machine itself. Although Redis Key expiration policy itself, but still need to estimate in advance and save

Memory, if memory is growing too fast, you need to delete data on a regular basis.

  • If a complete re-synchronization, due to the need to generate and rdb file transfer, and therefore consume host CPU and consume network bandwidth. Although Redis 2.8 version has been part of resynchronization

Function, but there may be a complete re-synchronization, such as the new on-line backup machine.

  • After modifying the configuration file, reboot to load the data on your hard disk into memory, relatively long time. In this process, Redis can not provide services.

    Remarks:

    In the system, typically used for data caching using Redis.

    Query cache: will be removed when the first query data from the database into the cache, subsequent queries are read from the cache data.

    Add / update the cache: after detecting the user data into the cache, and in response to the user, the background data fetch processing thread turn.

    Four, Redis common data types

    Redis common data types there are five kinds, namely String, Hash, List, Set, Zset (Sorted Set):

    String-- string

    String is a simple type of Key-Value, Value can not only String, may also be a digital (when numeric types can be represented by Long encoding is an integer, are stored in the other

sdshdr as a string), the type of independent elements contained in List, Set and Zset in both Redis String type. Type String Value up to 512MB.

    Hash-- dictionary

    Hash data type is closest to a relational database structure can be a record or a program stored in the hashmap object into the database Redis. In Memcached, we

Some of the information will often packaged into structured HashMap, then the client is stored as a sequence of values ​​of a string (usually JSON format), such as the user's nickname, age and sex.

Hash keys of the number of up to 2 ^ 32-1 (4294967295) number.

    List-- list

    List means the list type (double-ended list), the main function is to push, pop, get all the values ​​of a range, of which the Key can be understood as a linked list of names. In the Redis, List on

Is Redis String list, sorted by the insertion order, such as the use LPUSH List command to insert a head element, an element used in RPUSH List command inserts the tail. When these two commands

One acts on a blank Key, a new List is created out. List of number of elements up to 2 ^ 32-1 (4294967295) number.

    Set-- collection

    Set is a collection, the collection is a combination of the concept of a bunch of distinct values. Set data structure using Redis provided, you may store some of the data set. In the Redis, Set is Redis

String unordered collection does not permit duplicates, there is the intersection of Set operations, and union, difference and so on. Set the number of elements up to 2 ^ 32-1 (4294967295) number.

    ZSet (Sorted Set) - an ordered collection

    Set and compared, Sorted Set is a Set of elements adds a weight factor score, so that the elements in the collection can be ordered by score, it has been ordered to Zset, you can still

Use the SORT command, by specifying ASC | DESC parameter to sort them. Zset the number of elements up to 2 ^ 32-1 (4294967295) number.

    Five, Redis operation command

    5.1, string manipulation

SET Key value        // set value value 
GET Key              // Get value value 
strlen Key           // Get length value 
incr Key             // will add value. 1 
incrby Key I         // the value plus I 
DECR Key             // the value Save. 1 
decrby Key I         // the value Save I 
the append key value     // the value of the original key value argument splicing

    5.2, hashing

hset key field name field value                  // set a set 
value of the field name field name field value hmset key field     // multiple sets 
hget key field name                        // get a field value 
hmget key field name field name 1 2               // Get a plurality of fields value 
hlen Key                              // get the number of fields 
hkeys Key                             // see the field name 
hdel key field name                        // delete the field name

    5.3 List operations

Key value LPUSH                            // put value from the head (key inserted in the list exist, does not exist to create a list insert) 
RPUSH Key value                            // from the tail into the value 
lpop Key                                   // remove an element from the head 
RPOP Key                                   // delete from the tail an element 
lrem Key COUNT value                       // number of elements of value equal to the value delete 
lindex Key index                           // get the value at index value 
lrange Key Start End                       // get the elements specified range 
linsert key before | after the value of the element to be inserted     // insert elements 
LSET Key index value                       //Update the location value index value 
LLEN Key                                   // get a list of the number of elements

    5.4, ​​set operations

Key value value Sadd      // add an element to the collection 
Srem Key value / / delete the value element 
SCard Key                 // get the number of elements in the collection 
smembers Key              // see all the elements 
srandmember Key count     // get random numbers count 
Sinter key1 key2          // two set intersection, key1 and key2 have 
SUNION key1 key2          // two sets and set to repeat the combined 
sdiff key1 key2           // set difference, key1 there, no key2

    5.5, an ordered set

Key value Score Zadd     // add elements to the collection 
zrem Key value           // delete elements 
zcard Key                // number of elements 
Z Range The Key Start End     // get the elements specified range (small to large) 
zrevrange Key Start End // get the elements specified range (large to small) 
zrevrank Key value       // get value index (large to small) 
zrank Key value          / / get value index (small to large)

    5.6, key operation

pattern Keys          // Check key 
del key               // delete key 
The expire key Time       // set the effective time (in seconds) 
of the type key              // determine the value of the type of value 
the rename key newkey     // modify key name

    Six, Redis file format

    Redis uses two file formats: full and incremental amount of data requested.

    Full amount of data: the data in memory is written to disk, easy to read the file is loaded next time.

    Incremental request: a data memory into a sequence operation request for reading a file replay data obtained, the sequence of operations include SET, RPUSH, SADD, ZADD.

    Note: Redis stored into memory storage, disk storage and log files of three parts, the configuration file has three parameters to configure it.

    Seven, Redis application scenarios

    (1) the session cache (Session Cache)

    (2) full-page cache (FPC)

    (3) a queue

    (4) charts / Counter

    (5) Publish / Subscribe

 

    Reference from:

    https://blog.csdn.net/weixin_39671217/article/details/78021732

Guess you like

Origin www.cnblogs.com/atomy/p/12377407.html