redis学习笔记2
作者:互联网
一、redis的五大基本数据类型
1.1 redis-key
exists命令
127.0.0.1:6379> keys *
1) "name"
2) "age"
127.0.0.1:6379> exists name #查看key值为name的数据是否存在
(integer) 1 #若存在则返回1
127.0.0.1:6379> exists name1
(integer) 0 #若不存在则返回0
move命令
127.0.0.1:6379> move name 1 #移除一个key值,后面的数字1,代表当前数据库
(integer) 1 #返回值为1,则代表移除成功
127.0.0.1:6379> keys *
1) "age"
127.0.0.1:6379>
expire命令,设置过期时间,单位是秒
127.0.0.1:6379> keys *
1) "name"
2) "age"
127.0.0.1:6379> expire name 10 #给key值name,设置过期时间
(integer) 1
127.0.0.1:6379> ttl name #查看剩余的过期时间
(integer) 5
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命令,查看数据的类型
127.0.0.1:6379> keys *
1) "age"
127.0.0.1:6379> type age
string
127.0.0.1:6379>
1.2 string数据类型
append命令,在值后面追加新的字符串
127.0.0.1:6379> exists key1
(integer) 1
127.0.0.1:6379> get key1
"v1"
127.0.0.1:6379> append key1 hello
(integer) 7
127.0.0.1:6379> get key1
"v1hello"
127.0.0.1:6379>
strlen命令,获取字符串的长度
127.0.0.1:6379> strlen key1
(integer) 7
127.0.0.1:6379> append key1 ",kuangshen"
(integer) 17
127.0.0.1:6379> strlen key1
(integer) 17
append key,若该Key不存在,就相当于set key
127.0.0.1:6379> keys *
1) "key1"
127.0.0.1:6379> append name zhangsan
(integer) 8
127.0.0.1:6379> keys *
1) "name"
2) "key1"
incr命令,参数加1命令
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> get views
"1"
decr命令,参数减1命令
127.0.0.1:6379> get views
"0"
127.0.0.1:6379> decr views
(integer) -1
127.0.0.1:6379> decr views
(integer) -2
127.0.0.1:6379> get views
"-2"
incrby命令,按设置的步长增加数
127.0.0.1:6379> get views
"-2"
127.0.0.1:6379> incrby views 10
(integer) 8
127.0.0.1:6379> get views
"8"
decrby命令,按设置的步长减数
127.0.0.1:6379> get views
"8"
127.0.0.1:6379> decrby views 5
(integer) 3
127.0.0.1:6379> get views
"3"
getrange命令,获取指定的字符串
127.0.0.1:6379> set key1 "hello,kuangshen"
OK
127.0.0.1:6379> getrange key1 0 3
"hell"
127.0.0.1:6379> keys *
1) "key1"
127.0.0.1:6379> get key1
"hello,kuangshen"
127.0.0.1:6379> GETRANGE key1 0 -1 #0至-1,用于显示整个字符串
"hello,kuangshen"
setrange命令,修改指定的字符串
127.0.0.1:6379> set key2 abcdefgh
OK
127.0.0.1:6379> get key2
"abcdefgh"
127.0.0.1:6379> setrange key2 1 xxx
(integer) 8
127.0.0.1:6379> get key2
"axxxefgh"
setex命令(set with expire),设置值的同时设置过期时间
127.0.0.1:6379> setex key3 60 english
OK
127.0.0.1:6379> ttl key3
(integer) 54
127.0.0.1:6379> get key3
"english"
127.0.0.1:6379> ttl key3
(integer) -2
127.0.0.1:6379> get key3
(nil)
setnx命令(set if not exist),如果不存在该key值,则设置。如果存在该key值,再次设置,不生效。
127.0.0.1:6379> setnx mykey redis
(integer) 1
127.0.0.1:6379> keys *
1) "key1"
2) "key2"
3) "mykey"
127.0.0.1:6379> setnx mykey mongodb #mykey已经存在了,则设置失败
(integer) 0
127.0.0.1:6379> get mykey
"redis"
mset命令,批量设置值
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3
OK
127.0.0.1:6379> keys *
1) "k3"
2) "k2"
3) "k1"
mget命令,批量获取值
127.0.0.1:6379> mget k1 k2 k3
1) "v1"
2) "v2"
3) "v3"
msetnx命令,批量设置值,如果不存在,则创建。存在的key值,再创建,会失败
127.0.0.1:6379> keys *
1) "k3"
2) "k2"
3) "k1"
127.0.0.1:6379> msetnx k1 v1 k4 v4 #是一个原子性的操作,要么一起成功,要么一起失败
(integer) 0
127.0.0.1:6379> get key4
(nil)
设置对象,存储一个用户的年龄,名字信息
127.0.0.1:6379> mset user:1:name zhangsan user:1:age 4
OK
127.0.0.1:6379> mget user:1:name user:1:age
1) "zhangsan"
2) "4"
getset命令,先get值,再set值
127.0.0.1:6379> getset dg redis #get dg的值(因为首次dg的值不存在),所以不成功。但是set的值,是成功的
(nil)
127.0.0.1:6379> get dg
"redis"
127.0.0.1:6379> getset dg mongodb #先get dg的值,成功。再重新set dg的值,也成功
"redis"
127.0.0.1:6379> get dg
"mongodb"
1.3 list数据类型
list数据类型中,所有的命令都是以l开头的。
往list数据类型中添加值,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
查看list数据类型中的值,lrange命令
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> lrange list 0 1
1) "three"
2) "two"
从右方将值放入列表,rpush命令
rpush list right
(integer) 4
127.0.0.1:6379> lrange list 0 -1 #查看list列表中所有的值
1) "three"
2) "two"
3) "one"
4) "right"
lpop从左边移除一个元素,rpop从右边移除一个元素
127.0.0.1:6379> lpop list
"three"
127.0.0.1:6379> rpop list
"right"
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
lindex获取列表中指定索引的值
127.0.0.1:6379> lindex list 0
"two"
flushdb清空数据库
llen查看某个列表的长度
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> llen list
(integer) 3
lrem,移除list列表中的值
127.0.0.1:6379> LREM list 1 one
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "three"
3) "two"
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "three"
3) "two"
127.0.0.1:6379> lrem list 2 three
(integer) 2
127.0.0.1:6379> lrange list 0 -1
1) "two"
ltrim,截断命令,只保留指定区间内的元素
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> flushdb
OK
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> lrange mylist 0 -1
1) "hello"
2) "hello1"
3) "hello2"
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"
exists,判断某个列表是否存在
127.0.0.1:6379> exists list
(integer) 0
lset,将列表中指定下标的值,更新为新的值。lset只能更新值,不能添加值,添加值用lpush或rpush。
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> exists list
(integer) 0
127.0.0.1:6379> lpush list value1
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "value1"
127.0.0.1:6379> lset list 0 item
OK
127.0.0.1:6379> lrange list 0 -1
1) "item"
127.0.0.1:6379> lset list 1 other #不存在的下标,就会报错
(error) ERR index out of range
linsert,向列表中指定元素前或后,插入值
127.0.0.1:6379> flushdb
OK
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> LINSERT mylist before world other
(integer) 3
127.0.0.1:6379> lrange mylist 0 -1
1) "hello"
2) "other"
3) "world"
1.4 Set数据类型
set中的值不能重复。
sadd,向set数据类型中添加值。smembers,查看set数据类型中的元素。
127.0.0.1:6379> sadd myset "hello"
(integer) 1
127.0.0.1:6379> sadd myset "kuangshen"
(integer) 1
127.0.0.1:6379> sadd myset "love kuangshen"
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "love kuangshen"
2) "kuangshen"
3) "hello"
sismember,判断某个元素是否存在于指定的set集合中。
127.0.0.1:6379> SISMEMBER myset hello
(integer) 1
127.0.0.1:6379> SISMEMBER myset world
(integer) 0
scard,获取指定集合中元素的个数。
27.0.0.1:6379> scard myset
(integer) 3
srem,移除set集合中指定的元素。
127.0.0.1:6379> srem myset hello
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "love kuangshen"
2) "kuangshen"
srandmember,从set集合中随机挑选出一个元素。
127.0.0.1:6379> SRANDMEMBER myset
"kuangshen"
spop,随机从set集合中移除一个元素。
127.0.0.1:6379> SMEMBERS myset
1) "love kuangshen"
2) "kuangshen"
3) "zhouhao"
127.0.0.1:6379> SPOP myset
"zhouhao"
smov,将一个集合中的一个元素,移动到另一个集合中。
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> sadd myset kuangshen
(integer) 1
127.0.0.1:6379> sadd myset2 zhouhao
(integer) 1
127.0.0.1:6379> keys *
1) "myset"
2) "myset2"
127.0.0.1:6379> SMOVE myset myset2 hello
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "kuangshen"
2) "world"
127.0.0.1:6379> SMEMBERS myset2
1) "hello"
2) "zhouhao"
sdiff,查看两个集合的差集。
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> sadd key1 a
(integer) 1
127.0.0.1:6379> sadd key1 b
(integer) 1
127.0.0.1:6379> sadd key1 c
(integer) 1
127.0.0.1:6379> sadd key2 c
(integer) 1
127.0.0.1:6379> sadd key2 d
(integer) 1
127.0.0.1:6379> sadd key2 e
(integer) 1
127.0.0.1:6379> SDIFF key1 key2
1) "a"
2) "b"
sinter,查看两个集合的交集。
127.0.0.1:6379> SINTER key1 key2
1) "c"
sunion,获取两个集合的并集。
127.0.0.1:6379> SUNION key1 key2
1) "e"
2) "b"
3) "c"
4) "d"
5) "a"
1.5 Hash数据类型
Hash数据类型就是Map数据类型,key-,key-map
hset,设置值。hget,获取hash数据类型中的值。
127.0.0.1:6379> hset myhash field1 kuangshen
(integer) 1
127.0.0.1:6379> hget myhash field1
"kuangshen"
hmset,一次设置多个值。hmget,一次获取多个值。
127.0.0.1:6379> hmset myhash field1 hello field2 world
OK
127.0.0.1:6379> hmget myhash field1
1) "hello"
127.0.0.1:6379> hmget myhash field1 field2
1) "hello"
2) "world"
hgetall,获得hash数据类型中所有的值。
127.0.0.1:6379> hgetall myhash
1) "field1"
2) "hello"
3) "field2"
4) "world"
hdel,删除hash数据类型中的某一字段。
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> hgetall myhash
1) "field2"
2) "world"
3) "field1"
4) "hello"
127.0.0.1:6379> hexists myhash field1
(integer) 1
127.0.0.1:6379> hexists myhash field3
(integer) 0
hkeys,只获取hash中的key。hvals,只获取hash中的value。
127.0.0.1:6379> hkeys myhash
1) "field2"
2) "field1"
127.0.0.1:6379> hvals myhash
1) "world"
2) "hello"
hincrby,让某个元素增加一个值。
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> hgetall myhash
1) "field2"
2) "world"
3) "field1"
4) "hello"
5) "field3"
6) "6"
hsetnx,判断字段是否存在,不存在就创建值。如果存在,就创建失败。
127.0.0.1:6379> HSETNX myhash field4 hello
(integer) 1
127.0.0.1:6379> HSETNX myhash field4 world
(integer) 0
哈希数据类型,适合存储用户信息。
127.0.0.1:6379> hset user:1 name zhouhao age 10
(integer) 2
127.0.0.1:6379> hgetall user:1
1) "name"
2) "zhouhao"
3) "age"
4) "10"
1.6 Zset数据类型,有序集合
zadd,向集合中添加值。
127.0.0.1:6379> zadd myset 1 one
(integer) 1
127.0.0.1:6379> zadd myset 2 two 3 three
(integer) 2
zrange,获取集合中的值。
127.0.0.1:6379> zrange myset 0 -1
1) "one"
2) "two"
3) "three"
ZRANGEBYSCORE,按照score排序。
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> zadd salary 3000 xiaohong
(integer) 1
127.0.0.1:6379> zadd salary 5000 zhangsan
(integer) 1
127.0.0.1:6379> zadd salary 500 kuangshen
(integer) 1
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf
1) "kuangshen"
2) "xiaohong"
3) "zhangsan"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf withscores
1) "kuangshen"
2) "500"
3) "xiaohong"
4) "3000"
5) "zhangsan"
6) "5000"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf 4000 withscores #范围从负无穷至4000
1) "kuangshen"
2) "500"
3) "xiaohong"
4) "3000"
zrem,移除集合中,指定的key中,指定的元素。
127.0.0.1:6379> zrem salary xiaohong
(integer) 1
127.0.0.1:6379> zrange salary 0 -1
1) "kuangshen"
2) "zhangsan"
zcard,查看指定key中的元素。
127.0.0.1:6379> zcard salary
(integer) 2
zrevrange,按照倒叙排列value的值。即从大到小排序。
127.0.0.1:6379> ZREVRANGE salary 0 -1
1) "zhangsan"
2) "kuangshen"
zcount,获取指定区间内的元素数量。
127.0.0.1:6379> FLUSHDB
OK
127.0.0.1:6379> zadd myset 1 hello 2 world 3 kuangshen
(integer) 3
127.0.0.1:6379> zcount myset 1 3
(integer) 3
二、3种特殊数据类型
2.1 geospatial地理位置
geoadd,添加城市
127.0.0.1:6379> geoadd china:city 116.40 39.90 beijing
(integer) 1
127.0.0.1:6379> geoadd china:city 121.47 31.23 shanghai
(integer) 1
127.0.0.1:6379> geoadd china:city 106.50 29.53 chongqing 114.08 22.52 shenzhen
(integer) 2
127.0.0.1:6379> geoadd china:city 120.15 30.28 hangzhou 108.96 34.26 xian
(integer) 2
geopos,获取城市的经纬度。
127.0.0.1:6379> geopos china:city beijing
1) 1) "116.39999896287918091"
2) "39.90000009167092543"
127.0.0.1:6379> geopos china:city beijing chongqing
1) 1) "116.39999896287918091"
2) "39.90000009167092543"
2) 1) "106.49999767541885376"
2) "29.52999957900659211"
geodist,查看北京到上海之间的距离。
127.0.0.1:6379> GEODIST china:city beijing shanghai km
"1067.3788"
georadius,查找给定经纬度,给定半径内的城市。
127.0.0.1:6379> georadius china:city 110 30 1000 km
1) "chongqing"
2) "xian"
3) "shenzhen"
4) "hangzhou"
count参数,查看指定数量的城市。
127.0.0.1:6379> georadius china:city 110 30 500 km withcoord count 1
1) 1) "chongqing"
2) 1) "106.49999767541885376"
2) "29.52999957900659211"
GEORADIUSBYMEMBER,以城市为中心,给定半径,找出指定半径内的其它城市。
127.0.0.1:6379> GEORADIUSBYMEMBER china:city beijing 1000 km
1) "beijing"
2) "xian"
127.0.0.1:6379> GEORADIUSBYMEMBER china:city shanghai 400 km
1) "hangzhou"
2) "shanghai"
geohash,返回编码后的11位字符串
127.0.0.1:6379> geohash china:city beijing shanghai
1) "wx4fbxxfke0"
2) "wtw3sj5zbj0"
zrange,可以查看geo数据类型,key值中,的所有元素。
127.0.0.1:6379> zrange china:city 0 -1
1) "chongqing"
2) "xian"
3) "shenzhen"
4) "hangzhou"
5) "shanghai"
6) "beijing"
zrem,移除key中,指定的元素。
127.0.0.1:6379> zrem china:city beijing
(integer) 1
127.0.0.1:6379> zrange china:city 0 -1
1) "chongqing"
2) "xian"
3) "shenzhen"
4) "hangzhou"
5) "shanghai"
2.2 Hyperloglog
用途:用作网页的UA技算(一个人访问网站多次,还算作一个人)。
PFADD,向key中添加数据。
127.0.0.1:6379> PFADD myset a b c d e f g h i j
(integer) 1
pfcount,计算key中元素的个数。
127.0.0.1:6379> pfcount myset
(integer) 10
PFMERGE,合并两个key(去重合并)
127.0.0.1:6379> PFADD myset a b c d e f g h i j
(integer) 1
127.0.0.1:6379> pfcount mykey
(integer) 0
127.0.0.1:6379> pfcount myset
(integer) 10
127.0.0.1:6379> PFADD myset2 i j l m n x z y
(integer) 1
127.0.0.1:6379> PFMERGE myset3 myset myset2 #将myset,myset2合并后存放到myset3中,求并集
OK
127.0.0.1:6379> PFCOUNT myset3
(integer) 16
2.3 Bitmaps
Bitmaps,位图,也是一种数据结构。操作二进制来进行记录,只有0和1两种状态。
位存储
统计用户信息。登录、未登录,打卡、未打卡信息。
使用bitmap记录一周的打卡信息。值为0代表未打卡,值为1代表已打卡。
127.0.0.1:6379> setbit sign 0 1
(integer) 0
127.0.0.1:6379> setbit sign 1 0
(integer) 0
127.0.0.1:6379> setbit sign 2 0
(integer) 0
127.0.0.1:6379> setbit sign 3 1
(integer) 0
127.0.0.1:6379> setbit sign 4 1
(integer) 0
127.0.0.1:6379> setbit sign 5 1
(integer) 0
127.0.0.1:6379> setbit sign 6 0
(integer) 0
获取某一天的打卡信息。
127.0.0.1:6379> getbit sign 4
(integer) 1
统计打卡的总天数
127.0.0.1:6379> bitcount sign
(integer) 4
标签:127.0,0.1,list,myset,redis,笔记,学习,6379,integer 来源: https://blog.csdn.net/lemmon_tree/article/details/123070640