During cluster deployment, for high availability purposes, sessions are often shared and shared into two categories: Session replication and centralized management.
Redis can play a more important role in the centralized management of the session.
Two main factors restricting the session centralized sharing:
1. The session must have an HA mechanism to ensure that the session is not lost in the event of a failure of some servers in the cluster.
2. Session life cycle Management.
3. The session size is unknown, and the overall serialization and deserialization costs are high.
How to work with Redis
1. Redis has persistence and sentiment has ha effect (Redis cluster is currently in beta).
2. Redis provides a time-to-live management EXPIRE.
The 3.redis provides hash operations for local serialization and deserialization to ensure efficiency.
The following direct reference hashtable related commands (from: https://redis.readthedocs.org/en/2.4/hash.html)
Hash table (hash) Hset
- Hset key field value
Set the value of the Domain field in the hash table key to value.
If the key does not exist, a new hash table is created and the Hset operation is performed.
If the field field already exists in the hash table, the old value will be overwritten.
-
Complexity of Time:
-
O (1)
-
return value:
-
If field is a new domain in the hash table, and the value is set successfully, return 1. Returns 0if the field field in the hash table already exists and the old value has been overwritten by the new value.
redis> Hset website google "www.g.cn" # A new domain (integer) 1redis> hset website google "www.google.com" # Overwrite an old domain (inte GER) 0
Hsetnx
- Hsetnx key field value
Set the value of the field field in the hash table key to value if and only if the Domain field does not exist.
If the field field already exists, the operation is not valid.
If key does not exist, a new hash table is created and the HSETNX command is executed.
-
Complexity of Time:
-
O (1)
-
return value:
-
set Success, return 1. Returns 0if the given domain already exists and no operations are performed.
redis> hsetnx nosql key-value-store redis (integer) 1redis> hsetnx nosql key-value-store redis # operation is invalid, Domain Key-value-store already exists (integer) 0
Hmset
- Hmset key field value [field value ...]
Set multiple field - value(domain-value) pairs to the hash table key at the same time.
This command overwrites a domain that already exists in the hash table.
If the key does not exist, an empty hash table is created and the Hmset operation is performed.
-
Complexity of Time:
-
O (n), n is the number of field - value pairs.
-
return value:
-
returns OKif the command executes successfully. When key is not a hash table (hash) type, an error is returned.
# scenario 1: Hash table redis> hmset website Google www.google.com Yahoo www.yahoo.comokredis> hget website google "www.google.com "Redis> hget website yahoo" www.yahoo.com "# case 2: Type error when redis> SET G # error condition okredis> hmset G name Huangz age 2 0 (Error) ERR operation against a key holding the wrong kind of value
Hget
- Hget key Field
Returns the value of the given field field in the hash table key .
-
Complexity of Time:
-
O (1)
-
return value:
-
the value of the given field. Nilis returned when the given domain does not exist or a given key does not exist.
redis> hset Huangz Blog huangz.iteye.com (integer) 1redis> hget huangz Blog "huangz.iteye.com"
Hmget
- Hmget key field [field ...]
Returns the value of one or more given fields in a hash table key .
Returns a nil value if the given domain does not exist in the hash table.
Because the nonexistent key is treated as an empty hash table, a hmget operation on a nonexistent key returns a table with a nil value.
-
Complexity of Time:
-
O (n), n is the number of a given field.
-
return value:
-
a table that contains the associated values for a given field, and the table values are arranged in the same order as the request order for the given domain parameters.
redis> hmset pet dog "Doudou" Cat "Nounou" # Save multiple values at once okredis> hmget pet dog cat Fake_pet # The Order of return values is the same as the order of incoming parameters 。 1) "Doudou" 2) "Nounou" 3) (nil) # non-existent field returns nil value
Hgetall
- Hgetall Key
Returns all the fields and values in the hash table key .
In the return value, followed by each domain name (field name) is the value of the domain (value), so the length of the return value is twice times the size of the hash table.
-
Complexity of Time:
-
O (n), n is the size of the hash table.
-
return value:
-
returns the field and domain values of the Hashtable as a list. If key does not exist, returns an empty list.
Redis> hset hash_name Jack "Jack Sparrow" (integer) 1redis> hset hash_name Gump "Forrest Gump" (integer) 1redis> HG Etall hash_name1) "Jack" # domain 2) "Jack Sparrow" # value 3) "Gump" 4) "Forrest Gump"
Hdel
- Hdel key field [field ...]
Deletes one or more specified domains in the hash table key , and the nonexistent fields are ignored.
-
Complexity of Time:
-
O (n), n is the number of fields to be deleted.
-
return value:
The
-
number of domains successfully removed, excluding ignored domains.
Note
In versions below Redis2.4, Hdel can delete only a single domain at a time, and if you need to delete multiple domains in an atomic time, include the command in the MULTI/ EXEC block.
# test Data redis> Hgetall abbr1) "a" 2) "Apple" 3) "B" 4) "banana" 5) "C" 6) "cat" 7) "D" 8) "Dog" # Delete single domain redis> Hdel abbr A (inte GER) # Delete a nonexistent domain redis> hdel abbr not-exists-field (integer) 0# Delete multiple domains redis> Hdel abbr b C (integer) 2redis> hgetall A BBR1) "D" 2) "Dog"
Hlen
- Hlen Key
Returns the number of fields in the hash table key .
-
Complexity of Time:
-
O (1)
-
return value:
-
the number of fields in the hash table. Returns 0when key does not exist.
Redis> hset hash_name Jack "Jack Sparrow" (integer) 1redis> hset hash_name Gump "Forrest Gump" (integer) 1redis> HL EN hash_name (integer) 2
Hexists
- Hexists key Field
View the hash table key for the existence of a given domain field .
-
Complexity of Time:
-
O (1)
-
return value:
-
returns 1if the hash table contains a given field. Returns 0if the hash table does not contain the given domain, or the key does not exist.
redis> hexists phone myphone (integer) 0redis> hset phone myphone nokia-1110 (integer) 1redis> hexists phone Myphon E (integer) 1
Hincrby
- Hincrby key field Increment
Adds an increment incrementto the value of the field field in the hash table key .
The increment can also be negative, which is equivalent to subtracting a given field.
If key does not exist, a new hash table is created and the Hincrby command is executed.
If the domain field does not exist, the value of the domain is initialized to 0before the command is executed.
Executing the Hincrby command on a domain field that stores a string value will cause an error.
The value of this operation is limited to a 64-bit (bit) signed numeric representation.
-
Complexity of Time:
-
O (1)
-
return value:
-
after the Hincrby command is executed, the value of the Field field in the hash table key .
# case 1:increment is positive redis> hexists counter Page_view # Set airspace (integer) 0redis> hincrby counter Page_view (integer ) 200redis> Hget counter Page_view "200" # Case 2:increment Negative redis> hget counter Page_view "$" redis> Hincrby Counter page_view-50 (integer) 150redis> hget counter Page_view "150" # Scenario 3: Attempt to execute a hincrby command on a string value's domain redis> hset myhash String Hello,world # Sets a String value (integer) 1redis> hget myhash string "Hello,world" redis> hincrby Myhash string 1< c1/># command execution failed with error. (Error) ERR hash value is not a integerredis> hget Myhash string # Original value unchanged "Hello,world"
Hkeys
- Hkeys Key
Returns all the fields in the hash table key .
-
Complexity of Time:
-
O (n), n is the size of the hash table.
-
return value:
-
a table that contains all the fields in the hash table. Returns an empty table when key does not exist.
# scenario 1: Hash table non-empty redis> hmset website Google www.google.com Yahoo www.yahoo.comokredis> hkeys website1) "Google" 2) "Yahoo "# Case 2: Empty hash table/key does not exist redis> EXISTS fake_key (integer) 0redis> hkeys fake_key (empty list or set)
Hvals
- Hvals Key
Returns all values in the hash table key .
-
Complexity of Time:
-
O (n), n is the size of the hash table.
-
return value:
-
a table that contains all the values in the hash table. Returns an empty table when key does not exist.
# scenario 1: Non-empty hash table redis> hmset website Google www.google.com Yahoo www.yahoo.comokredis> hvals website1) "www.google.com" 2) "www.yahoo.com" # scenario 2: Empty hash table/nonexistent keyredis> EXISTS not_exists (integer) 0redis> hvals not_exists (empty list or set)
The application of Redis's hash operation in centralized session