数据类型(五大基本类型,3种特殊类型)

概述

官方文档

Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。

redis-key

move key dbindex  //移除当前数据库中的key
expire key time(s) //设置key的过期时间,单位s
keys * //查询当前数据库中的key
exists key //判断key是否存在
ttl key //查看当前key的剩余时间,还有多长时间过期
type key //查看当前key的类型

String (字符串类型)

  1. get key
  2. set key
  3. exists key
  4. append key value 追加字符串
    在这里插入图片描述
  5. strlen key查看字符串长度
    strlen name
  6. 增加减少
127.0.0.1:6379> set views 0
OK
127.0.0.1:6379> get views
"0"
127.0.0.1:6379> incr views
(integer) 1
127.0.0.1:6379> incr views
(integer) 2
127.0.0.1:6379> get views
"2"
127.0.0.1:6379> decr views
(integer) 1
127.0.0.1:6379> incrby views 10
(integer) 11
127.0.0.1:6379> get views
"11"
127.0.0.1:6379> decrby views 8
(integer) 3
127.0.0.1:6379> get views
  1. 字符串操作
127.0.0.1:6379> getrange name 1 2  # 获取部分字符串
"23"
127.0.0.1:6379> getrange name 1 5
"23ttt"
127.0.0.1:6379> getrange name 1 -1
"23tttfdafda"
127.0.0.1:6379> setrange name 2 nihao # 替换指定位置的字符串
(integer) 12
127.0.0.1:6379> get name
"12nihaodafda"
127.0.0.1:6379>
#####################################################################
# setex(set with expire) 设置过期时间
# setnx(set if not exist) 当键不存在,设置键值,存在设置失败;通常用在分布式数据库中用来作为分布式锁

127.0.0.1:6379> setex name "hello" 10
(error) ERR value is not an integer or out of range
127.0.0.1:6379> setex name 30 "hello"
OK
127.0.0.1:6379> ttl name
(integer) 27
127.0.0.1:6379> setnx name
(error) ERR wrong number of arguments for 'setnx' command
127.0.0.1:6379> setnx name "world"
(integer) 0
127.0.0.1:6379> get name
"hello"
127.0.0.1:6379> setnx name1 "world"
(integer) 1
127.0.0.1:6379> get name1
"world"
127.0.0.1:6379> ttl name
(integer) -2
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379>
####################################################################################################
# mset 同时设置key
127.0.0.1:6379> mset key1 v1 key2 v2 key3 v3
OK
127.0.0.1:6379> keys *
1) "age"
2) "key2"
3) "views"
4) "key3"
5) "hh"
6) "name1"
7) "key1"
# mget同时获取Key
127.0.0.1:6379> mget key1 key2 key3
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379>
# msetnx不存在设置,是一个原子性操作,要么一起成功,要么一起失败
127.0.0.1:6379> msetnx key1 v1 key4 v4
(integer) 0
127.0.0.1:6379> msetnx key5 v1 key4 v4
(integer) 1
# 先获取Key值,在设置Key
getset key value

使用场景

value除了可以是字符串还可以是数字。

  • 计算器(incr,decr)
  • 统计多单位的数量
  • 粉丝数
  • 对象缓存

List

基本数据类型,列表
在Redis里面可以把list作为栈、队列、阻塞队列
以l开头的都是List的操作命令

127.0.0.1:6379> flushall
OK
# lpush 头插法
127.0.0.1:6379> lpush list one
(integer) 1
127.0.0.1:6379> lpush list two
(integer) 2
127.0.0.1:6379> lpush list three
(integer) 3
127.0.0.1:6379> lrang list 0 -1
(error) ERR unknown command `lrang`, with args beginning with: `list`, `0`, `-1`,
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> lrang list 0 1
(error) ERR unknown command `lrang`, with args beginning with: `list`, `0`, `1`,
127.0.0.1:6379> lrange list 0 1
1) "three"
2) "two"
127.0.0.1:6379> lrange list 0 0
1) "three"
# 尾插法,插入列表的最后
127.0.0.1:6379>rpush list four
# 移除使用lpop移除列表第一个元素,rpop移除列表的最后一个元素
127.0.0.1:6379> lpop list
"three"
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
3) "four"
127.0.0.1:6379> rpop list
"four"
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
127.0.0.1:6379>
# 获取指定下标的元素
127.0.0.1:6379> lindex list 1
"one"
127.0.0.1:6379> lindex list 0
"two"
127.0.0.1:6379> lrem list 1 one
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "two"
127.0.0.1:6379> lpush list two
(integer) 2
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "two"
# 删除指定值的元素
127.0.0.1:6379> lrem list 2 two
(integer) 2
127.0.0.1:6379> lrange list 0 -1
# 截取列表 ltrim 根据下标截取指定范围的列表
127.0.0.1:6379> rpush mylist "v1"
(integer) 1
127.0.0.1:6379> rpush mylist "v2"
(integer) 2
127.0.0.1:6379> rpush mylist "v3"
(integer) 3
127.0.0.1:6379> rpush mylist "v4"
(integer) 4
127.0.0.1:6379> lrang mylist
(error) ERR unknown command `lrang`, with args beginning with: `mylist`,
127.0.0.1:6379> lrange mylist 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
127.0.0.1:6379> ltrim mylist 1 2
OK
127.0.0.1:6379> lrange mylist 0 -1
1) "v2"
2) "v3"

小结

  • List实际上是一个链表,before node,after node,left,right都可以插入值
  • 如果key不存在,创建新的链表
  • 如果key存在,新增内容
  • 如果移除了所有值,空链表,也代表列表不存在
  • 在两边插入或者改动值,效率最高,中间元素,相对来说效率较低。
  • 可以用在消息队列、栈。

set(集合,值不能重复,无序)

命令以s开头

# 集合添加元素
127.0.0.1:6379> sadd myset "hello"
(integer) 1
127.0.0.1:6379> sadd myset "world"
(integer) 1
# 集合查看元素
127.0.0.1:6379> smembers myset
1) "world"
2) "hello"
# 判断元素是否在集合中
127.0.0.1:6379> sismember myset hello
(integer) 1
127.0.0.1:6379> sismember myset hello1
(integer) 0
# 查看集合大小
127.0.0.1:6379> scard myset
(integer) 2
127.0.0.1:6379> smembers myset
1) "world"
2) "hello"
# 移除元素
srem myset hello
127.0.0.1:6379> smembers myset
1) "world"
# 获取随机元素
127.0.0.1:6379> srandmember myset
"world"
127.0.0.1:6379> srandmember myset
"world"
127.0.0.1:6379> srandmember myset
"world"
127.0.0.1:6379> sadd myset hello
(integer) 1
127.0.0.1:6379> srandmember myset
"hello"
127.0.0.1:6379> srandmember myset
"hello"
127.0.0.1:6379> srandmember myset
"world"
127.0.0.1:6379>
##############################################
# 随机删除
127.0.0.1:6379> spop myset
"world"
127.0.0.1:6379> smembers myset
1) "hello"
##################################################
# 集合操作
# 差集:sdiff key1 key2
# 交集:sinter key1 key2
# 并集: sunion key1 key2
127.0.0.1:6379> sadd myset2 hello
(integer) 1
127.0.0.1:6379> sadd myset2 world
(integer) 1
127.0.0.1:6379> smembers myset
1) "hello"
127.0.0.1:6379> smembers myset2
1) "world"
2) "hello"
127.0.0.1:6379> sdiff myset myset2
(empty array)
127.0.0.1:6379> sdiff myset2 myset1
1) "world"
2) "hello"
127.0.0.1:6379> sdiff myset2 myset2
(empty array)
127.0.0.1:6379> sdiff myset2 myset
1) "world"
127.0.0.1:6379> sinter myset myset2
1) "hello"
127.0.0.1:6379> sunion myset myset2
1) "world"
2) "hello"

Hash

Map集合,key-map是一个map集合,本质上和String类型没有太大的区别,还是异地的key-value。
以h开头的命令

127.0.0.1:6379> hset myhash f1 v1
(integer) 1
127.0.0.1:6379> hget myhash f1
"v1"
127.0.0.1:6379> hmset myhash f1 h1 f2 h2 f3 h3
OK
127.0.0.1:6379> hmget myhash f1 f2 f3
1) "h1"
2) "h2"
3) "h3"
127.0.0.1:6379>
# hgetall获取map集合中的所有值
127.0.0.1:6379> hgetall myhash
1) "f1"
2) "h1"
3) "f2"
4) "h2"
5) "f3"
6) "h3"
127.0.0.1:6379>

使用场景

hash变量适合存储对象,string更加适合存储字符串。

zset(有序集合)

在set的基础上,增加了一个值

127.0.0.1:6379> zadd myset 1 one
(integer) 1
127.0.0.1:6379> zadd myset 2 two 3 three
(integer) 2
127.0.0.1:6379> zrange myset 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379>

# 删除zset中的元素
127.0.0.1:6379> zrem myset one
0
############################################################
# 按照值排序
127.0.0.1:6379> zadd salary 2500 xiaohong
(integer) 1
127.0.0.1:6379> zadd salary 5000 zhangsan
(integer) 1
127.0.0.1:6379> zadd salary 1000 xiaoming
(integer) 1
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf  # 按照最小到最大排序
1) "xiaoming"
2) "xiaohong"
3) "zhangsan"
127.0.0.1:6379> ZRANGEBYSCORE salary +inf -inf
(empty array)
127.0.0.1:6379> ZRANGEBYSCORE salary 0 -1
(empty array)
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf
1) "xiaoming"
2) "xiaohong"
3) "zhangsan"
127.0.0.1:6379> ZRANGEBYSCORE salary 0 -1
(empty array)
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf withscores
1) "xiaoming"
2) "1000"
3) "xiaohong"
4) "2500"
5) "zhangsan"
6) "5000"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf 1000 withscores
1) "xiaoming"
2) "1000"
127.0.0.1:6379>
# 指定最小最大值排序
127.0.0.1:6379> ZRANGEBYSCORE salary 1000 5000
1) "xiaoming"
2) "xiaohong"
3) "zhangsan"
127.0.0.1:6379> ZRANGEBYSCORE salary 1000 5000 withscores
1) "xiaoming"
2) "1000"
3) "xiaohong"
4) "2500"
5) "zhangsan"
6) "5000"
127.0.0.1:6379>

场景

set 排序,存储班级成绩、工资表排序。
带权重进行判断。
排行榜应用实现。

查看命令如何使用、数据类型相关的命令有哪些

  1. ? 查询help有哪些命令
  2. help command 查看命令如何使用
  3. help @group 查询数据类型相关的操作命令
27.0.0.1:6379> ?
redis-cli 6.0.9
To get help about Redis commands type:
      "help @<group>" to get a list of commands in <group>
      "help <command>" for help on <command>
      "help <tab>" to get a list of possible help topics
      "quit" to exit

To set redis-cli preferences:
      ":set hints" enable online hints
      ":set nohints" disable online hints
Set your preferences in ~/.redisclirc
127.0.0.1:6379> help set

  SET key value [EX seconds|PX milliseconds|KEEPTTL] [NX|XX]
  summary: Set the string value of a key
  since: 1.0.0
  group: string

127.0.0.1:6379> help @string

  APPEND key value
  summary: Append a value to a key
  since: 2.0.0

  BITCOUNT key [start end]
  summary: Count set bits in a string
  since: 2.6.0

  BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]
  summary: Perform arbitrary bitfield integer operations on strings
  since: 3.2.0

  BITOP operation destkey key [key ...]
  summary: Perform bitwise operations between strings
  since: 2.6.0

  BITPOS key bit [start] [end]
  summary: Find first bit set or clear in a string
  since: 2.8.7

  DECR key
  summary: Decrement the integer value of a key by one
  since: 1.0.0

  DECRBY key decrement
  summary: Decrement the integer value of a key by the given number
  since: 1.0.0

  GET key
  summary: Get the value of a key
  since: 1.0.0

  GETBIT key offset
  summary: Returns the bit value at offset in the string value stored at key
  since: 2.2.0

  GETRANGE key start end
  summary: Get a substring of the string stored at a key
  since: 2.4.0

  GETSET key value
  summary: Set the string value of a key and return its old value
  since: 1.0.0

  INCR key
  summary: Increment the integer value of a key by one
  since: 1.0.0

  INCRBY key increment
  summary: Increment the integer value of a key by the given amount
  since: 1.0.0

  INCRBYFLOAT key increment
  summary: Increment the float value of a key by the given amount
  since: 2.6.0

  MGET key [key ...]
  summary: Get the values of all the given keys
  since: 1.0.0

  MSET key value [key value ...]
  summary: Set multiple keys to multiple values
  since: 1.0.1

  MSETNX key value [key value ...]
  summary: Set multiple keys to multiple values, only if none of the keys exist
  since: 1.0.1

  PSETEX key milliseconds value
  summary: Set the value and expiration in milliseconds of a key
  since: 2.6.0

  SET key value [EX seconds|PX milliseconds|KEEPTTL] [NX|XX]
  summary: Set the string value of a key
  since: 1.0.0

  SETBIT key offset value
  summary: Sets or clears the bit at offset in the string value stored at key
  since: 2.2.0

  SETEX key seconds value
  summary: Set the value and expiration of a key
  since: 2.0.0

  SETNX key value
  summary: Set the value of a key, only if the key does not exist
  since: 1.0.0

  SETRANGE key offset value
  summary: Overwrite part of a string at key starting at the specified offset
  since: 2.2.0

  STRALGO LCS algo-specific-argument [algo-specific-argument ...]
  summary: Run algorithms (currently LCS) against strings
  since: 6.0.0

  STRLEN key
  summary: Get the length of the value stored in a key
  since: 2.2.0

127.0.0.1:6379> help @hash

  HDEL key field [field ...]
  summary: Delete one or more hash fields
  since: 2.0.0

  HEXISTS key field
  summary: Determine if a hash field exists
  since: 2.0.0

  HGET key field
  summary: Get the value of a hash field
  since: 2.0.0

  HGETALL key
  summary: Get all the fields and values in a hash
  since: 2.0.0

  HINCRBY key field increment
  summary: Increment the integer value of a hash field by the given number
  since: 2.0.0

  HINCRBYFLOAT key field increment
  summary: Increment the float value of a hash field by the given amount
  since: 2.6.0

  HKEYS key
  summary: Get all the fields in a hash
  since: 2.0.0

  HLEN key
  summary: Get the number of fields in a hash
  since: 2.0.0

  HMGET key field [field ...]
  summary: Get the values of all the given hash fields
  since: 2.0.0

  HMSET key field value [field value ...]
  summary: Set multiple hash fields to multiple values
  since: 2.0.0

  HSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Incrementally iterate hash fields and associated values
  since: 2.8.0

  HSET key field value [field value ...]
  summary: Set the string value of a hash field
  since: 2.0.0

  HSETNX key field value
  summary: Set the value of a hash field, only if the field does not exist
  since: 2.0.0

  HSTRLEN key field
  summary: Get the length of the value of a hash field
  since: 3.2.0

  HVALS key
  summary: Get all the values in a hash
  since: 2.0.0

三种特殊数据类型

geospatial地理位置

朋友的定位,附件的人,打车距离计算等

hyperloglog

基数统计

bitmaps

按位存储
统计用户信息。
Bitmaps位图,数据结构!都是操作二进制位来进行记录,就只有0和1两个状态。
365天=365 bit
签到打卡场景。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Redis是一高性能的开源键值型数据库,支持多数据类型,在实际开发中使用非常广泛。Redis支持五主要的数据类型:字符串、哈希、列表、集合、有序集合。 1. 字符串数据类型 字符串是Redis基本数据类型Redis中的字符串是二进制安全的,这意味着Redis支持任意类型数据,如数字、图片、音频等。字符串数据类型是一个独立的子系统,具有一些特殊的命令,支持字符串连接操作,可用于缓存、计数器等场景。 2. 哈希数据类型 哈希数据类型将多个键值对存储在一个Redis键中,例如一个用户的所有相关信息可以存储在一个哈希数据类型中。哈希数据类型可以减少Redis内存使用量,使数据更简洁高效。可以使用多个命令操作哈希数据类型,如HSET、HGET、HDEL等。 3. 列表数据类型 Redis列表是一组有序的字符串元素,可以执行各列表操作,如从两端插入或删除元素,根据索引值等进行访问。列表数据类型通常用于实现队列、栈等数据结构,或用于实现消息传递系统。 4. 集合数据类型 Redis集合是一组无序的字符串元素,可以进行各集合操作,如添加、删除元素,计算交集、并集等。集合数据类型通常用于实现一些针对元素的计算操作,如共同关注列表、兴趣相同的用户列表等。 5. 有序集合数据类型 有序集合是Redis的一特殊类型,与集合类型类似,同时每个元素有一个分数,这个分数可以用来对有序集合中的元素进行排序。有序集合数据类型通常用于实现高级的计算问题,如排名、音乐排行榜等场景。 总之,Redis的五数据类型各具特色,可以在不同场景下灵活运用,大大提高了Redis的适用范围和性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

融极

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值