redis:string

学习自:Redis 的五种数据类型及其底层原理_redis的五种数据类型_ZJE_ANDY的博客-CSDN博客

Redis 的五种基本数据类型_redis的五种数据类型_喵先森爱吃鱼的博客-CSDN博客

Redis是K-V结构的数据库。

Redis有5种常用数据类型(这里的数据类型,都是针对K-V对中的V的类型)

  • String
  • List
  • Set
  • Hash(就是Python中的dict,{name:"Jack",age:21})
  • Zset(有序集合)

1、前言

K都是string类型。

String是Redis最基本的数据类型,一个Key对应一个Value。Redis的String可以表示任何数据,例如jpg图像或者序列化的对象,String最大能存储512MB

根据字符串格式的不同,又可以分为3类:

  • string:普通字符串
  • int:整数类型,可以做自增自减操作
  • float:浮点类型,可以做自增自减操作

不管哪种格式,在底层都是以字节数组的形式存储,只是编码方式不同。

值类型

string hello world
int 10
float 92.5

字符串类型的内部编码(可用object encoding K查看)

内部编码是指这种类型是如何用C源码实现的。

string类型的内部编码有:

  • int:8B长整型
  • embstr:≤44B的字符串
  • raw:>44B的字符串

Redis会根据当前值的类型与长度决定使用哪种内部编码实现。

set key 8653
OK
object encoding key
"int"

set key "hello,world"
OK
object encoding key
"embstr"

set key "one string greater than 39 byte.............."
OK
object encoding key
"raw"
strlen key
(integer) 45

  

2、命令(不区分大小写)

命令

用法

说明

append append K V

不存在K时,先创建V

存在K时,在V后追加

strlen strlen K K对应的V的长度

set

setex

psetex

set K V

setex K t V

psetex K t V

添加/设置一个K-V对

设置一个K-V对,过期时间t秒

设置一个K-V对,过期时间t毫秒

get get K 获取K对应的V
getset getset K V 获取K的原V,并将其设置为新V
mset mset K1 V1 K2 V2 一次增加多个K-V
mget mget K1 K2 ... 一次获取多个K的V
incr incr K K对应的V(整型)自增1
incrby incrby K n K对应的V(整型)自增n
incrbyfloat incrbyfloat K n K对应的V(浮点)自增n
decr decr K 自减1
decrby decrby  K n 自减n
sertange setrange K start V 从start开始,用新V替换原V的子串
getrange getrange K start end 取K对应的V的子串(start与end可以是负数,-1表示最后一个字符,-2代表第二个……)

3、具体用法

1)set:设置K-V

用法:set K V [ex sec] [px millisec] [nx|xx]

说明:

  • ex:过期时间,单位s
  • px:过期时间,单位ms
  • nx:只有K不存在时才能set,用于添加时检测
  • xx:只有K存在时才能set,用于更新时检测

补充:

  • setex K sec V:使用了选项ex时的set
  • setnx K V:使用了选项nx时的set

例子:

192.168.10.20:6379> exists hello
(integer) 0
192.168.10.20:6379> set hello world
OK
//由于hello存在,所以setnx失败,添加
192.168.10.20:6379> setnx hello redis
(integer) 0
//由于hello存在,所以set xx成功,更新
192.168.10.20:6379> set hello jedis xx
OK  

作用

setnx可作为分布式锁的一种实现方案:http://redis.io/topics/distlock

2)get:获取V

用法:get K

说明:如果不存在,则返回nil(空)

3)mset:批量设置

用法:mset K V [K V...]

例子:

mset a 1 b 2 c 3 d 4
OK 

4)mget:批量获取

用法:mget K1 [K2 K3 ...]

说明:如果其中某个K不存在,则返回时它的对应项为nil

例子:

mget a b c d f
1) "1"
2) "2"
3) "3"
4) "4"
5) (nil)

5)自增自减

自增:

  • incr K:K自增1
  • incrby K n:自增n
  • incrbyfloat K f:浮点自增f

自减:

  • decr K:K自减1
  • decrby K n:K自减n

返回值:

如果V不是符合类型的,会返回error

如果符合类型,会返回自增/自减后的值。

说明:

如果K不存在,则将V置1(自增)或0(自减)

 

6)append:往字符串尾追加内容

用法:append K V

返回值:数字,代表变化之后的字符串长

说明:如果K不存在,append相当于新建

例子:

get key
"redis"

append key world
(integer) 10

get key
"redisworld"

7)strlen:计算长度

用法:strlen K

返回值:数字,字符串长

例子:

get key
"redisworld"

strlen key
(integer) 10

8)getset:设置新值,返回原值

用法:getset K V

返回值:K原来的V,如果不存在K-V则返回nil

例子:

getset hello world
(nil)

getset hello redis
"world"  

9)setrange:替换

用法:setrange K offset V

说明:

从索引offset开始,把原V后半部分替换为新V

如果K的原V长度小于偏移量,则不足的地方用0补齐

返回值:修改后的串长

例子:

set redis pest
OK

//0号索引处替换为b
setrange redis 0 b
(integer) 4

get redis
"best"  
get K1 
hello

//中间填充0
setrange K1 10 --java
(integer) 16
get K1
"hello\x00\x00\x00\x00\x00--java"  

10)getrange:获取子串

用法:getrange K start end

说明:

取K对应的V的子串

选项:

start与end如果是正数,则以0为第一个字符

如果是负数,-1表示最后一个字符,-2代表第二个……

例子:

get K1 
helloworld


getrange K1 0 2
"hel"
getrange K1 -3 -1
"rld"

 

posted @ 2023-08-25 13:48  ShineLe  阅读(11)  评论(0编辑  收藏  举报