This is the 10th day of my participation in the August More Text Challenge. For details, see:August is more challenging

🌈 past review

Thank you for reading, I hope it can help you, if there are flaws in the blog, please leave a message in the comment area or add my private chat in the profile of the home page, thank you for your advice. I’m XiaoLin, a man who writes bugs and raps

  • Does Git really mean Pull and Push? ❤
  • How do you get 20K? ❤
  • Do you want to take a look at SpringBoot?

A, no

1.1 NoSQL Introduction

NoSQL(Not Only SQL), meaning Not Only SQL, refers to non-relational databases. Nosql is a technology category that has been mentioned in the early days and is gaining momentum in 2009.

Why NoSQL

With the rise of The Internet website, the traditional relational database has been unable to cope with the dynamic website, especially the pure dynamic website with large scale and high concurrency, and exposed a lot of insurmountable problems. Such as shopping mall website for commodity data frequent query, ranking statistics of hot search goods, order timeout problem, and wechat circle of friends (audio, video) storage related to the use of traditional relational database implementation is very complex, although can achieve the corresponding function but the performance is not so optimistic. Nosql is a class of technologies that solves these problems better and shows the world that there is more to it than JUST SQL.

1.3 The four categories of NoSQL

1.3.1 Key-value Storage database

1.3.1.1, description,

This type of database mainly uses a hash table with a specific key and a pointer to a specific data.

1.3.1.2, characteristics

  1. The advantage of the Key/ Value model for IT systems is that IT is simple and easy to deploy.
  2. Key/value is inefficient if the DBA only queries or updates partial values.

1.3.1.3 Related Products

  1. Tokyo Cabinet/Tyrant,
  2. Redis
  3. SSDB
  4. Voldemort
  5. Oracle BDB

1.3.2 Column storage database

1.3.2.1, description,

This part of the database is usually used to cope with the distributed storage of massive data.

1.3.2.2, characteristics

The keys still exist, but they are characterized by pointing to multiple columns. The columns are arranged by the column families.

1.3.2.3 Related Products

  1. Cassandra
  2. HBase
  3. Riak.

1.3.3 Document database

1.3.3.1, description,

The document database was inspired by Lotus Notes Office software, and it is similar to the first type of key-value store. This type of data model is a versioned document, semi-structured document stored in a specific format, such as JSON. The document database can be viewed as an updated version of the key database, allowing for the nesting of key values. And the query efficiency of document database is higher than that of key value database.

1.3.3.2 characteristics,

Store as a document.

1.3.3.3 Related Products

  1. MongoDB
  2. CouchDB
  3. MongoDb(4.x)
  4. SequoiaDB (domestic)

1.3.4 Graph Database

1.3.4.1 characteristics,

Unlike other rows and rows and rigid SQL databases, a graphically structured database uses a flexible graphical model and can scale across multiple servers.

1.3.4.2 Related Products

  1. Neo4J
  2. InfoGrid
  3. Infinite Graph

1.4 NoSQL Application Scenarios

  • The data model is simple

  • More flexible IT systems are needed

  • High requirements on database performance

  • A high degree of data consistency is not required

Introduction to Redis

2.1 overview,

Redis is an in-memory database

2.2 features of Redis

  1. Redis is a high performance key/value in-memory database.

  2. Redis supports a rich range of data types.

  3. Redis supports persistence.

  4. Redis single thread, single process.

2.3 Redis Installation (Linux)

2.3.1 Download the Redis source package

Download direct

2.3.2 Upload and decompress

The tar - ZXVF redis - 4.0.10. Tar. GzCopy the code

2.3.3 Install the GCC

 yum install -y gcc
Copy the code

2.3.4 Go to the decompression directory

make MALLOC=libc
Copy the code

2.3.5. Run the following command after the compilation

make install PREFIX=/usr/redis
Copy the code

2.3.6 Go to /usr/redis to start the Redis service

./redis-server
Copy the code

Once this page appears, Redis is launched!

2.3.7 Go to the bin directory to connect to the client

/ reis -cli -p 6379 --raw./ reis -cli -p 6379 --rawCopy the code

2.4 Details of Redis installation

Redis startup service details

/redis-server /redis-server/clone/clone/clone/clone/clone/clone/clone/clone/clone/clone/clone/clone/clone/clone Start the client in a new window./redis-cli -p 6379

How do I specify profile startup when starting Redis

By default, after reidis is installed, there is no configuration file in the installation directory. You need to copy the redis.conf configuration file from the source directory to the installation directory.

  1. Go to the source directory and copy the file
cp redis.conf /usr/redis
Copy the code
  1. Go to the /usr/redis installation directory to view the replication configuration file
 cd /usr/redis
 ls
Copy the code
  1. Go to the bin directory to load and start the configuration
./redis-server .. /redis.confCopy the code

The concept of libraries in Redis

A library can store key-value pairs. In Redis, each library has a unique name (number). Starting from 0, the default number of libraries is 16, and the library number is 0-15. We can use commands to switch libraries, each library is isolated from each other, each library can have the same or different key and value, do not affect each other.

#Switch the librarySelect dbid from dbid#Clearing the current library
flushDB   
#Empty all libraries 
flushAll 
Copy the code

Enable the Redis remote connection

  1. Modify the configuration file to enable remote connection
vim redis.conf
Copy the code
  1. By default, bing 127.0.0.1 only allows native access. Change the configuration to:
#Allow all client accessBing 0.0.0.0Copy the code
  1. Restart Redis as a configuration file
./redis-server .. /redis.confCopy the code
  1. You can turn off Redis on the client
#(Remember there is a space before it)
hutdown
Copy the code

3. Redis database related directives

3.1 Database operation instructions

3.1.1 Description of the library

The default Rsdis configuration configures the Redis service. By default, there are 16 libraries, numbered from 0 to 15. You can use the select library number to select a Rsdis library.

Seelect library numberCopy the code

3.1.2 Clearing the Library

#Clear the current library 
FLUSHDB
#Empty the entire library 
FLUSHALL
Copy the code

3.1.3 Redis client displays Chinese

./redis-cli  -p 7000 --raw
Copy the code

3.2 Relevant instructions of operation key

3.2.1 DEL instruction

3.2.1.1, grammar,

 DEL key [key ...] 
Copy the code

3.2.1.2,

Deletes one or more given keys. Non-existent keys are ignored.

3.2.1.3 Return value

Number of keys to be deleted.

3.2.2 EXISTS directive

3.2.2.1, grammar,

EXISTS key
Copy the code

3.2.2.2,

Check whether the given key exists.

3.2.2.3 Return value

Return 1 if key exists, 0 otherwise.

3.2.3 EXPIRE directive

3.2.3.1, grammar,

EXPIRE key seconds
Copy the code

3.2.3.2,

Sets the lifetime for a given key. When the key expires (lifetime is 0), it is automatically deleted. Its time complexity is order one.

3.2.3.3 Return value

1 is returned after the setting is successful.

3.2.4. PEXPIRE directive

3.2.4.1, grammar,

PEXPIRE key milliseconds
Copy the code

3.2.4.2,

This is similar to what the EXPIRE command does, but it sets the lifetime of the key in milliseconds rather than seconds as with the EXPIRE command. His time complexity is also order one.

3.2.4.3 Return value

If the key does not exist or the setting fails, 0 is returned

3.2.5. KEYS command

3.2.5.1, grammar,

KEYS pattern
Copy the code

His pattern is a wildcard that can be matched:

  1. KEYS * : matches all KEYS in the database.
  2. KEYS h? Llo: matches Hello, Hallo, and hxLLO.
  3. KEYS h*llo: matches hlLO and Heeeeello, etc.
  4. KEYS h[ae]llo: matches hello and hallo, but not Hillo. For special symbols"\" Separated.

3.2.5.2,

Finds all keys that match the given pattern.

3.2.5.3 Return value

List of keys that match the given pattern.

3.2.6 MOVE instruction

3.2.6.1, grammar,

MOVE key db
Copy the code

3.2.6.2,

Moves the key of the current database to the given database DB.

3.2.6.3 Return value

Return 1 on success and 0 on failure

3.2.7 TTL instruction

3.2.7.1, grammar,

 TTL key
Copy the code

3.2.7.2,

Returns the TTL (time to live) of a given key, in seconds.

3.2.7.3 Return Value

  1. If the key does not exist, return -2.
  2. If the key exists but the remaining lifetime is not set, -1 is returned.
  3. Otherwise, the remaining lifetime of the key is returned in seconds.
  4. Prior to Redis 2.8, the command returned -1 when the key did not exist, or when the remaining lifetime of the key was not set.

3.2.8. PTTL instruction

3.2.8.1, grammar,

PTTL key
Copy the code

3.2.8.2,

This command is similar to the TTL command, but it returns the remaining lifetime of the key in milliseconds rather than seconds as the TTL command does.

3.2.8.3 Return value

  1. If the key does not exist, return -2. If the key exists but the remaining lifetime is not set, -1 is returned.
  2. Otherwise, the remaining lifetime of the key is returned in milliseconds.
  3. Prior to Redis 2.8, the command returned -1 when the key did not exist, or when the remaining lifetime of the key was not set.

3.2.9 RANDOMKEY instruction

3.2.9.1, grammar,

RANDOMKEY
Copy the code

3.2.9.2,

Returns a key randomly (without deleting) from the current database.

3.2.9.3. Returned Value

Returns a key when the database is not empty. Returns nil when the database is empty.

3.2.10. RENAME command

3.2.10.1, grammar,

RENAME key newkey
Copy the code

3.2.10.2,

Change the name of key to newKey. Returns an error if the key is the same as newKey, or if the key does not exist. When newkey already exists, the RENAME command overwrites the old value.

3.2.10.3 Return Value

An OK message is displayed when the name change succeeds, and an error is returned when the name change fails.

3.2.11 TYPE directive

3.2.11.1, grammar,
TYPE key
Copy the code

3.2.11.2,

Returns the type of the value stored by key.Copy the code

3.2.11.3. Return value

  1. None (key does not exist)
  2. String (string)
  3. List (list)
  4. Set
  5. Zset (ordered set)
  6. Hash table

Data types in Redis

4.1. The type is String

4.1.1 Memory Model

4.1.2 Common Operation Commands

The command instructions
set Set a key/value
get Get the corresponding value based on the key
mset Set multiple key values at a time
mget Obtain the value of multiple keys at once
getset Gets the value of the original key and sets the new value
strlen Obtain the length of the corresponding key store value
append Append the value of the corresponding key
getrange Intercept the contents of value, where -1 can represent the end (index 0 starts)
setex Set the lifetime of a key in seconds
psetex Set the lifetime of a key in milliseconds
setnx Yes No operation is performed and no addition is performed
msetnx You can set multiple keys at the same time, atomic operations, as long as one of them exists is not saved
decr Perform the -1 operation on a numeric value
decrby Subtract from the data provided
Incr Perform a +1 operation on a numeric type
incrby Add operations based on the data provided
Incrbyfloat Add floating-point numbers based on the data provided

4.2. List type

The List type in Redis is the Java equivalent of the List collection, which is characterized by the sequential and repeatable elements

4.2.1 Memory Model

4.2.2 Common operation instructions

The command instructions
lpush Add a value to the header of a key list (from left)
lpushx Same as lpush, but you have to make sure that this key exists
rpush To add a value to the end of a key list (putting elements from the right)
rpushx Same as Rpush, but you have to make sure that this key exists
lpop Returns and removes the first element to the left of the list, and returns
rpop Returns and removes the first element to the right of the list, and returns
lrange Gets the elements in a range of subscripts (starting at 0 by default and ending at -1)
llen Gets the number of elements in the list
lset Sets the value of a specified index (the index must exist)
lindex Gets an element at a specified index location
lrem Delete duplicate elements lerm lists 3 zhangsan(delete 3 zhangsan in lists set)
ltrim Retain the elements within a specific interval of the list
linsert Insert new elements before and after an element

4.3 Set type

4.3.1 Memory Model

4.3.2 Common Commands

The command instructions
sadd Add elements to the collection
smembers Displays all the elements in the collection, which is actually unordered
scard Returns the number of elements in a collection
spop Returns an element at random and removes it from the collection
smove To move elements from one set to another, they must be of the same type
srem Removes an element from a collection
sismember Determine whether a set contains this element
srandmember Random return element
sdiff Get rid of the same elements in other sets of the first set
sinter masked
sunion Sum set

4.4. ZSet type

It is a sortable set that cannot be repeated, also known as a sortSet.

4.4.1 Memory Model

4.4.2 Common Commands

The command instructions
zadd Adds an ordered collection element
zcard Returns the number of elements in the collection
Zrange ascending zRevRange descending Returns an element in a range
zrangebyscore Finds elements in a range by score
zrank Return to top
zrevrank List in reverse chronological order
zscore Displays the score of an element
zrem Remove an element
zincrby Give points to a specific element

4.5 Hash type

Put a map structure in value, there is a key and value, key is unordered

4.5.1 Memory Model

4.5.2 Common Commands

The command instructions
hset Set a key/value pair
hget Get a value corresponding to a key
hgetall Get all key/value pairs
hdel Example Delete a key/value pair
hexists Determine whether a key exists
hkeys Get all the keys
hvals Get all the values
hmset Set multiple keys or values
hmget Get the value of multiple keys
hsetnx Sets the value of a nonexistent key
hincrby Add to value (implement increment)
hincrbyfloat Add floating-point value to value (retain up to 17 bits)