🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 5种数据类型 -hash哈希类型数据,赋值,获取 ``` <pre class="calibre14">``` hset car name bm hset car price <span class="token3">500</span> hmset car name bm price <span class="token3">500</span> 一次多条数据 hget car name hmget car name price 多条 hgetall car 获取全部键值 hexists car price 判断字段是否存在 存在返回<span class="token3">1</span> 键不存在也返回<span class="token3">0</span> car是键 hsetnx car id <span class="token3">456</span> 赋值<span class="token2">,</span>id存在将不会改变值 hincrby car id <span class="token3">1</span> 增加数字可以是任何正负数<span class="token2">,</span>返回数字 hdel car id 删除字段<span class="token2">,</span>支持多个返回删除个数 hkeys car 获取键的字段名 hvals car 获取值 hlen car 返回字段数量 ``` ``` - list列表类型 ``` <pre class="calibre14">``` lpush id <span class="token3">1</span> 左边增加元素<span class="token2">,</span>支持多个值 rpush id <span class="token3">1</span> <span class="token3">34</span> 右边增加元素<span class="token2">,</span>返回长度<span class="token2">,</span>支持多个值 lpop id 左边删除元素<span class="token3">1</span>个<span class="token2">,</span>返回元素值 rpop id 右边删除元素<span class="token3">1</span>个 brpop list1 list2 <span class="token3">0</span> 阻塞模式用于消息队列 blpop类似 llen id 返回元素个数 lrange id <span class="token3">0</span> <span class="token3">2</span> 返回截取值<span class="token2">,</span>包含索引两端<span class="token2">,</span><span class="token">-</span><span class="token3">1</span>表示最右端` lrange id <span class="token3">0</span> <span class="token">-</span><span class="token3">1</span>`为全部值 lrem id <span class="token3">1</span> <span class="token3">9</span> 从左边删除<span class="token3">1</span>个元素值为<span class="token3">9</span>的值<span class="token2">,</span>个数<span class="token3">1</span><span class="token2">;</span> lrem id <span class="token">-</span><span class="token3">2</span> <span class="token3">9</span> 从右边删除<span class="token3">2</span>个 lrem id <span class="token3">0</span> <span class="token3">9</span>删除全部 lindex id <span class="token3">0</span> 返回索引是<span class="token3">0</span>的元素值 <span class="token">-</span><span class="token3">1</span><span class="token2">;</span> lindex id <span class="token">-</span><span class="token3">1</span> 表示右边第一个 lset id <span class="token3">0</span> <span class="token3">999</span> 设置索引<span class="token3">0</span>元素值<span class="token2">,</span>索引可以负数 ltrim id <span class="token3">0</span> <span class="token">-</span><span class="token3">1</span> 删除指定索引元素 和lrange类似 linsert id before <span class="token3">999</span> <span class="token3">222</span> 从左向右查找<span class="token3">999</span> 在之前插入<span class="token3">222</span><span class="token2">,</span>返回元素个数<span class="token2">,</span>没有找到返回<span class="token">-</span><span class="token3">1</span><span class="token2">,</span>不插入 linsert id after <span class="token3">999</span> <span class="token3">0</span> 在之后插入<span class="token3">0</span> rpoplpush id id1 从id队尾取出元素<span class="token3">1</span>个插入id1队首<span class="token2">,</span>返回这个元素值 ``` ``` - set 集合类型,元素没有顺序,元素唯一 ``` <pre class="calibre14">``` sadd id a b <span class="token3">1</span> 添加元素a b <span class="token3">1</span>返回元素个数<span class="token2">,</span>有相同元素不会插入<span class="token2">,</span> smembers id 获取集合值<span class="token2">,</span>返回所有元素 sismember id <span class="token3">24</span> 判断是否在id中<span class="token2">,</span>是返回<span class="token3">1</span>否则<span class="token3">0</span> sdiff id id1 差集属于id<span class="token2">,</span>不属于id1<span class="token2">,</span> sdiff id1 id反之<span class="token2">{</span>B<span class="token">-</span>A<span class="token2">}</span>集合<span class="token2">;</span>支持多个差集 sdiff A B C sinter id id1 交集既属于id也属于id1<span class="token2">,</span>支持三个以上 sunion id id1 并集支持三个以上 scard id 元素数量 sdiffstore ids id id1 存储结果 sinterstore ids id id1 sunionstore ids id id1 srandmember id <span class="token3">3</span> 获取<span class="token3">3</span>个随机元素 负数会获得相同的<span class="token2">,</span>也可能不同的 spop id 随机删除一个<span class="token2">,</span>返回这个元素<span class="token2">,</span>返回nil就是没有元素 ``` ``` - zset 有序集合,支持正负数 0浮点数 ``` <pre class="calibre16">``` zadd id <span class="token3">10</span> tom <span class="token3">20</span> ma <span class="token3">90</span> liu 添加数据 zadd id <span class="token3">20</span> tom 修改数据 zscore id tom 获取元素分数 zrange id <span class="token3">0</span> <span class="token3">4</span> 排名从小到大<span class="token2">,</span>索引<span class="token3">0</span><span class="token">-</span><span class="token3">4</span>的元素 zrange id <span class="token3">0</span> <span class="token">-</span><span class="token3">1</span> 全部元素 <span class="token">-</span><span class="token3">1</span>表示最后一个元素 zrange id <span class="token3">0</span> <span class="token">-</span><span class="token3">1</span> withscores 同时获得元素分数 zrevrange id <span class="token3">0</span> <span class="token">-</span><span class="token3">1</span> 从大到小<span class="token2">,</span>类似上边 zrangebyscore id <span class="token3">1</span> <span class="token3">20</span> 返回<span class="token3">1</span>到<span class="token3">20</span>的key<span class="token2">,</span>从小到大<span class="token2">,</span>不包含用 <span class="token2">(</span><span class="token3">20</span> 表示 zrangebyscore id <span class="token3">1</span> <span class="token">+</span>inf <span class="token3">1</span>到正无群大<span class="token2">,</span><span class="token">-</span>inf 负的无群大 zrangebyscore id <span class="token3">1</span> <span class="token">+</span>inf withscores 返回带分数 zrangebyscore id <span class="token3">1</span> <span class="token">+</span>inf limit <span class="token3">0</span> <span class="token3">2</span> limit表示从<span class="token3">0</span>索引开始取两个 zrevrangebyscore id <span class="token3">100</span> <span class="token3">0</span> limit <span class="token3">0</span> <span class="token3">2</span> 相反<span class="token2">,</span>从后到前<span class="token2">,</span>从大到小 zincrby id <span class="token3">4</span> id id增加<span class="token3">4</span>分<span class="token2">,</span>如果不存在创建<span class="token2">,</span>存在增加<span class="token2">,</span>支持负数 zcard id 元素数量 zcount id <span class="token2">(</span><span class="token3">0</span> <span class="token3">90</span> 返回<span class="token3">0</span>到<span class="token3">90</span>元素数量<span class="token2">,</span>不含<span class="token3">0</span><span class="token2">,</span><span class="token">+</span>inf <span class="token">-</span>inf正负无穷大小 zrem id he tom 删除元素支持一个或多个<span class="token2">,</span>返回删除数量 zremRangeByRank id <span class="token3">0</span> <span class="token3">1</span> 分数按照从小到大排序<span class="token2">,</span>删除<span class="token3">0</span><span class="token2">,</span><span class="token3">1</span>元素 zremRangeByScore id <span class="token3">0</span> <span class="token2">(</span><span class="token3">30</span> 删除<span class="token3">0</span>到<span class="token3">30</span>范围内元素<span class="token2">,</span>返回数量 zrank id dudu 获得元素排名分数从小到大 zrevrank id dudu 分数从大到小 zInterStore ids <span class="token3">2</span> id id1 求id id1 交集<span class="token2">,</span>存储到ids键<span class="token2">,</span><span class="token3">2</span>表示<span class="token3">2</span>个的交集<span class="token2">,</span>返回交集个数 zInterStore ids <span class="token3">2</span> id id1 aggreGate sum<span class="token">|</span>min<span class="token">|</span>max 默认是sum表示交集分数之和<span class="token2">,</span>min最小<span class="token2">,</span>max最大 zUnionStore ids <span class="token3">2</span> id id1 求并集类似 ``` ```