Redis (REmote DIctionary Server) is an open source log-type, Key-Value database written in ANSI C language, complying with the BSD protocol, supporting the network, memory-based and persistent.
Redis is completely open source and free, complies with the BSD protocol, and is a high-performance key-value database.
Redis and other key-value caching products have the following three characteristics:
- Redis supports data persistence, which can save data in memory on disk, and can be loaded again for use when restarted.
- Redis not only supports simple key-value type data, but also provides storage of data structures such as list, set, zset, and hash.
- Redis supports data backup, that is, data backup in master-slave mode.
Advantages of Redis
- Extremely high performance - Redis can read 110,000 times/s and write 81,000 times/s.
- Rich data types – Redis supports Strings, Lists, Hashes, Sets and Ordered Sets data type operations for binary cases.
- Atomic – All Redis operations are atomic, and Redis also supports atomic execution of several operations after they are combined.
- Rich features - Redis also supports publish/subscribe, notifications, key expiration and more.
Redis data types
Redis supports five data types: string (string), hash (hash), list (list), set (collection) and zset (sorted set: ordered set).
Redis keys command
keys * : view all keys
keys "miao*" : View keys matching the prefix
flushdb : flush redis
type key : View the type of key
dbsize : View the number of keys in the database
exists key : Check if the element exists
del key : delete element
expire key seconds: Set how many seconds the element will expire after
ttl key: Check how long it can survive -2 means the key does not exist -1 means the scheduled task disappears and is permanently stored
Redis String (String)
SET key value : Set the value of the specified key, overwrite if it exists
GET key : Get the value of the specified key.
MSET key value [key value ...] : Set one or more key-value pairs at the same time.
MGET key1 [key2..] : Get all (one or more) values for the given key.
APPEND key value : If the key already exists and is a string, the APPEND command appends value to the end of the original value of the key
STRLEN key : Returns the length of the string value stored by key
Redis provides atomic auto-increment operation incr, which is used to prevent data errors from concurrent multi-threading (it must be a numerical value to add or subtract)
incr key: +1 of the atom
decr key: atomic -1
incrby key integer: atomic + integer
decrby key integer: atomic -integer
Redis hash (Hash)
Redis hash is a mapping table of field and value of string type, and hash is especially suitable for storing objects.
HSET key 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
HMGET key field1 [field2] : Get the values of all the given fields
HMSET key field1 value1 [field2 value2 ] : Set multiple field-value (field-value) pairs to the hash table key at the same time.
HEXISTS key field : Check whether the specified field exists in the hash table key.
HDEL key field2 [field2] : delete one or more hash table fields
HGETALL key Gets 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 the values in the hash table