String常用命令如下:
127.0.0.1:6379> exists name //查看某字段是否存在 (integer) 1 127.0.0.1:6379> keys * //查看该数据库下所有key 1) "name" 127.0.0.1:6379> get name //查看key对应的value "wenzemin" 127.0.0.1:6379> expire name 10 //设置字段有效时间(s) (integer) 1 127.0.0.1:6379> get name //10s之后查看,显示为null (nil) 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> set name wenzemin //设置K-V键值对 OK 127.0.0.1:6379> set age 1 OK 127.0.0.1:6379> type age 查看某字段的数据类型 string 127.0.0.1:6379> append name ",hello" //在某字段后面追加内容,若该字段不存在,则相当于set name xxx (integer) 14 127.0.0.1:6379> get name "wenzemin,hello" 127.0.0.1:6379> strlen name //获取某字段对应的value值的长度 (integer) 14 ######################################################### 127.0.0.1:6379> set views 0 //设置初始浏览量为0 OK 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> get views "0" 127.0.0.1:6379> incrby views 10 //设置步长,指定增量为10 (integer) 10 127.0.0.1:6379> incrby views 10 (integer) 20 127.0.0.1:6379> decrby views 5 (integer) 15 127.0.0.1:6379> decrby views 5 (integer) 10 ######################################################## //设置字符串范围(Range) 127.0.0.1:6379> set key hello,world OK 127.0.0.1:6379> get key "hello,world" 127.0.0.1:6379> getrange key 0 4 //截取字符串[0,4] "hello" 127.0.0.1:6379> getrange key 0 -14 "h" 127.0.0.1:6379> getrange key 0 -1 //获取全部字符串,和get一样 "hello,world" //替换 127.0.0.1:6379> setrange key 6 sunshine //从某下标开始替换 (integer) 14 127.0.0.1:6379> get key "hello,sunshine" //setex(set with expire) 设置过期时间 //setnx(set if not exist)不存在再设置(在分布式锁中常常使用) 127.0.0.1:6379> setex name 10 maria 设置name过期时间为10s OK 127.0.0.1:6379> ttl name //查看有效时间 (integer) 6 127.0.0.1:6379> setnx hobby study (integer) 1 127.0.0.1:6379> keys * 1) "hobby" 2) "key" 127.0.0.1:6379> get name (nil) 127.0.0.1:6379> ttl name (integer) -2 127.0.0.1:6379> setnx hobby running //hobby如果存在,则创建失败 (integer) 0 127.0.0.1:6379> get hobby "study" ########################################################### mset mget 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) ####################################################### //对象 set user:1 {name:zhangsan,age:18} 设置一个user:1对象,值为json字符串来保存一个对象 127.0.0.1:6379> mset user:1:name zhangsan user:1:age 18 OK 127.0.0.1:6379> mget user:1:name user:1:age 1) "zhangsan" 2) "18" 127.0.0.1:6379> getset db redis //先get然后set (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"
在Redis里面,可以用LIst实现栈、队列、阻塞队列
所有的List命令都是用 l 开头
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 1) "three" 2) "two" 3) "one" 127.0.0.1:6379> LRANGE list 0 1 1) "three" 2) "two" 127.0.0.1:6379> RPUSH list zero //将一个值或多个值插入到列表尾部(右) (integer) 4 127.0.0.1:6379> LRANGE list 0 -1 1) "three" 2) "two" 3) "one" 4) "zero" ![在这里插入图片描述](https://www.www.zyiz.net/i/ll/?i=20210606181646477.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl81NjYxNjMyNQ==,size_16,color_FFFFFF,t_70#pic_center) 127.0.0.1:6379> LPOP list //移除list的第一个元素 "three" 127.0.0.1:6379> RPOP list //移除list的最后一个元素 "zero" 127.0.0.1:6379> LRANGE list 0 -1 1) "two" 2) "one" 127.0.0.1:6379> LINDEX list 0 //通过下标获得list中的某一个值 "two" 127.0.0.1:6379> LINDEX list 1 "one" 127.0.0.1:6379> LLEN list //返回列表的长度Llen,命令不区分大小写 (integer) 2