redis中的数据类型

时间:2023-02-09 11:03:11

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中的数据类型的更多相关文章

  1. redis中各种数据类型对应的jedis操作命令

    redis中各种数据类型对应的jedis操作命令 一.常用数据类型简介: redis常用五种数据类型:string,hash,list,set,zset(sorted set). 1.String类型 ...

  2. 关于Redis中的数据类型

    一. Redis常用数据类型 Redis最为常用的数据类型主要有以下: String Hash List Set Sorted set 一张图说明问题的本质 图一: 图二: 代码: /* Object ...

  3. Redis 中的数据类型及基本操作

    Redis 内置的数据类型有 5种:字符串String.哈希Hash.列表List.集合Set.有序集合ZSet 字符串类型 String 是 Redis 中最基本的类型,一个 key 对应着一个 v ...

  4. redis中各种数据类型的常用操作方法汇总

    在spring中使用jedisTemplate操作,详见https://www.cnblogs.com/EasonJim/p/7803067.html 一.Redis的五大数据类型 1.String( ...

  5. redis中hash数据类型

    remoteSelf:1>hset website google "www.google.com" "1" remoteSelf:1>hget we ...

  6. 解析Redis操作五大数据类型常用命令

    摘要:分享经常用到一些命令和使用场景总结,以及对Redis中五大数据类型如何使用cmd命令行的形式进行操作的方法. 本文分享自华为云社区<Redis操作五大数据类型常用命令解析>,作者:灰 ...

  7. Redis中的Stream数据类型作为消息队列的尝试

    Redis的List数据类型作为消息队列,已经比较合适了,但存在一些不足,比如只能独立消费,订阅发布又无法支持数据的持久化,相对前两者,Redis Stream作为消息队列的使用更为有优势.   相信 ...

  8. Redis中3种特殊的数据类型(BitMap、Geo和HyperLogLog)

    前言 Reids 在 Web 应用的开发中使用非常广泛,几乎所有的后端技术都会有涉及到 Redis 的使用.Redis 种除了常见的字符串 String.字典 Hash.列表 List.集合 Set. ...

  9. 面试中经常问到的Redis七种数据类型,你都真正了解吗?

    前言 Redis不是一个简单的键值对存储,它实际上是一个支持各种类型数据结构的存储.在传统的键值存储中,是将字符串键关联到字符串值,但是在Redis中,这些值不仅限于简单的字符串,还可以支持更复杂的数 ...

随机推荐

  1. PHP编程效率的20个要点

    用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则 不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数” 用 单引号代替双引 ...

  2. linux日志审计项目案例实战(生产环境日志审计项目解决方案)

    所谓日志审计,就是记录所有系统及相关用户行为的信息,并且可以自动分析.处理.展示(包括文本或者录像) 推荐方法:sudo配合syslog服务,进行日志审计(信息较少,效果不错) 1.安装sudo命令. ...

  3. 在KALI LINUX中安装JAVA JDK

    1. 下载最新的JAVA JDK jdk-8u91-linux-x64 2. 解压缩文件并移动至/opt tar -xzvf jdk-8u91-linux-x64.tar.gz mv jdk1.8.0 ...

  4. Android开发中用到的框架、库介绍

    Android开发中用到的框架介绍,主要记录一些比较生僻的不常用的框架,不断更新中...... 网路资源:http://www.kuqin.com/shuoit/20140907/341967.htm ...

  5. C语言使用正则表达式

    http://blog.chinaunix.net/uid-479984-id-2114941.html C语言使用正则表达式 据说一个好的程序员是会使用DB和Regular Expression的程 ...

  6. apache &period;htaccess文件详解和配置技巧总结

    一..htaccess的基本作用 .htaccess是一个纯文本文件,它里面存放着Apache服务器配置相关的指令.       .htaccess主要的作用有:URL重写.自定义错误页面.MIME类 ...

  7. Flask学习笔记(3)--路由

    0x01 参数传递 传递参数的语法是: /<参数名>/,然后在视图函数中,也要定义同名的参数. 参数的数据类型: 1.如果没有指定具体的数据类型,那么默认就是使用string 数据类型. ...

  8. &lbrack;学习笔记&rsqb;Java的public&comma;protected&comma;private&comma;缺省的作用域

    0.引言 Java的访问指示符public,protected,private,缺省可以用来修饰类和方法. 1.作用域如下 具体如下: 作用域       当前类    同一package   子孙类 ...

  9. &lbrack;LightOJ 1265&rsqb; Island of Survival

    Island of Survival You are in a reality show, and the show is way too real that they threw into an i ...

  10. IE浏览器如何调试Asp&period;net的 js代码

    不管我们开发什么项目,都需要使用调试.后端的调试比较简单.前端js调试稍微复杂了一点,但是也别怕,因为我们有很多调试前端js代码的浏览器工具.比如IE浏览器.firefox浏览器.chrome浏览器等 ...