A, Redis commonly used instructions related to database
Note: After using the default configuration is moving redis redis service, by default there are 16 libraries, numbered 0-15
You can use numbers to select a library select the library redis
- Empty current library FLUSHDB
- Empty all of the library FLUSHALL
- 1. OF THE
Two, Redis commonly associated with the command key
Syntax: DEL key [key ...]
Role: remove the given one or more key. Non-existent key will be ignored.
Available versions:> = 1.0.0
time complexity:
O (N), N is the number of the key to be deleted.
Delete a single character string type key, the time complexity is O (1).
Delete a single list, a set of ordered set or type of hash table key, the time complexity is O (M), M is the number of elements in the data structure above.
Returns: the number key is deleted.
- 2. EXISTS
Syntax: EXISTS key
Role: Checks if the given key exists.
Available versions:> = 1.0.0
Time complexity: O (1)
Returns: If the key exists, returns 1, otherwise it returns 0.
- 3. EXPIRE
Syntax: EXPIRE key seconds
Role: set key survival time was set to, (survival time is 0), it will be automatically deleted when the key expires.
In the Redis, key with survival time was called "volatile" (volatile). Survival time can be removed by using the DEL command to delete the entire key, or by SET and GETSET command overwrites (overwrite),
This means that if a command just modify (alter) a median survival time of key band rather than a new key value to replace (replace) it, then the survival time will not be changed.
For example, the implementation of a key INCR command, for a list LPUSH command, or execute a command HSET hash table, such operations are key itself does not modify the survival time.
On the other hand, if you use a RENAME key to be renamed, then the survival time after the key was renamed and renamed as before. RENAME command Another possibility is to try to survive a key time band changed its name to another another_key with survival time then the old another_key (and its survival) will be deleted, then the old key will be renamed another_key Therefore, the new another_key survival time and the same as the original key. Use PERSIST command without delete key, removing key survival time, so that key again become a "permanent" (persistent) key. Updated survival time EXPIRE command key can be executed with a survival time already, the newly designated survival time will replace the old survival time. The accuracy of the expiration time in Redis 2.4 version, the expiration time delay within 1 second - That is, even if the key has expired, but it still may be accessed after the expiration within one second, and in the Redis new version 2.6, the delay is reduced to within a millisecond. Redis different from the previous 2.1.3
In the previous version of Redis 2.1.3, modify the key with a survival time will cause the entire key is removed, the behavior is copied by the time limit (replication) layer made of, this restriction has now been repaired.
Available versions:> = 1.0.0
Time complexity: O (1)
Return Value: Set the successful return 1.
- 4. KEYS
Syntax: KEYS pattern
Role: Find all key match a given pattern of pattern.
KEYS * matching database of all key.
KEYS h? Llo match hello, hallo and hxllo and so on.
KEYS h * llo match hllo and heeeeello and so on.
KEYS h [ae] llo and matching hello hallo, but does not match hillo.
Special symbols with \ separated
Note: KEYS very fast, but using it still can cause performance problems in a large database, if you need to find a specific focus from a key data, you'd better use Redis collection structure (set) instead.
Available versions:> = 1.0.0
Time complexity: O (N), N is the number of key database.
Returns: match a given pattern key list.
- 5. MOVE
Syntax: MOVE key db
Role: The current database key to move to a given database db them.
If the current database (source database) and a given database (the target database) with the same name given key, or key does not exist in the current database, then MOVE has no effect. Therefore, you can take advantage of this feature, as the MOVE lock (locking) primitive (primitive).
Available versions:> = 1.0.0
Time complexity: O (1)
Return value: the successful return movement 1, failure 0 is returned.
- 6. PEXPIRE
Syntax: PEXPIRE key milliseconds
Effect: this effect is similar to the command and the command EXPIRE, but it is set in milliseconds key lifetime, unlike EXPIRE command as in seconds.
Available versions:> = 2.6.0
Time complexity: O (1)
Return Value: set successfully, return 1 key set is not present or fails, returns 0
- 7. TTL
Syntax: TTL key
Role: in seconds, return the remaining lifetime of a given key (TTL, time to live).
Available versions:> = 1.0.0
Time complexity: O (1)
return value:
When the key is not present, returns -2.
When the key is present but the remaining lifetime is not provided, -1.
Otherwise, in seconds, to return the remaining lifetime of the key.
Note: In Redis 2.8 before, when the key does not exist, or is not provided key remaining lifetime, commands -1.
- 8. PTTL
Syntax: PTTL key
Role: This command is similar to TTL commands, but it returned in milliseconds remaining lifetime of the key, rather than command as TTL, in seconds.
Available versions:> = 2.6.0
Complexity: O (1)
Return value: when the key is not present, returns -2. When the key is present but the remaining lifetime is not provided, -1.
Otherwise, in milliseconds, return key remaining lifetime.
Note: In the previous Redis 2.8, when the key does not exist, or is not provided key remaining lifetime, commands -1.
- 9. RANDOMKEY
Syntax: RANDOMKEY
Role: random return (not deleted) a key from the current database.
Available versions:> = 1.0.0
Time complexity: O (1)
Return value: when the database is not empty, a return key. When the database is empty, returns nil.
- 10. RENAME
Syntax: RENAME key newkey
Role: The key renamed newkey. When the newkey same key, or key does not exist, an error is returned. When newkey already exists, RENAME command overwrites the old value.
Available versions:> = 1.0.0
Time complexity: O (1)
Returns: OK when prompted to rename success, failure, when an error is returned.
- 11. TYPE
Syntax: TYPE key
Effect: the return type of the stored key.
Available versions:> = 1.0.0
Time complexity: O (1)
return value:
none (key does not exist)
string (string)
list (list)
set (collection)
zset (ordered set)
hash (hash table)
Three, Redis String type of operation
command |
Explanation |
set |
Provided a key / value |
get |
The obtained value key corresponding to |
mset |
Set more than one key value |
mget |
Value once more get the key |
GetSet |
Get the value of the original key, and set the new value |
strlen |
Obtain a corresponding key value stored in a length |
append |
Is a key value corresponding to the content of the additional |
getrange |
Interception of the content value |
setex |
Set a key survival period (s) |
psetex |
Set a key survival period (milliseconds) |
setnx |
There is no action, there is no added |
msetnx |
You can set multiple simultaneous key, as long as there is a presence not save |
decr (does not exist, create) |
Numerical type 1 operation |
Decrby (does not exist, create) |
Subtraction operation according to the data provided |
Incr (does not exist, create) |
Numerical type +1 |
incrby (does not exist, create) |
An addition operation based on the data provided |
Incrbyfloat (reserved 17) |
The addition of floating-point data provided |
Four, Redis List of types of operations
- 1. illustrates
- 2. Command
command |
Explanation |
lpush |
Will be a value added to a list of key head |
lpushx |
With lpush, but we must ensure that this key exists |
Rpus |
Will be a value added to the end of a key list |
Rpushksh |
With rpush, but we must ensure that this key exists |
lpop |
Remove and return the first element of the list |
rpop |
Returns the last element of the list and removed |
lrange |
Get element within a certain index range |
len |
Gets the number of list elements |
lset |
Setting a value of a specified index (index must exist) |
lindex |
Gets the element at a specified index position |
lrem |
Remove duplicate elements |
LTrim |
Retention elements within a particular range list |
linsert |
Before a certain element, after inserting a new element |
Five, Redis of SET type of operation
- 1. illustrates
- 2. Command
command |
Explanation |
sadd |
Add elements to the collection |
smembers |
Display a collection of all the elements of disorder |
scard |
Returns the number of elements in the collection |
spop |
To delete a random element |
smove |
From a collection to another set of mobile elements |
srem |
Remove an element from the collection |
sismember |
Determining whether a set of elements comprising the |
srandmember |
Returns the random element |
sdiff |
筛选出第一个集合中其它集合含有的相同元素,保留剩下的元素 |
sinter |
求交集(同时拥有的元素) |
sunion |
求并集(和) |
六、 Redis的ZSET类型的操作
- 1. 图示
- 2. 命令
命令 |
说明 |
zadd |
添加一个有序集合元素 |
zcard |
返回集合的元素个数 |
zrange |
返回一个范围内的元素 |
zrangebyscore |
按照分数查找一个范围内的元素 |
zrank |
返回排名 |
zrevrank |
倒序排名 |
zscore |
显示某一个元素的分数 |
zrem |
移除某一个元素 |
zincrby |
给某个特定元素加分 |
七、 Redis的HASH类型的操作
- 1. 图示
- 2. 命令
命令 |
说明 |
hset |
设置一个key/value对 |
hget |
获得一个key对应的value |
hgetall |
获得所有的key/value对 |
hdel |
删除某一个key/value对 |
hexists |
判断一个key是否存在 |
hkeys |
获得所有的key |
hvals |
获得所有的value |
hmset |
设置多个key/value |
hmget |
获得多个key的value |
hsetnx |
设置一个不存在的key的值 |
hincrby |
为value进行加法运算 |
hincrbyfloat |
为value加入浮点值 |