博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Try Redis(Redis 简介)
阅读量:4058 次
发布时间:2019-05-25

本文共 7667 字,大约阅读时间需要 25 分钟。

Redis is what is called a key-value store, often referred to as a NoSQL database(NOSQL,非关系型). 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"

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

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:

(Tran:INCR命令有些特殊:我们本可以用几行代码就自己实现这个功能(写法就像下面这样简单),为什么还是提供了INCR这个命令呢?)

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.

(Tran:我们预期的结果是12,但是实际返回值却是11,这是由于上述几行代码的增加并非原子操作.而INCR命令是原子操作,所以它将会按照我们的预期来进行增加运算.)

Redis can be told that a key should only exist for a certain length of time(设置key生存时间). 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(TTL:查看key还剩多长时间会被删除). 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.

(Tran:-2表示key已经不存在了,-1表示永不过期. 注意:如果你设置了key.她的TTL将会被重置:-1,即永不过期)

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

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(List:有序,可以存储重复元素). 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.(我们可以在使用key作为list,前提是该key没有以其他数据类型存在redis中)

RPUSH puts the new value at the end of the list.(放在list尾部)

RPUSH friends "Alice"    RPUSH friends "Bob"

LPUSH puts the new value at the start of the list.(放在list头部)

LPUSH friends "Sam"

LRANGE gives a subset of the list(LRANGE获得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.(第二个参数设置成-1意味着取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"

LLEN returns the current length of the list.(LLEN:长度)

LLEN friends => 3

LPOP removes the first element from the list and returns it.(LPOP:删除并返回第一个元素)

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"

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(set无序并且元素唯一). 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"

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.

(SUNION:合并两个或者两个以上的set,返回所有元素(无序且唯一))

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

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.(set是一种十分顺手的数据类型但是它无序的特性限制了它处理一些问题的能力,因此Redis1.2后引入了Sorted Sets-ZSet ,有序集合)

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"

Zrange hackers 0 -1:同样是列出所有元素:

Zrange hackers 0 -1=>    1) "Grace Hopper"    2) "Alan Turing"    3) "Claude Shannon"    4) "Alan Kay"    5) "Richard Stallman"    6) "Sophie Wilson"    7) "Yukihiro Matsumoto"    8) "Linus Torvalds"

Simple strings,lists, 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):

(Tran:哈希是字符串字段和字符串值之间的映射,因此它们是表示对象的完美数据类型)

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:

(Tran:使用HMSET命令可以一次性输入多个属性)

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"

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.

(Tran:散列字段中的数值处理与简单字符串中的数字值完全相同,并且存在以原子方式递增此值的操作)

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

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.


原文地址:

hahs命令链接:
参考:

转载地址:http://wowji.baihongyu.com/

你可能感兴趣的文章
C++常用数学函数
查看>>
【积跬步以至千里】Windows无法访问指定设备,路径或文件,您可能没有合适的权限访问
查看>>
【数据结构基础笔记】第一章绪论之基本概念
查看>>
【数据结构基础笔记】第一章绪论之算法及算法分析
查看>>
【数据结构基础笔记】第二章线性表之基本概念与类型定义
查看>>
【数据结构基础笔记】第二章线性表之顺序表
查看>>
C++报错:无法打开文件“路径\Debug\文件名.exe”
查看>>
【数据结构基础笔记】第二章线性表之单链表
查看>>
【积跬步以至千里】Excel行列互换
查看>>
【YOLO学习笔记】之YOLO初体验
查看>>
【YOLO学习笔记】之YOLO配置文件详解
查看>>
【YOLO学习笔记】之YOLO v1 论文笔记1(超详细:翻译+理解)
查看>>
【YOLO学习笔记】之YOLO v1 论文笔记2(超详细:翻译+理解)
查看>>
【YOLO学习笔记——数据集】之一YOLO数据集制作1(含LabelImg工具讲解)
查看>>
【积跬步以至千里】pdf转word后数字和英文格式别扭,无法修改
查看>>
【YOLO学习笔记——数据集】之一YOLO数据集制作2
查看>>
【深度学习小常识】CPU(中央处理器)和GPU(图像处理器)的区别
查看>>
【人工智能小常识】一篇文章搞懂人工智能、机器学习和深度学习
查看>>
【积跬步以至千里】如何查看浏览器保存的密码
查看>>
【opencv拓展】摄像头基本操作
查看>>