In the previous article, we Simply put a little Redis
application scenarios, install, how to connect other more basic knowledge, so in this article, we will build upon and continue to Redis
learn the basics of it.
In this article, we talk about Redis
data structures and general commands.
Redis data structures
Redis
It supports a variety of different data structures, including five kinds of basic data structures and some more complex data, these data structures to meet different application scenarios.
The five base data structure
-
String
: String, is the foundation for the other data structures -
Hash
: Hash List -
List
: List -
Set
: Collection, realized on the basis of the hash list -
Sort Set
: An ordered collection
Complex data structures
-
Bitmaps
: Bitmap, the bit operation based on the string, the data structure may be implemented to save space. -
Hyperloglog
: For estimating the number of elements in a set of probabilistic data structure. -
Geo
: Geospatial, geospatial index radius queries. -
BloomFilter
: Bloom filter.
Different data structures in common
From the above description, we see different data structures of support, but in fact, Redis的
each consists of a data structure key
and value
composition, it can be abstracted as:
Data structure and all key
values are any valid string, distinguish between different data structures is that value
differs stored values.
For example, most of the simple String data structure value for the String, String it can be expressed as:
The Hash data structure is a hash value list, so Hash can be expressed as:
Hash String listed here to explain and illustrate, more about the internal structure of the data structure and detailed operation, we then talk about it in a later article.
Redis generic command
Redis
The official website Redis
of the command according to the function is divided into 15 theme groups, in which Kyes
the theme of command for all data structures are common, therefore, it is necessary to understand the structure of command before other data to learn about.
keys
keys
Action command is to list Redis
all of the key
time complexity of the order of O (N) , N with Redis
the key
increase in the number increases, so Redis
a large number of key, keys
the command will be executed for a long time, and because Redis
a single-threaded, a command takes too long, it will cause all subsequent requests will not get a response, therefore, do not use on a production server keys
command.
# key命令,时间复杂度为O(n)
keys pattern #pattern可为一个包含匹配模式的字符串,可以包含*,+,?,[a-z]等模式。
复制代码
Examples
> mset hello_test1 one hello_test2 two helloa a hellob b
> keys hello*
1) "hello_test1"
2) "hello_test2"
3) "helloa"
4) "hellob"
> keys heelo?
1) "helloa"
2) "hellob"
> keys hello[a-z]
1) "helloa"
2) "hellob"
复制代码
exists
exists
A command key for determining whether there are one or more, determining a plurality of key
time, key
between the separated by spaces, exists
the return value is an integer that represents the current determines how many key
are present.
# exists命令,时间复杂度O(1)
exists key [key ...]
复制代码
Examples
> set test1 t1
> exists test1 test2
(integer) 1 #只有一个key存在
> exists test3 test3
(integer) 0 #key都不存在
复制代码
of the
del
Command is used to delete one or more key
, more key
separated by spaces, whose return value is an integer representing the number of successfully deleted exists key
, therefore, if only a delete key, it can be determined whether the return value success, If you delete multiple key, you can only get the number of successfully deleted.
# del命令,时间复杂度O(n)
del key [key ...]
复制代码
Examples
> set test t
> del test
(integer) 1
> mset test1 2 test2 1
> del test1 test2 test3
(integer) 2 # 返回2,表示成功删除两个
#再次删除,返回0,因为删除成功个数为0
> del test1 test2 test3
(integer) 0
复制代码
expire,pexpire
Setting key expiration expire after how many seconds, pexpire set key after the number of milliseconds expired, successful return 1, else return 0.
# expire命令,时间复杂度为O(1)
expire key seconds
# pexpire命令,时间复杂度为O(1)
pexpire key milliseconds
复制代码
Examples
> mset test test_value test1 test1_value
> expire test 10 #设置10秒后过期
(integer) 1
> pexpire test1_value 10000 #设置10000毫秒(10s)后过期
(integer) 1
> expire ttt 100
(integer) 0 # 不存在的key,设置失败,返回0
复制代码
ttl,pttl
ttl and pttl command is used to obtain key expiration time, the return value is an integer, is divided into several cases on behalf of significance:
- When the expiration time does not exist or key, returns -2.
- When a valid key is present and permanently, -1.
- When the set key has an expiration time, returns for the remaining number of seconds (Pttl is a few milliseconds)
# ttl命令,时间复杂度O(1)
ttl key
# pttl命令,时间复杂度O(1)
pttl key
复制代码
Example (ttl presentation, pttl similar)
> set test test
> expire test 100
> ttl test
(integer) 98#返回剩下的秒数
> set test1 #永久有效
> ttl test1
(integer) -1
> ttl test2
(integer) -2#不存在或过期
#100秒后
> ttl test # test已过期
(integer) -2
复制代码
expireat,pexpireat
A key disposed in the timestamp expires, expreat timestamp parameter is expressed in seconds, and then use pexpireat milliseconds, and functionally similar pexpire expire and returns a success, 0 indicates a failure.
#expireat命令,时间复杂度为O(1)
expireat key timestamp
#pexpireat命令,时间复杂度为O(1)
pexpireat key milliseconds-timestamp
复制代码
Examples
> set test test
> expireat test 1560873600 # 2019-06-19 00:00:00
(integer) 1
> set test1 test1
> pexpireat test1 156087360000 # 2019-06-19 00:00:00的毫秒表示
(integer) 1
复制代码
persist
Removing key expiration time, the permanent key set, when the expiration time set key use persist after removal command returns 1 if the key does not exist or is itself a permanent, 0 is returned.
# persist命令,时间复杂度O(1)
persist key
复制代码
Examples
> set test test
> ttl test
(integer) -1 # 表示永久有效
> persist test
(integer) 0 # 对永久有效或不存在的key使用persist命令,返回
> expire test 10
(integer) 1
> persist test
(integer) 1
复制代码
type
Determining what type of key data structures, the return value is string
, list
, set
, hash
, zset
, respectively, we introduce the foregoing Redis
five kinds of basic data structure.
geo, hyperloglog, bitmaps and other complex data structures, are on five basic data structure to achieve, such as geo is zset type, hyperloglog and bitmaps are string.
# type命令,时间复杂度O(1)
type key
复制代码
Examples
> set test test
> type test
string
> hset htest test test
> type htest
hash
复制代码
summary
Described above is Redis
the most commonly used generic commands, though simple, but still very necessary to master its use and use matters to pay attention, in fact, for ordinary developers, many times, just use common these basic commands to operate Redis
it.
Reproduced in: https: //juejin.im/post/5d078cd6f265da1b8466e62c