Redis.2017-08-13

Redis官网
try redis

Tutorial

1. SET, GET

SET server:name "fido"
GET server:name => "fido"

2. INCR, DEL

# INCR原子操作,保证多线程情况下更新值问题
SET connections 10
INCR connections => 11
INCR connections => 12
DEL connections 
INCR connections => 1

3. EXPIRE, TTL

#  A -1 for the TTL of the key means that it will never expire.
# Note that if you SET a key, its TTL will be reset.

SET resource:lock "Redis Demo 1"
EXPIRE resource:lock 120
TTL resource:lock => 119
SET resource:lock "Redis Demo 2"
TTL resource:lock => -1

4. List structure

# RPUSH, LPUSH, LLEN, LRANGE, LPOP, RPOP

RPUSH friends "Alice"
RPUSH friends "Bob"
# puts the new value at the end of the list.
LPUSH friends "Sam"
# puts the new value at the start of the list.
LRANGE friends 0 -1 => 1) "Sam", 2) "Alice", 3) "Bob"
LRANGE friends 0 1 => 1) "Sam", 2) "Alice"
LRANGE friends 1 2 => 1) "Alice", 2) "Bob"
# LPOP \ RPOP 都有删除效果

5. Set structure

A set is similar to a list, except it does not have a specific order and each element may only appear once.

# SADD, SREM, SISMEMBER, SMEMBERS, SUNION

SADD superpowers "flight"
SADD superpowers "x-ray vision"
SADD superpowers "reflexes"
# adds the given value to the set.
SREM superpowers "reflexes"
# removes the given value from the set.

SISMEMBER superpowers "flight" => 1
SISMEMBER superpowers "reflexes" => 0
# tests if the given value is in the set. It returns 1 if the value is there and 0 if it is not.
SMEMBERS superpowers => 1) "flight", 2) "x-ray vision"
# returns a list of all the members of this set.

SADD birdpowers "pecking"
SADD birdpowers "flight"
SUNION superpowers birdpowers => 1) "pecking", 2) "x-ray vision", 3) "flight"
# combines two or more sets and returns the list of all elements.

6. Sorted Sets【Redis 1.2 introduced】

A sorted set is similar to a regular set, but now each value has an associated score. This score is used to sort the elements in the set.

# ZADD, ZRANGE
ZADD hackers 1940 "Alan Kay"
ZADD hackers 1906 "Grace Hopper"
ZADD hackers 1953 "Richard Stallman"
ZADD hackers 1965 "Yukihiro Matsumoto"
ZADD hackers 1916 "Claude Shannon"
ZADD hackers 1969 "Linus Torvalds"
ZADD hackers 1957 "Sophie Wilson"
ZADD hackers 1912 "Alan Turing"

ZRANGE hacker  2 4 => 1) "Claude Shannon", 2) "Alan Kay", 3) "Richard Stallman"

7. Hashes

# HSET, HGETALL, HMSET, HGET
HSET user:1000 name "John Smith"
HSET user:1000 email "[email protected]"
HSET user:1000 password "s3cret"
HGETALL user:1000

HMSET user:1001 name "Mary Jones" password "hidden" email "[email protected]"
HGET user:1001 name => "Mary Jones"
# HINCRBY, HDEL
HSET user:1000 visits 10
HINCRBY user:1000 visits 1 => 11 HINCRBY user:1000 visits 10 => 21 HDEL user:1000 visits
HINCRBY user:1000 visits 1 => 1

Check the full list of Hash commands for more information.

8. Check out the following links to continue learning about Redis.

Redis Documentation
Command Reference
Implement a Twitter Clone in Redis
Introduction to Redis Data Types

你可能感兴趣的:(Redis.2017-08-13)