Redis 的数据类型和命令帮助

news/2024/7/6 0:44:56

文章结构

  • Redis 数据类型
    • 1. Redis全局命令(跟key有关系,而跟value无关)
    • 2. Strings
      • Getting and setting Strings
      • Managing counters
    • 3. Lists(L)
      • Basic commands
      • Blocking commands
    • 4. Sets(S)
      • Basic commands
    • 5. Hashes(H)
      • Basic commands
    • 6. Sorted sets(Z)
      • Basic commands
    • 7. Bitmaps
      • Basic commands
    • 8. HyperLogLog(pf开头,发明算法的人的简写)
      • Basic commands
    • 9. Geospatial(Geo)
      • Basic commands
  • Redis 应用
  • Reids 命令帮助或资源

Redis 数据类型

在 redis7 版本中,数据类型有 10 种

  • 常规的 5 种数据类型(Strings、Lists、Sets、Sorted sets、Hashs
  • 5 种不常见的(Geospatial、Streams、HyperLogLogBitmaps、Bitfields)

备注:本文只介绍其中标粗的常用部分

官方参考

# 各种数据类型总览(含总览和详细命令参考)
https://redis.io/docs/data-types/

# 官方数据类型教程
https://redis.io/docs/data-types/tutorial/

# 官方在线尝试及人门教程
https://try.redis.io/

以下的数据类型介绍参考了官网

1. Redis全局命令(跟key有关系,而跟value无关)

注意:下面的这些命令跟 value 的无关,只跟 key 有关系

  • Keys pattern

  • Exists key

  • del key

  • Expire key second

  • Ttl key

  • Type key

2. Strings

Getting and setting Strings

  • SET stores a string value

  • GET retrieves a string value

  • SETNX stores a string value only if the key doesn’t already exist. Useful for implementing locks

    对于实现锁很有用

  • MGET retrieves multiple string values in a single operation

Managing counters

  • INCRBY atomically increments (and decrements when passing a negative number) counters stored at a given key

    为什么要有 INCR 等这些命令,因为它们是原子的

    举例:

    > INCR views:page:2
    (integer) 1
    > INCRBY views:page:2 10
    (integer) 11
    

3. Lists(L)

redis 的 list 用的是链表结构!

用途:

1、记住最新的更新(如网络上的最近10条数据)

记住最新的记录(如lpush和ltrim和lrange的配合可以获取最新的记录,ltrim会删除范围外的其他数据只保留范围内的最新记录)

2、2个进程的交流(如生产者消费者)

Basic commands

  • LPUSH adds a new element to the head of a list; RPUSH adds to the tail
  • LPOP removes and returns an element from the head of a list; RPOP does the same but from the tails of a list
  • LLEN returns the length of a list
  • LMOVE atomically moves elements from one list to another
  • LTRIM reduces a list to the specified range of elements

Blocking commands

常用于生产者消费者模式???

支持不同的阻塞命令

  • BLPOP removes and returns an element from the head of a list. If the list is empty, the command blocks until an element becomes available or until the specified timeout is reached

    要么阻塞要么超时

4. Sets(S)

唯一,但是无序

Basic commands

  • SADD adds a new member to a set

  • SREM removes the specified member from the set

  • SISMEMBER tests a string for set membership

  • SINTER returns the set of members that two or more sets have in common (i.e., the intersection)

    交集:sinter

    差集:sdiff

    并集:sunion

  • SCARD returns the size (a.k.a. cardinality) of a set

5. Hashes(H)

非常适合代表“对象”、效率非常高效

Basic commands

  • HSET sets the value of one or more fields on a hash
  • HGET returns the value at a given field
  • HMGET returns the values at one or more given fields
  • HINCRBY increments the value at a given field by the integer provided

6. Sorted sets(Z)

既有 set 的特征(key不重复)也有 hash 的特征(score,一个key对应一个分数)

基本同set,但是有一个分数;所以非常适合用于获取范围的元素,例如:前10,最后10个

Basic commands

  • ZADD adds a new member and associated score to a sorted set. If the member already exists, the score is updated

  • ZRANGE returns members of a sorted set, sorted within a given range

  • ZRANK returns the rank of the provided member, assuming the sorted is in ascending order

    排名:获取前多少的元素

  • ZREVRANK returns the rank of the provided member, assuming the sorted set is in descending order

7. Bitmaps

是 String 数据类型的拓展,可以对象 string 像一个 bit 的向量;因为只能设置 0 和 1,所以适合是否判断的情况

1、操作上分为两组:设置获取值和对组的统计(统计值)

2、判断是否时,提供极大的空间节省(比如配合自增长id,就可以使用512M的空间判断4亿人是否在位图中)

Basic commands

  • SETBIT sets a bit at the provided offset to 0 or 1

  • GETBIT returns the value of a bit at a given offset

  • BITOP lets you perform bitwise operations against one or more strings

    备注:位操作

8. HyperLogLog(pf开头,发明算法的人的简写)

是一个概率性的数据结构,用来估算一个 set 的基数(基数就是不重复元素),是一种概率算法存在一定的误差,占用内存只有12kb但是非常适合超大数据量的统计,比如网站访客的统计

Basic commands

  • PFADD adds an item to a HyperLogLog

  • PFCOUNT returns an estimate of the number of items in the set

    返回基数的估算值

  • PFMERGE combines two or more HyperLogLogs into one

9. Geospatial(Geo)

地理位置坐标,即经纬度

Basic commands

  • geoadd:添加地理位置的坐标

  • geopos:获取地理位置的坐标

  • geodist:计算两个位置之间的距离

  • georadius:根据用户给定的经纬度坐标来获取指定范围内的地理位置集合

    以某个点为中心,半径多少的范围

  • geohash:返回一个或多个位置对象的 geohash 值

    备注:

    1、返回 hash 值是为了不丢失精度

    2、可以根据返回的 hash 值反向计算出经纬度

Redis 应用

案例 1:生成一个 6 为数字的验证码,每天只能发送 3 次,5 分钟内有效

1、生成 6 个数字验证码(randon类)

2、计数的工具(redis的incr。 并且设计过期时间为24 * 60 * 60秒)

3、吧生成的验证码放入 redis 中

步骤:

1、校验是否满足次数要求

2、生成验证码放入 redis,并修改次数

3、对用户提交的验证码做

Reids 命令帮助或资源

Redis 官网:https://redis.io

源码地址:https://github.com/redis/redis

Redis 在线测试:http://try.redis.io

Redis 命令参考:http://doc.redisfans.com、https://redis.io/commands(把命令按类 group 进行了分组)

获取 Redis 命令帮助:

1、直接用命令行获取参数的帮助

2、在官方文档的命令帮助中可按组(group)或命令(command)直接查询

传送门:保姆式Spring5源码解析

欢迎与作者一起交流技术和工作生活

联系作者


http://lihuaxi.xjx100.cn/news/1170272.html

相关文章

flask实现简易图书管理系统

项目结构 技术选型 flask 做后端, 提供数据和渲染html 暂时没有提供mysql, 后续会更新操作mysql和样式美化的版本 起一个flask服务 flask是python的一个web框架, 下面演示如何提供http接口, 并返回json数据 main.py # flask创建http接口 from flask import Flask, request, jso…

SelFlow: Self-Supervised Learning of Optical Flow

本文提出了一种用于光流的自监督学习方法。该方法从非遮挡像素中提取可靠的流估计,并使用这些预测作为真值来学习幻觉遮挡的光流。 本文设计了一个简单的 CNN,以利用 来自多个帧的时间信息 来更好地进行流估计。本方法可以在 MPI Sintel、KITTI 2012 和 …

jenkins 使用教程

1 拉取镜像 docker pull jenkins/jenkins 2 mkdir-p /usr/local/docker/jenkins_docker cd usr/local/docker/jenkins_docker mkdir data 编写 vim docker-compose.yml version: "1.0.0" services: jenkins: image: jenkins/jenkins containe…

JDK动态代理和CGLIB动态代理

JDK动态代理和CGLIB动态代理 JDK动态代理和CGLIB动态代理 JDK动态代理和CGLIB动态代理 ① JDK动态代理只提供接口的代理,不支持类的代理,要求被代理类实现接口。JDK动态代理的核心是InvocationHandler接口和Proxy类,在获取代理对象时&#x…

Three.js--》实现3d圣诞贺卡展示模型

目录 项目搭建 初始化three.js基础代码 加载环境模型 设置环境纹理 添加水面并设置阴影效果 实现幽灵小球的运动 实现相机切换和文字切屏 实现漫天星星和爱心样式 今天简单实现一个three.js的小Demo,加强自己对three知识的掌握与学习,只有在项目…

Proxy的实现

1.Proxy的实现 (1)New创建的proxy(不可撤销) var pro new Proxy(person,{ get:function(target,property){ return ls } set:function(target,property,value){ return } }) get方法:target参数表示所要拦截的目标…

删除Windows11和WIN10桌面图标小箭头

方法一: 1.右键“开始”菜单,找到“运行”,输入命令 regedit ,打开“注册表编辑器” 2.在“注册表编辑器”左侧窗口,按照以下路径找到对应项 HKEY_CLASSES_ROOT\lnkfile 3.选择右侧窗口的lsShortcut项,右击lsShortcut项,点击删除选项,将lsShortcut删除即可 4.重启电…

探索编程的极限:挑战炫技代码

程序员常常被视为具有超强技术能力的人才,而他们手中的代码也往往充满了令普通人惊叹的炫技操作。作为程序员的我,将和大家分享一些炫技的代码写法 一、编程语言介绍 本人主攻Java。下面我将介绍一下Java语言。 Java是一种广泛使用的高级编程语言&…