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 "john.smith@example.com"
HSET user:1000 password "s3cret"
HGETALL user:1000

HMSET user:1001 name "Mary Jones" password "hidden" email "mjones@example.com"
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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,559评论 0 23
  • 周五下午,很久都没出来看电影了。决定带儿子去看一次电影。适合孩子看的只有《熊出没》,但是我对这部动画片印象很差,光...
    做个纯真的孩子阅读 3,188评论 0 3
  • 混沌, 才滋生情趣, 因为永远出其不意; 一切试图, 建立秩序的努力, 都是破灭的前奏曲; 不去期待, 才能为每一...
    2souls阅读 3,582评论 3 50
  • 有人的地方,就有江湖。 职场更不会例外。 我们都是平凡人,没有谁会得到额外的加持。 也没有那么多的逆袭,也不会有太...
    遇见琉璃阅读 3,567评论 0 1

友情链接更多精彩内容