# Set the number of databases. The default database is DB 0, you can select # a different one on a per-connection basis using SELECT <dbid> where # dbid is a number between 0 and 'databases'-1 databases 16
Redis库的操作
切换库指令:select 库的编号
1 2 3 4 5
[root@localhost bin]# ./redis-cli -h localhost -p 6379 localhost:6379> select 0 OK localhost:6379> select 1 OK
localhost:6379> set name zhangsan OK localhost:6379> set age 19 OK localhost:6379> set bir 2021-03-25 OK localhost:6379> set content 北京市丰台区 OK localhost:6379> keys * age content name bir localhost:6379> del bir age 2 localhost:6379> keys * content name
EXISTS指令
语法: EXISTS key
作用: 检查给定key 是否存在
可用版本: >= 1.0.0
返回值: 若key 存在,返回1 ,否则返回0
1 2 3 4 5 6
localhost:6379> exists name 1 localhost:6379> exists age 0 localhost:6379> exists name age bir (只要有一个存在,就返回1) 1
EXPIRE
语法: EXPIRE key seconds
作用: 为给定key 设置生存时间,当key 过期时(生存时间为0 ),它会被自动删除。
可用版本: >= 1.0.0
时间复杂度: O(1)
返回值:设置成功返回1
1 2 3 4 5 6 7
localhost:6379> keys * content name localhost:6379> expire name 10 1 localhost:6379> keys * content
localhost:6379> set name 王小花 OK localhost:6379> get name 王小花 localhost:6379> mset age 18 bir 2012-12-12 OK localhost:6379> keys * username age name bir localhost:6379> mget name age bir 王小花 18 2012-12-12 localhost:6379> getset name 小黑 王小花 localhost:6379> get name 小黑 localhost:6379> strlen name 6 localhost:6379> append name thisgoodman 17 localhost:6379> get name 小黑thisgoodman localhost:6379> getrange name 6 16 thisgoodman localhost:6379> getrange name 6 -1 thisgoodman localhost:6379> keys *
localhost:6379> set name zhangsan OK localhost:6379> ttl name -1 localhost:6379> setex age 100 19 OK localhost:6379> ttl age 96 localhost:6379> keys * name localhost:6379> setnx name xiaohei 0 localhost:6379> get name zhangsan localhost:6379> setnx content xiaohei 1 localhost:6379> get content xiaohei localhost:6379> set age 19 OK localhost:6379> get age 19 localhost:6379> decr age 18 localhost:6379> decr age 17 localhost:6379> incr age 18 localhost:6379> incr age 19 localhost:6379> incr age 20
localhost:6379> hset maps name zhangsan 1 localhost:6379> type maps hash localhost:6379> hget maps name zhangsan localhost:6379> hset maps age 18 1 localhost:6379> hset maps bir 2012-12-12 1 localhost:6379> hgetall maps name zhangsan age 18 bir 2012-12-12 localhost:6379> hdel maps bir 1 localhost:6379> hgetall maps name zhangsan age 18 localhost:6379> hexists maps bir 0 localhost:6379> hexists maps name 1 localhost:6379> hkeys maps name age localhost:6379> hvals maps zhangsan 18 localhost:6379> hmset maps bir 2012-12-12 address beijing clazz 2001 OK localhost:6379> hgetall maps name zhangsan age 18 bir 2012-12-12 address beijing clazz 2001 localhost:6379> hmget maps name age bir content address clazz zhangsan 18 2012-12-12
beijing 2001 localhost:6379> hsetnx maps course redis 1 localhost:6379> hgetall maps name zhangsan age 18 bir 2012-12-12 address beijing clazz 2001 course redis localhost:6379> hincrby maps age 10 28 localhost:6379> hincrby maps age 10 38 localhost:6379> hincrbyfloat maps claszz 10.23423423423423423 10.23423423423423423