Redis常规命令
[root@cong11 ~]# redis-cli -h 192.168.1.11 -p 6379 -a 123456
192.168.1.11:6379> set myname "berry" #插入一条记录
OK
192.168.1.11:6379> get myname #获取myname key的值
"berry"
192.168.1.11:6379> set foo bar
OK
192.168.1.11:6379> get foo
"bar"
192.168.1.11:6379> keys * #查看所有key
-
"myname"
-
"foo"
1.2、 键的遵循:
可以使用ASCII字符
键的长度不要过长,键的长度越长则消耗的空间越多
在同一个库中(名称空间),键的名称不得重复,如果复制键的名称,实际上是修改键中的值 ##一个库就是一个名称空间

在不同的库中(名称空间),键的同一个名称可以重复 select 0
键可以实现自动过期
1.3、 Redis帮助命令
说明:redis的help命令非常强大,因为redis支持众多的数据结构,每一种数据结构当中都支持N种操作,因此需要使用 help @group方式来获取某一种数据结构所支持的操作
192.168.1.11:6379> help #获取命令帮助
redis-cli 5.0.5
To get help arubout Redis commands type:
"help @<group>" to get a list of commands in <group>
"help <command>" for help on <command>
"help <tab>" to get a list of possible help topics
"quit" to exit
To set redis-cli preferences:
":set hints" enable online hints(提示)
":set nohints" disable online hints
Set your preferences in ~/.redisclirc
查询set帮助
192.168.1.11:6379> help set #查询set帮助
SET key value [expiration EX seconds|PX milliseconds] [NX|XX]
//命令 键 值 [EX 过期时间,单位秒]
NX:如果一个键不存在,才创建并设定值,否则不允许设定
XX:如果一个键存在则设置建的值,如果不存在则不创建并不设置其值
summary: Set the string value of a key
since: 1.0.0
group: string
举例:
192.168.1.11:6379> set cjk aaa
OK
192.168.1.11:6379> set cjk bbb NX
(nil) #反回提示一个没能执行的操作
192.168.1.11:6379> get cjk
"aaa"
192.168.1.11:6379> set foo abc XX #设置foo key值,foo之前存在
OK #修改成功
192.168.1.11:6379> get foo
"abc"
查询get帮助
192.168.1.11:6379> help get #查询get帮助
GET key
summary: Get the value of a key
since: 1.0.0
group: string
查询一个命令组的帮助
#查询一个命令组的帮助,例如查询字符串类型数据的操作命令
192.168.1.11:6379> help @string (字符串帮助,没必要过于理解,了解即可)
APPEND key value
summary: Append a value to a key
since: 2.0.0
BITCOUNT key [start end]
summary: Count set bits in a string
since: 2.6.0
BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]
summary: Perform arbitrary bitfield integer operations on strings
since: 3.2.0
BITOP operation destkey key [key ...]
summary: Perform bitwise operations between strings
since: 2.6.0
BITPOS key bit [start] [end]
summary: Find first bit set or clear in a string
since: 2.8.7
DECR key
summary: Decrement the integer value of a key by one
since: 1.0.0
DECRBY key decrement
summary: Decrement the integer value of a key by the given number
since: 1.0.0
GET key
summary: Get the value of a key
since: 1.0.0
GETBIT key offset
summary: Returns the bit value at offset in the string value stored at key
since: 2.2.0
查询string组中APPEND命令帮助
192.168.1.11:6379> help APPEND #查询string组中APPEND命令帮助
APPEND key value
summary: Append a value to a key
since: 2.0.0
group: string
append添加键中的值(在原有键中附加值的内容):
192.168.1.11:6379> append cjk fda
(integer) 6
192.168.1.11:6379> get cjk
"aaafda"
获取指定键中的值的字符串的长度:
192.168.1.11:6379> strlen cjk ##查看cjk键值的长度
(integer) 6
删除键:
192.168.1.11:6379> del cjk
(integer) 1
192.168.1.11:6379> get cjk
(nil)
列表的操作:
键指向一个列表,而列表可以理解为是一个字符串的容器,列表是由众多元素组成的集合,可以在键所指向的列表中附加一个值
LPUSH //在键所指向的列表前面插入一个值(左边加入)
RPUSH //在键所指向的列表后面附加一个值(右边加入)
LPOP //在键所指向的列表前面弹出一个值(左边弹出)
RPOP //在键所指向的列表后面弹出一个值(右边弹出)
LINDEX //根据索引获取值,指明索引位置进行获取对应的值
LSET //用于修改指定索引的值为指定的值
1、 创建一个列表
创建一个新的列表,在帮助中并没产明哪个命令用于创建一个新的列表,实际上创建一个新的列表使用LPUSH或RPUSH都可以。
192.168.1.11:6379> lpush ll cjk # ll为列表名称,cjk为值
(integer) 1
192.168.1.11:6379> lindex ll 0 #第一个索引(值)则为0
"cjk"
2、 在原有的列表中的左侧加入一个值
192.168.1.11:6379> lpush ll fda #在原有的列表中的左侧加入一个值
(integer) 2
192.168.1.11:6379> lindex ll 0
"fda"
192.168.1.11:6379> lindex ll 1
"cjk"
3、 在原有的列表中的右侧加入一个值
192.168.1.11:6379> rpush ll www
(integer) 3
192.168.1.11:6379> lindex ll 2
"www"
192.168.1.11:6379> lindex ll 1
"cjk"
192.168.1.11:6379> lindex ll 0
"fda"
4、 修改一个已有的列表中的值:
192.168.1.11:6379> lset ll 0 abc
OK
192.168.1.11:6379> lindex ll 0
"abc"
5、 查看列表中的值的数量
192.168.1.11:6379> llen ll
(integer) 3
6、 在已有的列表中右侧弹出(删除)一个值
192.168.1.11:6379> rpop ll
"www"
7、 在已有的列表中左侧弹出(删除)一个值
192.168.1.11:6379> lpop ll
"abc"
192.168.1.11:6379> lpop ll
"cjk"
192.168.1.11:6379> lpop ll
(nil)
8、 清空数据库:
FLUSHDB:删除当前选择的数据库所有key
FLUSHALL:清空所有库
192.168.1.11:6379> flushdb
OK
type查看 key 的数据类型
数据类型有:
none (key不存在)
string (字符串)
list (列表)
set (集合)
zset (有序集)
hash (哈希表)
192.168.1.11:6379> set myname "zhenglincong"
192.168.1.11:6379> type myname #查看key的数据查看类型
string #字符串
192.168.1.11:6379> lpush book_list "programming in scala" #插入一个列表
(integer) 1
192.168.1.11:6379> type book_list
list #列表
192.168.1.11:6379> sadd myset "hello" #插入一个集合
(integer) 1
192.168.1.11:6379> sadd myset "foo" "world"
(integer) 2
注:Redis Sadd 命令将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略。
192.168.1.11:6379> smembers myset
-
"foo"
-
"world"
-
"hello"
192.168.1.11:6379> type myset
set
Pexpireat设置 key 的过期时间以毫秒计
Redis PEXPIREAT 命令用于设置 key 的过期时间,以毫秒计。key 过期后将不再可用。
实例:
首先创建一个 key 并赋值:
192.168.1.11:6379> set w3 redis #创建一条记录
OK
192.168.1.11:6379> PEXPIREAT w3 10000 #为 key 设置过期时间
(integer) 1
查看redis状态信息
192.168.1.11:6379> info #查看redis状态信息

官网命令详解