Redis is an open source (BSD-licensed), in-memory data structure storage system that can be used as a database, cache, and messaging middleware. It supports many types of data structures, such as strings (hashes), lists (Lists), sets (sets), sorted sets (sorted sets) and range queries, bitmaps, Hyperloglogs and Geospatial index radius query.

Redis is built with replication, LUA scripting, LRU eviction, transactions and different levels of disk persistence, And provides high availability through Redis Sentinel and Automated partitioning (Cluster).

Redis is a single thread

Redis is memory based, CPU is not the performance bottleneck of Redis, its bottleneck depends on the memory and network bandwidth of the machine, so if you can achieve a single thread then use a single thread.

So why is it so fast with a single thread?

First of all, there are two misunderstandings to be clear:

  • High-performance servers are not necessarily multithreaded.
  • Multithreading is not necessarily more efficient than single-threading.

Redis is to put all the data in memory, but with multi-threading, there will be CPU context switch, which will increase the time, and for memory system, without context switch, single thread operation efficiency is the highest.

Two, the common operation about the database

1. Default database

Redis has 16 databases by default and uses the first database (subscript 0) by default. If you look at the configuration file, there is database 16.

2. Switch databases

Select 3 to switch to a fourth database.

3. Check the database size

Dbsize Displays the current database size.

In this case, give the database set value, check the database size again, the change.

4. Clear the database

Clear the current database

flushdb
Copy the code

Clear all databases

flushall
Copy the code

Common operations on redis-key

1. Set Sets the key and value

set name pingguo222
Copy the code

2. Keys * View all keys

You can view all the keys. Switch back to the default database and look at all the keys.

keys *
Copy the code

3. Get key value

get name
Copy the code

4. Exists Key Whether a key exists

exists name
Copy the code

If it exists, return 1; if it does not, return 0.

5. Move A mobile key

The MOVE command moves the key of the current database to the given database db.

move name 0
Copy the code

For example, in the current 3 database, the command is to move the name of the current 3 database to the 0 database. Returns 1 on success and 0 on failure.

6. Set the key expiration time

I set the age to expire 10 seconds later, so I can’t get the value again after 10 seconds.

expire age 10
Copy the code

7. Check the remaining time of the key

I set the expiration time of name to 100s and use TTL to check.

ttl name
Copy the code

8. Check the key type

type name
Copy the code

There are actually a lot of commands, the official website provides a place to query the command, after what will not be checked what.