Redis data types --string

String (String)

redis command usage:

SET key value // stored string key pair

MSET key value [key2 value2 ...] // string of key-value pairs stored in bulk

SETNX key value // stored in the absence of a key

GET key // get a string key

MGET key [key2 ...] // get bulk key

DEL key // delete a key

EXPIRE key seconds // set the expiration time of a key

Atomic operation:

INCR key // The key number stored value plus 1

DECR key // key value stored in the decrementing 1

INCRBY key increment // the value of the key storage plus increment

DECRBY key decrement // subtract the value of the key stored decrement

 

Special Note:

SET key value [EX seconds] [PX milliseconds] [NX|XX]

From Redis 2.6.12 release,  SET the behavior of commands can be modified by a series of parameters:

  • EX seconds : The key expiration time set to  seconds seconds. Performing   effect equivalent to execution   .SET key value EX secondsSETEX key seconds value
  • PX milliseconds : The key expiration time is set to  milliseconds milliseconds. Performing   effect equivalent to execution   .SET key value PX millisecondsPSETEX key milliseconds value
  • NX : Only when the bond is absent, fishes key set operation. Performing   effect equivalent to execution   .SET key value NXSETNX key value
  • XX : Only when key already exists, fishes key set operation.

Note

Because the  SET command can be achieved through parameters  SETNX ,  SETEX as well as  PSETEX the effect of command, so Redis future versions may be removed and discarded  SETNX ,  SETEX and  PSETEX these three commands.

 

 

String application scenarios:

Single-value cache

  set key value

  get key

Object Caching:

1, set user: 1 value (user: json information format 1)

2、Mset user:1 value1 user:2 value2 ...(key.. value..)

 

Distributed Lock:

SETNX product: 1001 true // returns 1 representative for the lock success

SETNX product: 1001 true // returns 0 for success to acquire the lock, because the key already exists

. . . Execution of business operations

DEL product: 1001 // Business execution is completed, the lock is released

 

set product: 1001 true ex 10 nx // equivalent to setnx set a timeout period, the program is intended to prevent death plug lead to deadlock. And the atomic operations are more and more thread-safe

 

Guess you like

Origin www.cnblogs.com/flycc/p/12670276.html