这篇文章简单的记录一下redis的基本使用,一方面做一个分享,另一方面也怕自己忘记。
因为刚写博客不久,关注,收藏,评论,博主会及时回关。有任何建议可直接评论共同进步!
使用Docker启动Redis镜像
docker pull redis ## 拉取redis镜像,默认是最新版本
docker exec -it java_redis ## 使用PowerShell启动redis容器
Redis的数据类型
- String
- List
- Set
- Hash
- Zset
String数据类型指令
存储键值:set key value
127.0.0.1:6379> set keyName valueName
OK
获取值:get key
127.0.0.1:6379> get keyName
"valueName"
拼接字符串:append key value
127.0.0.1:6379> append keyName after
(integer) 14
127.0.0.1:6379> get keyName
"valueNameafter"
获取字符串长度:strlen key
127.0.0.1:6379> strlen keyName
(integer) 14
自增值:实现类似于java中的i++
127.0.0.1:6379> set count 0
OK
127.0.0.1:6379> incr count
(integer) 1
127.0.0.1:6379> get count
"1"
127.0.0.1:6379> incr count
(integer) 2
127.0.0.1:6379> get count
"2"
自减值:
127.0.0.1:6379> get count
"2"
127.0.0.1:6379> decr count
(integer) 1
127.0.0.1:6379> get count
"1"
自增值设置步长:
127.0.0.1:6379> get count
"1"
127.0.0.1:6379> incrby count 10
(integer) 11
127.0.0.1:6379> get count
"11"
获取范围字符串:如果end值为-1,则获取全部字符
127.0.0.1:6379> get keyName
"valueNameafter"
127.0.0.1:6379> getrange keyName 0 4
"value"
将指定索引的字符更换:如下,将valueNameafter中的“N”更换为example
127.0.0.1:6379> setrange keyName 5 example
(integer) 14
127.0.0.1:6379> get keyName
"valueexampleer"
如果存在key,则设置此key有效时长为10s,值为abc
127.0.0.1:6379> setex key 10 abc
OK
如果不存在key,则设置值为abc
127.0.0.1:6379> setnx key1 abc
(integer) 1
设置key的有效时长
127.0.0.1:6379> expire count 10 ##设置key有效时长为10s
(integer) 1
127.0.0.1:6379> ttl count ## 查看有效时长
(integer) 6
127.0.0.1:6379> get count ## 查看值
"11"
127.0.0.1:6379> ttl count ##当有效时长为-2时,表示失效
(integer) -2
127.0.0.1:6379> get count
(nil)
批量插入、获取键值
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3
OK
127.0.0.1:6379> mget k1 k2 k3
1) "v1"
2) "v2"
3) "v3"
插入对象类型
127.0.0.1:6379> set user:1 {name:zhangsan,age:3}
OK
127.0.0.1:6379> get user:1
"{name:zhangsan,age:3}"
getset:先获取再设置
127.0.0.1:6379> getset db redis
(nil)
127.0.0.1:6379> get db
"redis"
127.0.0.1:6379> getset db mysql
"redis"
127.0.0.1:6379> get db
"mysql"
List数据类型指令
在redis中,List可以作为栈、队列、阻塞队列使用
前后赋值、移除元素、通过下标获取值、获取list长度、移除指定的元素、通过下标截取list、根据下标修改值、移动值到另一个list、前后插入值
给list赋值
127.0.0.1:6379> lpush list1 1 2 3 4 5 ## 从左边开始赋值
(integer) 5
127.0.0.1:6379> lrange list1 0 -1 ## 获取list中的值,可以看到是先进后出的
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> rpush list1 6 7 ## 从右边赋值
(integer) 7
127.0.0.1:6379> lrange list1 0 -1 ## 注意6 7的位置
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
6) "6"
7) "7"
移除元素
127.0.0.1:6379> lpop list1 2 ## 从左边移除两个元素
1) "5"
2) "4"
127.0.0.1:6379> lrange list1 0 -1 ## 可以看到左边的 5,4被移除了
1) "3"
2) "2"
3) "1"
4) "6"
5) "7"
127.0.0.1:6379> rpop list1 ## 如果不写数量,则默认为1
"7"
127.0.0.1:6379> lrange list1 0 -1
1) "3"
2) "2"
3) "1"
4) "6"
通过下标获取值
127.0.0.1:6379> lindex list1 0
"3"
获取list长度
127.0.0.1:6379> llen list1
(integer) 4
移除list中指定数量的value
127.0.0.1:6379> lrem list1 1 3 ## 移除list中的1个3元素
(integer) 1
127.0.0.1:6379> lrange list1 0 -1
1) "2"
2) "1"
3) "6"
通过下标截取list(实质上修改了list)
127.0.0.1:6379> ltrim list1 0 1
OK
127.0.0.1:6379> lrange list1 0 -1
1) "2"
2) "1"
把list1的左边第一个值移到list2中:rpoplpush
127.0.0.1:6379> lrange list1 0 -1
1) "2"
2) "1"
127.0.0.1:6379> rpoplpush list1 list2
"1"
127.0.0.1:6379> lrange list1 0 -1
1) "2"
127.0.0.1:6379> lrange list2 0 -1
1) "1"
根据下标修改值:lset
127.0.0.1:6379> lrange list2 0 -1
1) "2"
127.0.0.1:6379> lset list2 0 1
OK
127.0.0.1:6379> lrange list2 0 -1
1) "1"
在指定元素前后插入值:linsert
127.0.0.1:6379> lrange list2 0 -1
1) "1"
127.0.0.1:6379> linsert list2 after "1" "hello"
(integer) 2
127.0.0.1:6379> lrange list2 0 -1
1) "1"
2) "hello"
Set数据类型指令
set集合不能有重复值
插入值及查询值:sadd、smembers
127.0.0.1:6379> sadd set "hello"
(integer) 1
127.0.0.1:6379> sadd set "caozuogong"
(integer) 1
127.0.0.1:6379> sadd set "redis"
(integer) 1
127.0.0.1:6379> smember set
127.0.0.1:6379> smembers set
1) "redis"
2) "hello"
3) "caozuogong"
判断元素是否在集合中:sismember
127.0.0.1:6379> sismember set hello
(integer) 1
查看集合中有多少个元素:scard
127.0.0.1:6379> scard set
(integer) 3
移除指定元素:srem
127.0.0.1:6379> srem set hello
(integer) 1
127.0.0.1:6379> smembers set
1) "redis"
2) "caozuogong"
随机抽取元素:srandmember,后面可以指定个数
127.0.0.1:6379> srandmember set
"redis"
127.0.0.1:6379> srandmember set
"caozuogong"
127.0.0.1:6379> srandmember set
"caozuogong"
随机移除元素:spop,后面可以指定个数
127.0.0.1:6379> spop set
"redis"
127.0.0.1:6379> smembers set
1) "caozuogong"
将一个指定值移到另一个集合中
127.0.0.1:6379> smembers set
1) "caozuogong"
2) "mysql"
127.0.0.1:6379> smove set set1 mysql
(integer) 1
127.0.0.1:6379> smembers set
1) "caozuogong"
127.0.0.1:6379> smembers set1
1) "mysql"
取差集sdiff 、交集sinter 、并集sunion
127.0.0.1:6379> smembers set
1) "redis"
2) "caozuogong"
3) "mysql"
127.0.0.1:6379> smembers set1
1) "mysql"
127.0.0.1:6379> sdiff set set1
1) "redis"
2) "caozuogong"
127.0.0.1:6379> sinter set set1
1) "mysql"
127.0.0.1:6379> sunion set set1
1) "redis"
2) "caozuogong"
3) "mysql"
Hash数据类型操作
hash值的存取:hset、hget,其中myhash为key,field为map
127.0.0.1:6379> hset myhash field caozuogong
(integer) 1
127.0.0.1:6379> hget myhash field
"caozuogong"
一次存取多个值:hmset、hmget
127.0.0.1:6379> hmset myhash1 field v1 field1 v2 field2 v3
OK
127.0.0.1:6379> hmget myhash1
127.0.0.1:6379> hmget myhash1 field field1 field2
1) "v1"
2) "v2"
3) "v3"
一次获取所有值:hgetall,以key-value方式呈现
127.0.0.1:6379> hgetall myhash1
1) "field"
2) "v1"
3) "field1"
4) "v2"
5) "field2"
6) "v3"
删除指定field:hdel
127.0.0.1:6379> hdel myhash1 field
(integer) 1
127.0.0.1:6379> hgetall myhash1
1) "field1"
2) "v2"
3) "field2"
4) "v3"
查看集合中有多少个值:hlen
127.0.0.1:6379> hlen myhash1
(integer) 2
判断指定字段存不存在:hexists
127.0.0.1:6379> hgetall myhash1
1) "field1"
2) "v2"
3) "field2"
4) "v3"
127.0.0.1:6379> hexists myhash1 field1
(integer) 1
127.0.0.1:6379> hexists myhash1 field
(integer) 0
只获取key或value:hkeys、hvals
127.0.0.1:6379> hkeys myhash1
1) "field1"
2) "field2"
127.0.0.1:6379> hvals myhash1
1) "v2"
2) "v3"
自增自减
127.0.0.1:6379> hset myhash1 field 3
(integer) 1
127.0.0.1:6379> hincrby myhash1 field 10
(integer) 13
127.0.0.1:6379> hget myhash1 field
"13"
如果不存在就设置:hsetnx
127.0.0.1:6379> hsetnx myhash1 field 3
(integer) 0
Zset(有序集合)
在set的基础上,增加了一个值,
存取值:zadd、zrange
127.0.0.1:6379> zadd zset 1 one
(integer) 1
127.0.0.1:6379> zadd zset 2 two
(integer) 1
127.0.0.1:6379> zadd zset 3 three
(integer) 1
127.0.0.1:6379> zrange zset 0 -1
1) "one"
2) "two"
3) "three"
查询元素
127.0.0.1:6379> zrangebyscore salary -inf +inf # 普通查询
1) "lisi"
2) "zhangsan"
3) "wangwu"
127.0.0.1:6379> zrangebyscore salary -inf +inf withscores # 带值查询
1) "lisi"
2) "2000"
3) "zhangsan"
4) "2500"
5) "wangwu"
6) "3000"
127.0.0.1:6379> zrangebyscore salary -inf 2500 withscores #查询小于等于2500的
1) "lisi"
2) "2000"
3) "zhangsan"
4) "2500"
移除集合中指定元素:zrem
127.0.0.1:6379> zrem salary lisi
(integer) 1
127.0.0.1:6379> zrange salary 0 -1
1) "zhangsan"
2) "wangwu"
127.0.0.1:6379> zrange salary 0 -1 withscores
1) "zhangsan"
2) "2500"
3) "wangwu"
4) "3000"
从大到小获取元素:zrevrange
127.0.0.1:6379> zrevrange salary 0 -1 withscores
1) "wangwu"
2) "3000"
3) "zhangsan"
4) "2500"
获取集合中的个数:zcard
127.0.0.1:6379> zcard salary
(integer) 2
按区间计算个数:zcount
127.0.0.1:6379> zcount salary 2500 3000
(integer) 2