Some commonly used data instructions Redis

The operation is redis key - value stored in the form of
key is a string
Value: String, hash table, list set, set collections, zset five types of collections

19.6.1.String type of operation

Set command : set key value creation instruction
 get command : get key data to obtain
key-value pairs, valid: setex key second value
after setex name 10 zs # 10 seconds, and the name disappears
 set up multiple key-value pairs : mset key1 value1 key2 value2 ... simultaneously inserted
MSET name ZS Age 18 is
l obtaining a plurality of values : mget key1 key2 ...
MSET name Age
l See all keys : keys * See redis all primary keys
l view to key a beginning. : keys a *
keys a *, A * Keys, Keys A

View button:

. 1 View all keys : Keys *
Keys *
. 2 key a view to beginning : a * Keys
Keys a *, a * Keys, Keys a
. 3 to see whether there are key : Key EXISTS
EXISTS name
4. Check the type of key : key1 of the type
of the type name
l set valid key, delete key : expire key seconds
after expire 18500348106 300 # 10 Miao, name disappears

 set key is valid, delete key : expire key seconds (used to make operational verification code)
after 10 # 10 seconds expire name, name disappears
 Check the validity of key : Key ttl
ttl name

Description: Permanent valid key value, showing out of the data valid time is "-1", if it has expired "-2"

flushall, empty
flushall (Do not operate this directive in the company)

19.6.2.Hash instructions

Increased data

1. Increase of the key and value of the field : Field value HSET key
HSET Person name ZS

Attribute to the plurality of domain : Key hmset filed1 VALUE1 Field2 value2 ...
hmset name Person Age 18 is ZS

retrieve data:

1. Obtain the domain attributes (fields) : Field hget Key
hget Person name

2. Get more properties domains : hmget Key filed1 Field2 ...
hmget the Person name Age

All attributes (fields) 3. Get domain : Key hkeys
hkeys Person # shows, name age

delete data:

1. Remove the plurality of attribute values (fields) : Key field1 HDEL Field2 ...
HDEL name Person Age

19.6.3.List instruction

Insert data:

1. The left insert data : LPUSH value2 ... Key VALUE1
. 1 # 2. 3 inserted into the left data lpush num_list

2. Insert the right data : Key RPUSH VALUE1 value2 ...
RPUSH num_list. 1 to the right to insert data # 2. 3

3. Specify the key, the element before the insertion position of the old elements:
Format: Key before linsert oldValue newValue
linsert num_list before. 1 10

4. Specify the key, the position of the insertion element old elements :
Format: Key linsert After oldValue newValue
linsert After num_list. 1 10

retrieve data:

1. start position acquired from the ID number to the stop position of the elements:
Format: Lrange Key start stop
(note point, the last element can represent -1)
Lrange num_list -1 0
NOTE: Be sure to add back subscript space

2. Set the specified index element values : Key index value LSET
LSET num_list index value is the number 0 6 # 0 to 6

delete data:

1. The key field list, count times before, the element deletion value is value :
Format: lrem key count value
count: the number to be deleted
values to be deleted: value
count> 0: Number of scratch start, count <0 from tail head, count = 0 to find all the elements
lrem num_list 2 a # from left to right to delete two a
lrem num_list -2 # right to left to delete a two a
lrem num_list a # 0 to delete all of a

19.7.Set instruction

set type

Features: disordered (order) collection, ordered magnitude relation, can not store duplicate elements
16392

Additive element
1. Add to a plurality of data key fields (set) in
, the member1 member2 is ... key Sadd
Sadd num_set. 1. 4. 4. 5. 6. 7

Gets the element:
2. Get all the elements of a collection of key domains
: smembers key
smembers num_set

Removing elements:
1. Delete the specified value key elements
: Srem key value
Srem num_set 1

19.8.Zset instruction

Features: ordered set, score value is a weight
Sequence: natural order
to increase the data
1. In addition to the plurality score1 key in the set of domains, member1 data

format: Zadd key score1 the member1 score2 memever2 ...
Zadd ZS 20 is the nums 10 Lisi

Data acquisition:
1. Return member element within the specified range:

Format: Z Range The STOP Key Start
Start: to start index, comprising
stop: ending index, comprising, -1 is the last element
zrange nums 0 -1

2. Get the value member between the min and max weights (inclusive):
Format: min max zrangebyscore Key
zrangebyscore the nums 0 230

3. Get score key domain member of the set of weight values:
Format: Key member zscore
zscore the nums ZS

Removing elements:
1. Delete the specified field in the collection element values:
Format: zrem Key member1 member2 ...
zrem nums ZS lisi

2. Remove the weights set lies within a specified range (min, max) elements:
Format: min max zremrangebyscore Key
zremrangebyscore 20 is the nums 0

Published 14 original articles · won praise 4 · Views 248

Guess you like

Origin blog.csdn.net/Red_rose9/article/details/105257213