5 4.1 Redis data types
redis is an advanced key-value storage system, in which the value supports five data types:
-
String (String)
-
Hash (hash)
-
String list (list)
-
String collection (set)
-
An ordered set of character strings (sorted set)
Definition of the key, and note the following:
-
key not too long, it is best not to operate 1024 bytes, it will not only consume memory also reduce the search efficiency
-
key not too short, too short, will reduce the key if the readability
-
In the project, key best to have a uniform naming convention
4.2 string type string
String String type 4.2.1 Overview
Redis string type is the most basic type of data storage, it is binary safe in Redis, this would mean that the type of deposit and access to the same data. Data type Value length of the string can accommodate up in Redis is 512M.
String String type commonly used commands 4.2.2
-
set key value
Key set held by a specified string value, if the key exists overwrite operation. Always returns "OK"
127.0.0.1:6379> set company "itcast"
OK
127.0.0.1:6379>
-
get key
Gets the key value. If the value associated with the key is not of type String, redis will return an error message because the get command can only be used to acquire String value; if the key does not exist, return (nil).
127.0.0.1:6379> set name "itcast"
OK
127.0.0.1:6379> get name
"itcast" -
del key
Delete the specified key
127.0.0.1:6379> del name
(Integer). 1
127.0.0.1:6379> GET name
(nil) incr command increment decr Save command from incrby key step increment the number of steps decrby number of steps from the Save key step
4.3 Types hash hash
4.3.1 Types hash hash Overview
The Hash Redis type can be viewed as having a String Value String Key and the container map. Therefore, this type is very suitable for storing the information value of the object. The Username, Password Age, and the like. If the Hash contains very little field, then this type of data will also only use very little disk space. Each Hash can store 4,294,967,295 pairs.
4.3.2 hash hash type commonly used commands
-
hset key field value
Set specified key field / value pair (key-value pairs).
127.0.0.1:6379> hset myhash username haohao
(integer) 1
127.0.0.1:6379>
-
hget key field
Returns the field value in the specified key
127.0.0.1:6379> hset myhash username haohao
(integer) 1
127.0.0.1:6379> hget myhash username
"haohao"
-
hdel key field [field … ]
Can delete one or more fields, the return value is the number of fields deleted
127.0.0.1:6379> hdel myhash username
(integer) 1
127.0.0.1:6379> hget myhash username
(nil)
127.0.0.1:6379>
4.4 List Type list
4.4.1 List Type list Overview
In the Redis, List type of insertion order are sorted string list. And data structures in general list, we can and tail (right) to add a new element in its head (left). When inserted, if the key does not exist, Redis will create a new list for that key. On the contrary, if all the elements in the list are removed, then the key will also be deleted from the database. List the number of elements can be included in the maximum is 4294967295
4.4.2 List Type list
-
lpush key values[value1 value2…]
Insert all of the values associated with the specified key list head, if the key does not exist, the command creates a blank list associated with the key inserted before, after the head of the list again to insert data. Insert success, returns the number of elements.
127.0.0.1:6379> lpush mylist a b c
(integer) 3
127.0.0.1:6379>
-
lpop key
A return element associated with the first key and pop specified linked list, i.e., the head element. If the key does not exist, it returns nil; if the key is present, the head elements of the list is returned.
127.0.0.1:6379> lpush mylist a b c
(integer) 3
127.0.0.1:6379> lpop mylist
"c"
127.0.0.1:6379> lpop mylist
"b" -
rpop key
From the tail pop elements.
127.0.0.1:6379> lpush mylist a b c
(integer) 3
127.0.0.1:6379> rpop mylist
"a"
4.5 collection type set
4.5.1 collection types set
In Redis, we can see the Set as character type is not the sort of collection, and List types, we can also perform on the added value of this type of data, or delete an element to determine whether there are other operations. Note that the time complexity of these operations is O (1), i.e., within a constant time to complete the operations. Set the number of elements that can contain a maximum of 4,294,967,295 and List type is different, repeated elements Set collection is not allowed.
4.5.2 collection type set of common commands
-
sadd key values[value1、value2…]
To add data to the set, and if the value of the existing key is not repeated addition
127.0.0.1:6379> sadd myset a b c
(integer) 3 -
smembers key
Get all the members of the set in
127.0.0.1:6379> sadd myset a b c
(integer) 3
127.0.0.1:6379> smembers myset
1) "c"
2) "a"
3) "b" -
srem key members[member1、member2…]
Delete the specified set of members
127.0.0.1:6379> srem myset a b (integer) 2 127.0.0.1:6379> smembers myset 1) "c" 127.0.0.1:6379>
Chapter 5 Redis generic command
-
keys pattern
Get all the pattern matching key, returns all matches with the key keys. * Represents any one or more characters,? Represent any one character
127.0.0.1:6379> keys *
1) "company"
2) "mylist"
3) "myhash"
4) "myset" -
the key1 key2 ...
Delete the specified key
127.0.0.1:6379> partner
(integer) 1 -
expire key
Determining whether the key is present, indicates the presence of 1, 0 represents the absence
127.0.0.1:6379> exists compnay
(integer) 0
127.0.0.1:6379> exists mylist
(integer) 1
127.0.0.1:6379> -
type key
Gets the specified key type. The format of the command is returned as a string. Returned string string, list, set, hash, if the key does not exist or none
127.0.0.1:6379> type company
string
127.0.0.1:6379> type mylist
list
127.0.0.1:6379> type myset
set
127.0.0.1:6379> type myhash
hash
127.0.0.1:6379>expire
expire date:
sec expire key value
Chapter 6 Redis persistence
6.1 Redis persistence Overview
Redis is due to its high performance all data stored in the memory, in order to make Redis after the restart still ensure data is not lost, you need to synchronize data from memory to the hard drive, this process is persistence. Redis supports persistent in two ways, one is RDB ways, one way is to AOF. Which may be used alone or both in combination.
-
RDB persistence (supported by default, no configuration)
This mechanism means within the specified time intervals a snapshot of the data set in the memory is written to disk.
-
AOF persistence
This mechanism will be in the form of log records processed by the server every write operation, at the beginning of Redis server starts, it reads this file to rebuild the database to ensure data after starting the database is complete.
-
No persistence
We can disable persistence Redis server function by way of configuration, so that we can Redis as a function of the enhanced version of memcached.
-
redis RDB and can be used at the same time AOF
6.2 RDB persistence mechanism
6.2.1 RDB lasting advantage institutionalized
-
Once this manner, then your entire Redis database will contain only one file, which for file backup is perfect. For example, you might intend to archive every hour last 24 hours of data, but also archived once in the last 30 days of data every day. With this backup strategy, once the system has failed catastrophically, we can very easily be restored.
-
For disaster recovery purposes, RDB is a very good choice. Because we can very easily be a separate file compression and then transferred to other storage media
-
Performance is maximized. For service of process Redis, at the beginning of persistence, its only need to do is fork (fork) the child process, and then after the completion of these persistent work by the child, so that you can avoid a great service execution process IO operated.
Compared to AOF mechanism, if the data set is large, start efficiency will be higher RDB
6.2.2 RDB persistence mechanism shortcomings
-
If you want to guarantee high availability of data, that is, the maximum to avoid loss of data, then the RDB would not be a good choice. Because once the system downtime phenomenon before the timing of persistence, had not had time to write data on the disk will be lost.
-
Since the RDB by fork child process to assist with data persistence work, so if and when the data set is large, it may cause the entire server to stop serving hundreds of milliseconds, or even 1 second
Configuration 6.2.3 RDB persistence mechanism
There follows redis.windows.conf configuration file:
################################ SNAPSHOTTING #################################
#
# Save the DB on disk:
#
# save <seconds> <changes>
#
# Will save the DB if both the given number of seconds and the given
# number of write operations against the DB occurred.
#
# In the example below the behaviour will be to save:
# after 900 sec (15 min) if at least 1 key changed
# after 300 sec (5 min) if at least 10 keys changed
# after 60 sec if at least 10000 keys changed
#
# Note: you can disable saving at all commenting all the "save" lines.
#
# It is also possible to remove all the previously configured save
# points by adding a save directive with a single empty string argument
# like in the following example:
#
# save ""
save 900 1
save 300 10
save 60 10000
Among them, the above configuration is the way RDB data persistence timing:
Keyword | Time (sec) | Modify the number of key | Explanation |
---|---|---|---|
save | 900 | 1 | At least every 900 seconds (15 minutes) a key is changed, the memory snapshot dump |
save | 300 | 10 | (5 minutes) at least every 300 seconds key 10 is changed, the memory snapshot dump |
save | 60 | 10000 | At least every 60 seconds (1 minute) 10 000 key changes, the memory snapshot dump |
6.3 AOF persistence mechanism
6.3.1 AOF persistence mechanism advantage
-
This mechanism can result in higher data security, namely data persistence. Redis provides 3 in synchronization strategy, that sync every second, every modification and synchronization are not synchronized. In fact, asynchronous synchronization is complete per second, its efficiency is very high, the difference is that once the system downtime phenomenon, then within seconds the modified data will be lost. And every modify the synchronization, we can be regarded as synchronization persistence that occurs every time data changes will be immediately recorded to disk. It is foreseeable that in this way is the lowest in efficiency. As for non-synchronous, needless to say, I think we can correct understanding of it.
-
Since the mechanism writes the log file is used in append mode, so even if there is the phenomenon of downtime during the writing process, it will not destroy the contents of the log file that already exists. However, if we write this operation is only half the data appeared system crash, do not worry, before the next startup Redis, we can to help us solve the problem of data consistency by redis-check-aof tool.
-
If the log is too large, Redis can automatically enable rewrite mechanism. That Redis to append mode will keep the modified data is written to the old disk file, while Redis also creates a new file to record what has changed on command is executed during this period. Therefore, when switching is performed rewrite data security can be guaranteed better.
-
AOF format comprising a clear, easy to understand for the log file records all the changes. In fact, we can complete the reconstruction data through the file
6.3.2 AOF persistence mechanism shortcomings
-
For the same number of data sets, AOF document file is typically greater than RDB
-
Depending on the synchronization policy, AOF on the operating efficiency tends to be slower than in RDB. In short, the efficiency of synchronous per second strategy is relatively high, disable synchronization efficiency and RDB policy as efficient.
6.3.3 AOF persistence mechanism configuration
6.3.3.1 open AOF persistence
############################## APPEND ONLY MODE ###############################
# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (see later in the config file) Redis can lose just one second of writes in a
# dramatic event like a server power outage, or a single write if something
# wrong with the Redis process itself happens, but the operating system is
# still running correctly.
#
# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
#
# Please check http://redis.io/topics/persistence for more information.
appendonly no
The appendonly modified to yes, open aof persistence mechanism, the default directory will be produced under a appendonly.aof file
6.3.3.2 AOF persistent timing
# appendfsync always
appendfsync everysec
# appendfsync no
For the above-described configuration aof persistence time, explained as follows:
Keyword | Persistence opportunity | Explanation |
---|---|---|
appendfsync | always | Each time the update command, persistence time |
appendfsync | everysec | Persistence once per second |
appendfsync | no | Non-endurance of |