1、启动redis服务,redis-server 通过哪个配置文件启动
redis-server kconfig/redis.conf
2、客户端连接
redis-cli -p 6379
3、select
redis默认有16个数据库,相互切换用select,默认为第0个数据库,select 0
select 1
4、keys *,查看redis中的存储的key
127.0.0.1:6379> keys * 1) "myhash" 2) "mylist" 3) "name" 4) "counter:__rand_int__" 5) "key:__rand_int__" 127.0.0.1:6379>
5、flushdb和flushall清空当前表和清空所有
127.0.0.1:6379> keys * 1) "myhash" 2) "mylist" 3) "name" 4) "counter:__rand_int__" 5) "key:__rand_int__" 127.0.0.1:6379> keys * 1) "myhash" 2) "mylist" 3) "name" 4) "counter:__rand_int__" 5) "key:__rand_int__" 127.0.0.1:6379> flushdb OK 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> <br><br>127.0.0.1:6379> flushall OK<br>清空所有表
6、dbsize查看db大小
127.0.0.1:6379> dbsize (integer) 0 127.0.0.1:6379> set name zhangsan OK 127.0.0.1:6379> dbsize (integer) 1 127.0.0.1:6379>
7、exists key判断该key是否存在
127.0.0.1:6379> exists name (integer) 1 127.0.0.1:6379> get name "zhangsan"
8、move key db 删除该db的key对应的值,db为1代表当前数据库
127.0.0.1:6379> move name 1 (integer) 1 127.0.0.1:6379> keys * (empty array)
9、expire name second 在second 秒后key为name的值过期
127.0.0.1:6379> set name wang OK 127.0.0.1:6379> get name "wang" 127.0.0.1:6379> expire name 5 (integer) 1 127.0.0.1:6379> get name (nil) 127.0.0.1:6379>
10、ttl查看当前key的过期时间
127.0.0.1:6379> ttl name (integer) 6 127.0.0.1:6379> ttl name (integer) 2 127.0.0.1:6379>
11、type key 查看key的数据类型
127.0.0.1:6379> set name zhangsan OK 127.0.0.1:6379> type name string 127.0.0.1:6379>
String类型常用方法
12、append 追加字符串
127.0.0.1:6379> get name "zhangsan" 127.0.0.1:6379> append name 3333 (integer) 12 127.0.0.1:6379> get name "zhangsan3333" 127.0.0.1:6379>
13、strlen查看字符串的长度
127.0.0.1:6379> strlen name (integer) 12 127.0.0.1:6379> get name "zhangsan3333" 127.0.0.1:6379>
14、incr自增1
127.0.0.1:6379> set views 0 OK 127.0.0.1:6379> incr views (integer) 1 127.0.0.1:6379> get views "1" 127.0.0.1:6379> incr views (integer) 2 127.0.0.1:6379> get views "2"
15、decr自减1
127.0.0.1:6379> get views "3" 127.0.0.1:6379> decr views (integer) 2 127.0.0.1:6379> get views "2"
16、decrby key step与incrby key step 减少增加,可设置步长,指定增量
127.0.0.1:6379> get views "2" 127.0.0.1:6379> incrby views 2 (integer) 4 127.0.0.1:6379> get views "4" 127.0.0.1:6379> decrby views 3 (integer) 1 127.0.0.1:6379> get views "1"
17、getrange key start end 截取字符串(end为-1,代表结尾)
127.0.0.1:6379> get name "zhangsan3333" 127.0.0.1:6379> getrange name 3 5 "ngs" 127.0.0.1:6379> getrange name 3 8 "ngsan3"
18、setrange key start value 指定位置替换指定的值
127.0.0.1:6379> get name "zhangsan3333" 127.0.0.1:6379> setrange name 2 KKKK (integer) 12 127.0.0.1:6379> get name "zhKKKKan3333"
19、setex(set with expire) #设置过期时间 setnx(set if not exist) #不存在设置 (在分布式锁中常用)
127.0.0.1:6379> setex key1 50 ceshi OK 127.0.0.1:6379> get key1 "ceshi" 127.0.0.1:6379> ttl key1 (integer) 39 127.0.0.1:6379> keys * 1) "views" 2) "key1" 3) "name" 127.0.0.1:6379> setnx key2 ceshi (integer) 1 127.0.0.1:6379> setnx key2 ceshis (integer) 0 127.0.0.1:6379> get key2 "ceshi" 127.0.0.1:6379>
20、mset,mget批量设置和获取值
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"
21、msetnx 为原子性操作,失败的话全部失败。
127.0.0.1:6379> msetnx k1 v1 k6 v6 k8 v8 (integer) 0
22、getset 先get后set #如果不存在,返回nil,如果存在,返回之前的值,并设置新的值
127.0.0.1:6379> get k1 "v1" 127.0.0.1:6379> get m2 (nil) 127.0.0.1:6379> getset m2 hello (nil) 127.0.0.1:6379> getset k1 java "v1" 127.0.0.1:6379> get k1 "java" 127.0.0.1:6379>
list型常用方法
23、lpush 将一个值或者多个值插入到列表头部
127.0.0.1:6379> lpush lists test (integer) 4 127.0.0.1:6379> lrange lists 0 -1 1) "test" 2) "three" 3) "two" 4) "one" 127.0.0.1:6379>
24、lrange遍历list数组
127.0.0.1:6379> lrange lists 0 -1 #获取list中的所有值 1) "test" 2) "three" 3) "two" 4) "one" 127.0.0.1:6379> lrange lists 2 5 #通过区间获取具体的值 1) "two" 2) "one"
25、rpush在列表的尾部插入值
127.0.0.1:6379> lrange lists 0 -1 1) "test" 2) "three" 3) "two" 4) "one" 127.0.0.1:6379> rpush lists end (integer) 5 127.0.0.1:6379> lrange lists 0 -1 1) "test" 2) "three" 3) "two" 4) "one" 5) "end"
26、lpop rpop 移除列表的值
127.0.0.1:6379> lrange lists 0 -1 1) "test" 2) "three" 3) "two" 4) "one" 5) "end" 127.0.0.1:6379> lpop lists "test" 127.0.0.1:6379> lrange lists 0 -1 1) "three" 2) "two" 3) "one" 4) "end"
27、lindex 获取下标值
127.0.0.1:6379> lrange lists 0 -1 1) "three" 2) "two" 3) "one" 4) "end" 127.0.0.1:6379> lindex lists 2 "one"
28、llen获取数组的长度
127.0.0.1:6379> llen lists (integer) 4
29、lrem 移除指定的值,移除list集合中指定个数的value,精确匹配
127.0.0.1:6379> lrange lists 0 -1 1) "three" 2) "two" 3) "one" 4) "end" 127.0.0.1:6379> lpush lists one (integer) 5 127.0.0.1:6379> lrange lists 0 -1 1) "one" 2) "three" 3) "two" 4) "one" 5) "end" 127.0.0.1:6379> lrem lists 2 one (integer) 2 127.0.0.1:6379> lrange lists 0 -1 1) "three" 2) "two" 3) "end" 127.0.0.1:6379> lrem lists 2 two (integer) 1 127.0.0.1:6379> lrange lists 0 -1 1) "three" 2) "end"
30、ltrim通过下标截取指定的长度
127.0.0.1:6379> lrange lists 0 -1 1) "two" 2) "one" 3) "three" 4) "end" 127.0.0.1:6379> ltrim lists 0 2 OK 127.0.0.1:6379> lrange lists 0 -1 1) "two" 2) "one" 3) "three"
31、rpoplpush移除列表的最后一个元素,将他移动到新的列表中
127.0.0.1:6379> lrange lists 0 -1 1) "two" 2) "one" 3) "three" 127.0.0.1:6379> rpoplpush lists otherlists "three" 127.0.0.1:6379> lrange lists 0 -1 1) "two" 2) "one" 127.0.0.1:6379> lrange otherlists 0 -1 1) "three"
32、lset将列表中指定下标替换为指定的值(更新)不存在下标报错,存在更新
127.0.0.1:6379> lrange lists 0 -1 1) "two" 2) "one" 127.0.0.1:6379> lset lists 0 test OK 127.0.0.1:6379> lrange lists 0 -1 1) "test" 2) "one" 127.0.0.1:6379> lset lists 3 hello (error) ERR index out of range
33、linsert 将某个具体的value插入到列表某个元素之前或之后
127.0.0.1:6379> lrange lists 0 -1 1) "test" 2) "one" 127.0.0.1:6379> linsert lists before one adds (integer) 3 127.0.0.1:6379> lrange lists 0 -1 1) "test" 2) "adds" 3) "one" 127.0.0.1:6379> linsert lists after one addss (integer) 4 127.0.0.1:6379> lrange lists 0 -1 1) "test" 2) "adds" 3) "one" 4) "addss" 127.0.0.1:6379>
set型常用方法
34、sadd 给set中添加元素
127.0.0.1:6379> clear 127.0.0.1:6379> sadd keys one (integer) 1 127.0.0.1:6379> sadd keys two (integer) 1 127.0.0.1:6379> sadd keys hello
35、smembers 查看set中的元素
127.0.0.1:6379> smembers keys 1) "one" 2) "hello" 3) "two" 127.0.0.1:6379>
36、sismember 查看set元素中是否存在该元素
127.0.0.1:6379> smembers keys 1) "one" 2) "hello" 3) "two" 127.0.0.1:6379> sismember keys one (integer) 1 127.0.0.1:6379> sismember keys test (integer) 0 127.0.0.1:6379>
37、scard 获取set集合中元素的个数
127.0.0.1:6379> scard keys (integer) 3
38、srem 移除set中的指定元素
127.0.0.1:6379> smembers keys 1) "one" 2) "hello" 3) "two" 127.0.0.1:6379> srem keys one (integer) 1 127.0.0.1:6379> smembers keys 1) "hello" 2) "two"
39、srandmember 随机获取set中的值
127.0.0.1:6379> srandmember keys "hello" 127.0.0.1:6379> srandmember keys "two" 127.0.0.1:6379> srandmember keys "two" 127.0.0.1:6379> srandmember keys 2 1) "hello" 2) "two"
40、spop 随机删除元素
127.0.0.1:6379> spop keys "two" 127.0.0.1:6379> spop keys "hello" 127.0.0.1:6379>