Redis的五大数据类型

Redis的五大数据类型

核心:redis是将所有的数据全部放在内存中的,所以说使用单线程区操作效率是最高的,多线程(CPU上下文会切换:耗时),对于内存系统来说,如果没有上下文切换效率就是最高的。多次读写都是在一个CPU上的,在内存情况下,就是最佳方案。
redis中文网址:http://www.redis.cn/

Redis-Key

127.0.0.1:6379> set name maple # set key
OK
127.0.0.1:6379> keys * # 查看所有key
1) "name"
127.0.0.1:6379> set age 18
OK
127.0.0.1:6379> keys *
1) "age"
2) "name"
127.0.0.1:6379> EXISTS name # 判断当前的key是否存在
(integer) 1
127.0.0.1:6379> EXISTS name1
(integer) 0
127.0.0.1:6379> move name 1 # 移除当前的key
(integer) 1
127.0.0.1:6379> keys *
1) "age"
127.0.0.1:6379> set name maple
OK
127.0.0.1:6379> get name 
"maple"
127.0.0.1:6379> EXPIRE name 10 # 设置过期时间,单位是秒
(integer) 1
127.0.0.1:6379> ttl name # 查看设置的过期时间还剩多久
(integer) 2
127.0.0.1:6379> ttl name
(integer) -2
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> TYPE age # 查看当前key的类型
string

String

127.0.0.1:6379> set key1 v1 # 设置值
OK
127.0.0.1:6379> get key1
"v1"
127.0.0.1:6379> EXISTS key1 
(integer) 1
127.0.0.1:6379> append key1 "hello" # 追加字符串,如果当前key不存在,就相当于set key
(integer) 7
127.0.0.1:6379> get key1
"v1hello"
127.0.0.1:6379> append key1 "maplehello"
(integer) 17
127.0.0.1:6379> STRLEN key1 # 获取字符串的长度
(integer) 17
127.0.0.1:6379> get key1
"v1hellomaplehello"

自增和自减

127.0.0.1:6379> set views 0
OK
127.0.0.1:6379> get views
"0"
127.0.0.1:6379> incr views # 自增 1
(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 # 自减 1
(integer) 1
127.0.0.1:6379> decr views
(integer) 0
127.0.0.1:6379> INCRBY views 10 # 设置步长,指定增加量
(integer) 10
127.0.0.1:6379> decrby views 10 # 设置步长,指定减少量
(integer) 0

字符串截取

127.0.0.1:6379> set key1 "abcdefghijkmnopq"
OK
127.0.0.1:6379> get key1
"abcdefghijkmnopq"
127.0.0.1:6379> GETRANGE key1 0 3 # 截取字符串[0,3]
"abcd"
127.0.0.1:6379> GETRANGE key1 0 -1 # 获取全部字符串[0,-1]
"abcdefghijkmnopq"
127.0.0.1:6379> 

字符串替换

127.0.0.1:6379> set key2 abcdefg
OK
127.0.0.1:6379> get key2
"abcdefg"
127.0.0.1:6379> SETRANGE key2 1 xx # 替换指定位置开始的字符串
(integer) 7
127.0.0.1:6379> get key2
"axxdefg"

setex和setnx

setex(set with expire) 设置过期时间
setnx(set if not exist) 不存在再设置(在分布式锁中常使用)

127.0.0.1:6379> setex key3 30 "hello"
OK
127.0.0.1:6379> ttl key3
(integer) 26
127.0.0.1:6379> get key3
"hello"
127.0.0.1:6379> setnx mykey "redis" # 如果mykey不存在,创建mykey
(integer) 1
127.0.0.1:6379> keys * 
1) "key1"
2) "key2"
3) "mykey"
127.0.0.1:6379> ttl key3
(integer) -2
127.0.0.1:6379> setnx mykey "MongoDB" # 如果mykey存在,创建失败
(integer) 0
127.0.0.1:6379> get mykey
"redis"

同时设置多个和获取多个

127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 # 同时设置多个值
OK
127.0.0.1:6379> keys *
1) "k2"
2) "k1"
3) "k3"
127.0.0.1:6379> mget k1 k2 k3 # 同时获取多个值
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> msetnx k1 v1 k4 v4 # msetnx是一个原子操作,要么一起成功,要么一起失败
(integer) 0
127.0.0.1:6379> get k4
(nil)

对象

user:{id}:{filed}

127.0.0.1:6379> mset user:1:name maple user:1:age 18
OK
127.0.0.1:6379> mget user:1:name user:1:age
1) "maple"
2) "18"

getset

先get然后再set,如果不存在值,则返回nil,如果存在,则获取原来的值并设置新的值。

127.0.0.1:6379> getset db redis
(nil)
127.0.0.1:6379> get db
"redis"
127.0.0.1:6379> getset db mongodb
"redis"
127.0.0.1:6379> get db
"mongodb"

List

基本的数据类型,列表

lpush和rpush

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> lrange list 0 -1 # 获取list中的值
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> lrange list 0 1 # 通过区间获取具体的值
1) "three"
2) "two"
3) 127.0.0.1:6379> rpush list four # 将一个值或者多个值,插入列表的尾部
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "four"

lpop和rpop

127.0.0.1:6379> lpop list # 移除列表的第一个元素
"three"
127.0.0.1:6379> rpop list # 移除列表的最后一个元素
"four"
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"

lindex

127.0.0.1:6379> LINDEX list 1 # 通过下标获得list中的某一个值
"one"
127.0.0.1:6379> LINDEX list 0
"two"

Llen

127.0.0.1:6379> llen list # 查看list的长度
(integer) 2

lrem

127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
127.0.0.1:6379> lrem list 1 one # 移除list集合中指定个数的value,精确匹配
(integer) 1

trim

截断

127.0.0.1:6379> rpush mylist "hello"
(integer) 1
127.0.0.1:6379> rpush mylist "hello1"
(integer) 2
127.0.0.1:6379> rpush mylist "hello2"
(integer) 3
127.0.0.1:6379> rpush mylist "hello3"
(integer) 4
127.0.0.1:6379> ltrim mylist 1 2 # 通过下标截取指定的长度,只剩下截取的元素
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello1"
2) "hello2"

rpoplpush

移除列表的最后一个元素,并将它移动到新的列表中

127.0.0.1:6379> rpush mylist "hello"
(integer) 1
127.0.0.1:6379> rpush mylist "hello1"
(integer) 2
127.0.0.1:6379> rpush mylist "hello2"
(integer) 3
127.0.0.1:6379> rpoplpush mylist myotherlist
"hello2"
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "hello1"
127.0.0.1:6379> LRANGE myotherlist 0 -1
1) "hello2"

lset

将列表中指定下标的值替换为另外一个值,更新操作。

127.0.0.1:6379> lpush list v1
(integer) 1
127.0.0.1:6379> LRANGE list 0 0
1) "v1"
127.0.0.1:6379> lset list 0 item
OK
127.0.0.1:6379> LRANGE list 0 0
1) "item"
127.0.0.1:6379> lset list 1 other
(error) ERR index out of range

insert

将某一个具体的value插入到列表中某个元素的前面或者后面

127.0.0.1:6379> rpush mylist hello
(integer) 1
127.0.0.1:6379> rpush mylist world
(integer) 2
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "world"
127.0.0.1:6379> linsert mylist before world other
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "other"
3) "world"
127.0.0.1:6379> linsert mylist after world new 
(integer) 4
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "other"
3) "world"
4) "new"

Set

添加元素、查看元素和判断元素是否在集合中

127.0.0.1:6379> sadd myset hello # set集合中添加元素
(integer) 1
127.0.0.1:6379> sadd myset hello1
(integer) 1
127.0.0.1:6379> sadd myset hello2
(integer) 1
127.0.0.1:6379> SMEMBERS myset # 查看set集合中的所有元素
1) "hello2"
2) "hello1"
3) "hello"
127.0.0.1:6379> SISMEMBER myset hello # 判断某个元素是不是在set集合中
(integer) 1
127.0.0.1:6379> SISMEMBER myset world
(integer) 0

获取set集合中的元素个数

127.0.0.1:6379> scard myset
(integer) 3

移除元素

127.0.0.1:6379> srem myset hello
(integer) 1
127.0.0.1:6379> scard myset
(integer) 2
127.0.0.1:6379> SMEMBERS myset
1) "hello2"
2) "hello1"

随机获取元素

可以随机抽取多个元素

127.0.0.1:6379> SRANDMEMBER myset 
"hello2"
127.0.0.1:6379> SRANDMEMBER myset 
"hello1"

移除指定的key,随机删除key

127.0.0.1:6379> spop myset
"hello1"
127.0.0.1:6379> spop myset
"hello2"

将一个set集合中指定的值,移动到另外一个set集合

127.0.0.1:6379> sadd myset hello
(integer) 1
127.0.0.1:6379> sadd myset helloworld
(integer) 1
127.0.0.1:6379> sadd myset helloworld1
(integer) 1
127.0.0.1:6379> sadd myset2 set2
(integer) 1
127.0.0.1:6379> smove myset myset2 hello
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "helloworld1"
2) "helloworld"
127.0.0.1:6379> SMEMBERS myset2
1) "set2"
2) "hello"

差集、交集、并集

127.0.0.1:6379> sadd k1 a
(integer) 1
127.0.0.1:6379> sadd k1 b
(integer) 1
127.0.0.1:6379> sadd k1 c
(integer) 1
127.0.0.1:6379> sadd k1 d
(integer) 1
127.0.0.1:6379> sadd k2 c 
(integer) 1
127.0.0.1:6379> sadd k2 d
(integer) 1
127.0.0.1:6379> sadd k2 e
(integer) 1
127.0.0.1:6379> SDIFF k1 k2 # 差集
1) "b"
2) "a"
127.0.0.1:6379> SINTER k1 k2 # 交集
1) "d"
2) "c"
127.0.0.1:6379> SUNION k1 k2 # 并集
1) "a"
2) "d"
3) "c"
4) "b"
5) "e"

Hash

类比Map集合,key-map集合

127.0.0.1:6379> hset myhash field1 maple # set一个具体的key-value
(integer) 1
127.0.0.1:6379> hget myhash field1 # 获取一个字段值
"maple"
127.0.0.1:6379> hmset myhash field1 hello field2 world # set多个key-value
OK
127.0.0.1:6379> hmget myhash field1 field2 # 获取多个字段值
1) "hello"
2) "world"
127.0.0.1:6379> hgetall myhash # 获取全部的数据
1) "field1"
2) "hello"
3) "field2"
4) "world"

hdel:删除指定的key

127.0.0.1:6379> hdel myhash field1
(integer) 1
127.0.0.1:6379> hgetall myhash
1) "field2"
2) "world"

hlen:获取长度

127.0.0.1:6379> hlen myhash
(integer) 1

hexists:判断hash中指定字段是否存在

127.0.0.1:6379> HEXISTS myhash field2
(integer) 1

只获取key

127.0.0.1:6379> hkeys myhash
1) "field2"

只获得value

127.0.0.1:6379> hvals myhash
1) "world"

指定自增和自减

127.0.0.1:6379> hset myhash field3 5
(integer) 1
127.0.0.1:6379> HINCRBY myhash field3 1
(integer) 6
127.0.0.1:6379> HINCRBY myhash field3 -1
(integer) 5

hsetnx

127.0.0.1:6379> hsetnx myhash field4 hello # 不存在可以设置
(integer) 1
127.0.0.1:6379> hsetnx myhash field4 world # 存在不可以设置
(integer) 0

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> zadd salary 2500 maple
(integer) 1
127.0.0.1:6379> zadd salary 5000 pjs
(integer) 1
127.0.0.1:6379> zadd salary 500 ryan
(integer) 1
127.0.0.1:6379> ZREVRANGE salary 0 -1 # 从大到小排序
1) "maple"
2) "ryan"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf 
1) "ryan"
2) "maple"
3) "pjs"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf withscores
1) "ryan"
2) "500"
3) "maple"
4) "2500"
5) "pjs"
6) "5000"

移除指定元素

127.0.0.1:6379> zrem salary pjs
(integer) 1
127.0.0.1:6379> ZRANGE salary 0 -1
1) "ryan"
2) "maple"
127.0.0.1:6379> zcard salary 
(integer) 2

获取指定区间的成员数量

127.0.0.1:6379> zadd myset 1 hello
(integer) 1
127.0.0.1:6379> zadd myset 2 world 3 maple
(integer) 2
127.0.0.1:6379> zcount myset 1 3
(integer) 3
127.0.0.1:6379> zcount myset 1 2
(integer) 2

三种特殊数据类型

geospatial地理位置

相关命令

GEOADD:添加地理位置
GEODIST:两个人之间的距离
GEOHASH:
GEOPOS:获取当前定位
GEORADIUS:以给定的经纬度为中心,找出某一个半径的元素
GEORADIUSBYMEMBER:找出位于指定元素周围的元素

hyperloglog基数统计

127.0.0.1:6379> pfadd mykey a b c d e f g h i j # 创建一组元素
(integer) 1
127.0.0.1:6379> pfcount mykey # 统计mykey元素基数数量
(integer) 10
127.0.0.1:6379> pfadd mykey2 i j z x c v b n m
(integer) 1
127.0.0.1:6379> pfcount mykey2
(integer) 9
127.0.0.1:6379> PFMERGE mykey3  mykey mykey2 # 合并两组
OK
127.0.0.1:6379> pfcount mykey3
(integer) 15

bitmaps位图场景

位存储
setbit、getbit:打卡~~0,1

127.0.0.1:6379> setbit sign 0 1
(integer) 0
127.0.0.1:6379> setbit sign 1 0
127.0.0.1:6379> 、getbit sign 2
(integer) 1
127.0.0.1:6379> bitcount sign
(integer) 2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值