table of Contents
Preface
REmote DIctionary Server (Redis) is a key-value storage system written by Salvatore Sanfilippo.
Redis is an open source log-type, Key-Value database that is written in ANSI C language, complies with the BSD protocol, supports the network, can be memory-based or persistent, and provides APIs in multiple languages. It is usually called a data structure server because the value can be of string (String), hash (Hash), list, set, and sorted sets.
Three major roles
- database
- Cache
- Message middleware MQ
Extension:
Redis bottleneck:
Redis is single-threaded, based on memory operations, CPU is not a performance bottleneck, the bottleneck is memory and network bandwidth
Why is redis fast?
Redis puts all the data in the memory, so single thread is the highest, because multi-threaded cpu takes more time to switch between the door and the door. For the memory system, if there is no context switch, single thread is The best. The cpu switch is between 1000-1500 nanoseconds.
Five types
Strings
Hashes (hash)
Lists
Sets
Sorted Sets (Ordered Sets)
Three special data types
Bitmap
Geo
GEOADD
Basic Redis operation
Redis has 16
a database by default
vim /redis.conf # 查看配置文件
The number of the library is 0-15. The
default entry is library 0
select 2
Switch to the second database
set name jack
Create a key with name value jack
type name
View the value type of the name key
MSET age1 10 age2 20
Create multiple values at the same time
DBSIZE
View the capacity of the database
keys *
View all keys of the current database*
get name
View the value of the name key
flushdb
Clear the current library
flushall
Clear all libraries (open RDB persistence, the data is saved on the disk) to
exists name
determine whether the name key exists. If it returns 1, it exists, 0 does not exist.
move name 2
Move the name key to the second library.
del name
Delete the name key
EXPIRE name 30
Set the value of the name key to be valid for 30 seconds, and clear the value after expiration (1. For example, store a cookie and expire after half a month 2. Single sign-on, set the expiration time 3. Verification code)
TTL name
View remaining time
Value is empty after expiration