设为首页 收藏本站
查看: 552|回复: 0

[经验分享] Redis TUTORIAL

[复制链接]

尚未签到

发表于 2016-12-17 09:28:32 | 显示全部楼层 |阅读模式


 

 
  Welcome to Try Redis, a demonstration of the Redis database!
  Please type TUTORIAL to begin a brief tutorial, HELP to see a list of supported commands, or any valid Redis command to play with the database.



> TUTORIAL



  Redis is what is called a key-value store, often referred to as a NoSQL database. The essence of a key-value store is the ability to store some data, called a value, inside a key. This data can later be retrieved only if we know the exact key used to store it. We can use the command SET to store the value "fido" at key "server:name":


SET server:name "fido"

  Redis will store our data permanently, so we can later ask "What is the value stored at key server:name?" and Redis will reply with "fido":


GET server:name => "fido"

   Tip: You can click the commands above to automatically execute them. The text after the arrow (=>) shows the expected output.
Type NEXT to continue the tutorial.





> NEXT



  Other common operations provided by key-value stores are DEL to delete a given key and associated value, SET-if-not-exists (called SETNX on Redis) that sets a key only if it does not already exist, and INCR to atomically increment a number stored at a given key:


SET connections 10
INCR connections => 11
INCR connections => 12
DEL connections
INCR connections => 1

Type NEXT to continue the tutorial.





> NEXT



  There is something special about INCR. Why do we provide such an operation if we can do it ourself with a bit of code? After all it is as simple as:

x = GET count
x = x + 1
SET count x

  The problem is that doing the increment in this way will only work as long as there is a single client using the key. See what happens if two clients are accessing this key at the same time:


  • Client A reads count as 10.
  • Client B reads count as 10.
  • Client A increments 10 and sets count to 11.
  • Client B increments 10 and sets count to 11.
  We wanted the value to be 12, but instead it is 11! This is because incrementing the value in this way is not an atomic operation. Calling the INCR command in Redis will prevent this from happening, because it is an atomic operation. Redis provides many of these atomic operations on different types of data.
Type NEXT to continue the tutorial.





> NEXT



  Redis can be told that a key should only exist for a certain length of time. This is accomplished with the EXPIRE and TTL commands.


SET resource:lock "Redis Demo"
EXPIRE resource:lock 120

  This causes the key resource:lock to be deleted in 120 seconds. You can test how long a key will exist with the TTL command. It returns the number of seconds until it will be deleted.


TTL resource:lock => 113
// after 113s
TTL resource:lock => -2

  The -2 for the TTL of the key means that the key does not exist (anymore). 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

Type NEXT to continue the tutorial.





> NEXT


list


  Redis also supports several more complex data structures. The first one we'll look at is a list. A list is a series of ordered values. Some of the important commands for interacting with lists are RPUSH, LPUSH, LLEN, LRANGE, LPOP, and RPOP. You can immediately begin working with a key as a list, as long as it doesn't already exist as a different type.
  RPUSH puts the new value at the end of the list.


RPUSH friends "Alice"
RPUSH friends "Bob"

  LPUSH puts the new value at the start of the list.


LPUSH friends "Sam"

  LRANGE gives a subset of the list. It takes the index of the first element you want to retrieve as its first parameter and the index of the last element you want to retrieve as its second parameter. A value of -1 for the second parameter means to retrieve elements until the end 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"

Type NEXT to continue the tutorial.





> NEXT



  LLEN returns the current length of the list.


LLEN friends => 3

  LPOP removes the first element from the list and returns it.


LPOP friends => "Sam"

  RPOP removes the last element from the list and returns it.


RPOP friends => "Bob"

  Note that the list now only has one element:


LLEN friends => 1
LRANGE friends 0 -1 => 1) "Alice"

Type NEXT to continue the tutorial.





set
  The next data structure that we'll look at is a set. A set is similar to a list, except it does not have a specific order and each element may only appear once. Some of the important commands in working with sets are SADD, SREM, SISMEMBER, SMEMBERS and SUNION.
  SADD adds the given value to the set.

    SADD superpowers "flight"
SADD superpowers "x-ray vision"
SADD superpowers "reflexes"

  SREM removes the given value from the set.

    SREM superpowers "reflexes"

Type NEXT to continue the tutorial.





> NEXT



  SISMEMBER tests if the given value is in the set. It returns 1 if the value is there and 0 if it is not.

    SISMEMBER superpowers "flight" => 1
SISMEMBER superpowers "reflexes" => 0

  SMEMBERS returns a list of all the members of this set.

    SMEMBERS superpowers => 1) "flight", 2) "x-ray vision"

  SUNION combines two or more sets and returns the list of all elements.

    SADD birdpowers "pecking"
SADD birdpowers "flight"
SUNION superpowers birdpowers => 1) "pecking", 2) "x-ray vision", 3) "flight"

 





Sorted Sets


  Sets are a very handy data type, but as they are unsorted they don't work well for a number of problems. This is why Redis 1.2 introduced Sorted Sets.
  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 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"

  In these examples, the scores are years of birth and the values are the names of famous hackers.

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



Hashes


  Simple strings, sets and sorted sets already get a lot done but there is one more data type Redis can handle: Hashes.
  Hashes are maps between string fields and string values, so they are the perfect data type to represent objects (eg: A User with a number of fields like name, surname, age, and so forth):

    HSET user:1000 name "John Smith"
HSET user:1000 email "john.smith@example.com"
HSET user:1000 password "s3cret"

  To get back the saved data use HGETALL:

    HGETALL user:1000

  You can also set multiple fields at once:

    HMSET user:1001 name "Mary Jones" password "hidden" email "mjones@example.com"

  If you only need a single field value that is possible as well:

    HGET user:1001 name => "Mary Jones"

Type NEXT to continue the tutorial.





> NEXT



  Numerical values in hash fields are handled exactly the same as in simple strings and there are operations to increment this value in an atomic way.


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.
Type NEXT to continue the tutorial.





> NEXT



  That wraps up the Try Redis tutorial. Please feel free to goof around with this console as much as you'd like.
  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








  This site was originally written by Alex McHale (github twitter) and inspired by Try Mongo. It's now maintained and hosted by Jan-Erik Rediger (github twitter)
  The source code to Try Redis is available on GitHub.


   启动和关闭
  cd 到 redis 目录,启动redis服务器
  $ src/redis-server
  开一个redis终端
  $ src/redis-cli
  关闭
  在 redis-cli 端  shutdown ,命令关闭 redis-server
  quit  退出 redis-cli
  在python中使用redis

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-315396-1-1.html 上篇帖子: Redis学习笔记(二)——Redis的准备 下篇帖子: 备份redis
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表