1. 程式人生 > >redis中的數據類型

redis中的數據類型

is a set 包含 binary because fin floating new long inter

redis不是一個純文本kv存儲,實際上,它是一個數據結構服務,支持不同類型的value。

包含以下類型:

1.Binary-safe strings. 二進制安全的字符串

2.Lists: collections of string elements sorted according to the order of insertion. 按照插入順序排序
 They are basically linked lists. 基於鏈表

3.Sets: collections of unique, unsorted string elements. 集合,唯一且無序

4.Sorted sets, similar to Sets but where every string element is associated to a floating number value, called score. 有序集合
 The elements are always taken sorted by their score, so unlike Sets it is possible to retrieve a range of elements (for example you may ask: give me the top 10, or the bottom 10).

5.Hashes, which are maps composed of fields associated with values. 散列,類似python的字典
 Both the field and the value are strings. This is very similar to Ruby or Python hashes.

6.Bit arrays (or simply bitmaps): it is possible, using special commands, 位圖
 to handle String values like an array of bits: you can set and clear individual bits, count all the bits set to 1, find the first set or unset bit, and so forth.

7.HyperLogLogs: this is a probabilistic data structure which is used in order to estimate the cardinality of a set. 超重對數,估算集合的基數
 Don‘t be scared, it is simpler than it seems... See later in the HyperLogLog section of this tutorial.

關於key:

Redis keys are binary safe, this means that you can use any binary sequence as a key, from a string like "foo" to the content of a JPEG file.
The empty string is also a valid key.

Very long keys are not a good idea.
Very short keys are often not a good idea.
Try to stick with a schema.
The maximum allowed key size is 512 MB.

key是二進制安全的,可以使用任意二進制序列作為key,包括純字符串甚至是一個JPEG文件。
最大允許大小為512MB。


Strings

字符串類型是最簡單的數據類型,是Memcached中唯一的數據類型,適合新手使用。

GET key 獲取值
MGET key [key ...] 獲取多個給定的鍵的值
GETSET key value 返回舊值並設置新值
STRLEN key 字符串長度
APPEND key value 追加字符串。如果鍵存在就追加,如果不存在就等同於SET key value
GETRANGE key start end 獲取子串,索引值從0開始,支持負索引
SETRANGE key offset value 從指定索引處開始覆蓋字符串,返回覆蓋後字符串長度


Lists

列表是基於鏈表實現的。具備鏈表的特性。

The LPUSH command adds a new element into a list, on the left (at the head),
while the RPUSH command adds a new element into a list ,on the right (at the tail).
Finally the LRANGE command extracts ranges of elements from lists.

rpush mylist A
(integer) 1
rpush mylist B
(integer) 2
lpush mylist first
(integer) 3
lrange mylist 0 -1
1) "first"
2) "A"
3) "B"

Redis returned a NULL(nil) value to signal that there are no elements in the list.

Redis implements commands called BRPOP and BLPOP which are versions of RPOP and LPOP able to block if the list is empty,
they‘ll return to the caller only when a new element is added to the list, or when a user-specified timeout is reached.

列表可以當做隊列使用,需要配合BRPOP和BLPOP指令使用。


Hashes

哈希常用於表示對象object。

hmset user:1000 username antirez birthyear 1977 verified 1
OK
hget user:1000 username
"antirez"
hget user:1000 birthyear
"1977"
hgetall user:1000
1) "username"
2) "antirez"
3) "birthyear"
4) "1977"
5) "verified"
6) "1"

hmget user:1000 username birthyear no-such-field
1) "antirez"
2) "1977"
3) (nil)


Sets

集合是無序字符串的組合。

SCARD provides the number of elements inside a set. 返回集合中元素的數量
This is often called the cardinality of a set in the context of set theory.

SRANDMEMBER key [count]
隨機返回集合中指定個數的元素如果 count 為正數,且小於集合基數,那麽命令返回一個包含 count 個元素的數組,數組中的元素各不相同。如果 count 大於等於集合基數,那麽返回整個集合
如果 count 為負數,那麽命令返回一個數組,數組中的元素可能會重復出現多次,而數組的長度為 count 的絕對值
如果 count 為 0,返回空
如果 count 不指定,隨機返回一個元素

差集
SDIFF key [key ...] 從第一個key的集合中去除其他集合和自己的交集部分
SDIFFSTORE destination key [key ...] 將差集結果存儲在目標key中

交集
SINTER key [key ...] 取所有集合交集部分
SINTERSTORE destination key [key ...] 將交集結果存儲在目標key中

並集
SUNION key [key ...] 取所有集合並集
SUNIONSTORE destination key [key ...] 將並集結果存儲在目標key中


Sorted sets

Sorted sets are a data type which is similar to a mix between a Set and a Hash.
Like sets, sorted sets are composed of unique, non-repeating string elements, so in some sense a sorted set is a set as well.

However while elements inside sets are not ordered, every element in a sorted set is associated with a floating point value, called the score
this is why the type is also similar to a hash, since every element is mapped to a value.

有序集合是set和hash的混合。本質上,它仍然是一個set,不同的是它的每個元素都關聯了一個浮點數value。

They are ordered according to the following rule:

  • If A and B are two elements with a different score, then A > B if A.score is > B.score.
  • If A and B have exactly the same score, then A > B if the A string is lexicographically greater than the B string.
    A and B strings can‘t be equal since sorted sets only have unique elements.

zadd hackers 1940 "Alan Kay"
(integer) 1
zadd hackers 1957 "Sophie Wilson"
(integer) 1
zadd hackers 1953 "Richard Stallman"
(integer) 1
zadd hackers 1949 "Anita Borg"
(integer) 1
zadd hackers 1965 "Yukihiro Matsumoto"
(integer) 1
zadd hackers 1914 "Hedy Lamarr"
(integer) 1
zadd hackers 1916 "Claude Shannon"
(integer) 1
zadd hackers 1969 "Linus Torvalds"
(integer) 1
zadd hackers 1912 "Alan Turing"
(integer) 1

As you can see ZADD is similar to SADD, but takes one additional argument (placed before the element to be added) which is the score.

With sorted sets it is trivial to return a list of hackers sorted by their birth year because actually they are already sorted.

What if I want to order them the opposite way, youngest to oldest? Use ZREVRANGE instead of ZRANGE:

zrevrange hackers 0 -1
1) "Linus Torvalds"
2) "Yukihiro Matsumoto"
3) "Sophie Wilson"
4) "Richard Stallman"
5) "Anita Borg"
6) "Alan Kay"
7) "Claude Shannon"
8) "Hedy Lamarr"
9) "Alan Turing"

It is possible to return scores as well, using the WITHSCORES argument:

zrange hackers 0 -1 withscores
1) "Alan Turing"
2) "1912"
3) "Hedy Lamarr"
4) "1914"
5) "Claude Shannon"
6) "1916"
7) "Alan Kay"
8) "1940"
9) "Anita Borg"
10) "1949"
11) "Richard Stallman"
12) "1953"
13) "Sophie Wilson"
14) "1957"
15) "Yukihiro Matsumoto"
16) "1965"
17) "Linus Torvalds"
18) "1969"

Operating on ranges

zrangebyscore hackers -inf 1950
1) "Alan Turing"
2) "Hedy Lamarr"
3) "Claude Shannon"
4) "Alan Kay"
5) "Anita Borg"

We asked Redis to return all the elements with a score between negative infinity and 1950 (both extremes are included).

It‘s also possible to remove ranges of elements. Let‘s remove all the hackers born between 1940 and 1960 from the sorted set:

zremrangebyscore hackers 1940 1960
(integer) 4

Another extremely useful operation defined for sorted set elements is the get-rank operation.
It is possible to ask what is the position of an element in the set of the ordered elements.
The ZREVRANK command is also available in order to get the rank, considering the elements sorted a descending way.

zrank hackers "Anita Borg"
(integer) 4

常見使用場景,排行榜。

Updating the score: leader boards

Sorted sets‘ scores can be updated at any time.
Just calling ZADD against an element already included in the sorted set will update its score (and position) with O(log(N)) time complexity.

ZCARD key 返回有序集合中元素的個數
ZCOUNT key min max 返回指定score範圍內元素的個數
ZSCORE key member 顯示分值
ZINCRBY key increment member 增加或減少分值。increment為負數就是減少
ZRANGE key start stop [WITHSCORES] 返回指定索引區間元素
ZREVRANGE key start stop [WITHSCORES] 返回指定索引區間元素,逆序
ZRANK key member 返回元素的排名(索引)
ZREVRANK key member 返回元素的逆序排名(索引)

參考:
https://redis.io/topics/data-types
https://redis.io/topics/data-types-intro

redis中的數據類型