在平时的工作中,需要根据需求对Redis数据库进行一些操作。
可以参考Redis官网http://redis.io/commands 进行详细了解
1.SELECT 切换数据库
redis 127.0.0.1:6379[1]> HELP SELECT
SELECT index
summary: Change the selected database for the current connection
since: 1.0.0
group: connection
redis 127.0.0.1:6379[1]> SELECT 2
OK
2.LLEN 得到一个列表的长度
redis 127.0.0.1:6379[2]> HELP LLEN LLEN key summary: Get the length of a list since: 1.0.0 group: listredis 127.0.0.1:6379[2]> LLEN bi(integer) 412
3.LRANGE 获取一个列表的所有元素
LRANGE 索引是以0开始的,0表示第一个元素,-1表示最后一个元素
redis 127.0.0.1:6379[2]> HELP LRANGE LRANGE key start stop summary: Get a range of elements from a list since: 1.0.0 group: listredis 127.0.0.1:6379[2]> LRANGE bi 0 5
4.LPUSH 将一个或多个值添加到一个列表的开头
redis 127.0.0.1:6379[2]> HELP LPUSH LPUSH key value [value ...] summary: Prepend one or multiple values to a list since: 1.0.0 group: listredis 127.0.0.1:6379[2]> LPUSH bi http://abc.com/logUserLogin?event_id=25&uid=de721bcef5cba1fc182d18
5.RPUSH 将一个或多个值追加到一个列表的末尾
redis 127.0.0.1:6379[2]> HELP RPUSH RPUSH key value [value ...] summary: Append one or multiple values to a list since: 1.0.0 group: listredis 127.0.0.1:6379[2]> RPUSH bi http://abc.com/logUserLogin?event_id=25&uid=de721bcef5cba1fc182d18
6.SAVE 同步数据到磁盘
SAVE命令执行的时候会阻塞连接,所以生成环境最好使用BGSAVE命令
redis 127.0.0.1:6379[2]> HELP SAVE SAVE - summary: Synchronously save the dataset to disk since: 1.0.0 group: serverredis 127.0.0.1:6379[2]> SAVEOK(1.33s)
7.BGSAVE 异步数据到磁盘
使用BGSAVE,Redis将会在后台执行保存数据的操作,不影响正常的客户端连接,Redis将会fork出一个子进程用于保存数据,父进程继续处理客户端请求。
redis 127.0.0.1:6379[2]> HELP BGSAVE BGSAVE - summary: Asynchronously save the dataset to disk since: 1.0.0 group: serverredis 127.0.0.1:6379[2]> BGSAVEBackground saving started
8.TYPE 判断一个KEY的类型
redis 127.0.0.1:6379[2]> HELP TYPE TYPE key summary: Determine the type stored at key since: 1.0.0 group: genericredis 127.0.0.1:6379[2]> TYPE bilist
9.BGREWRITEAOF
异步重写AOF文件,Redis将会创建一个对当前AOF文件优化过的AOF版本。
redis 127.0.0.1:6379> help BGREWRITEAOF BGREWRITEAOF - summary: Asynchronously rewrite the append-only file since: 1.0.0 group: server
10.CONFIG GET
获取某个配置项的值
redis 127.0.0.1:6379> help config get CONFIG GET parameter summary: Get the value of a configuration parameter since: 2.0.0 group: serverredis 127.0.0.1:6379> config get maxmemory1) "maxmemory"2) "0"
11.CONFIG SET
设置某个参数的值
redis 127.0.0.1:6379> help config set CONFIG SET parameter value summary: Set a configuration parameter to the given value since: 2.0.0 group: serverredis 127.0.0.1:6379> config set maxmemory 200000000OK
12.DBSIZE
返回当前数据库的KEY值得数量
redis 127.0.0.1:6379[3]> HELP DBSIZE DBSIZE - summary: Return the number of keys in the selected database since: 1.0.0 group: serverredis 127.0.0.1:6379[3]> dbsize(integer) 12502
13.DEL
删除一个KEY值
redis 127.0.0.1:6379> help del DEL key [key ...] summary: Delete a key since: 1.0.0 group: genericredis 127.0.0.1:6379> del foo(integer) 1
14.EXISTS
检查一个KEY是否存在
redis 127.0.0.1:6379> help exists EXISTS key summary: Determine if a key exists since: 1.0.0 group: genericredis 127.0.0.1:6379> exists foo(integer) 1
15.SET 命令
设置一个KEY的值
redis 127.0.0.1:6379> help set SET key value summary: Set the string value of a key since: 1.0.0 group: stringredis 127.0.0.1:6379> set foo testOKredis 127.0.0.1:6379>
16.PERSIST
删除一个KEY的过期时间
edis 127.0.0.1:6379> help persist PERSIST key summary: Remove the expiration from a key since: 2.2.0 group: generic
17.RENAME
重新命名一个KEY
redis 127.0.0.1:6379> help rename RENAME key newkey summary: Rename a key since: 1.0.0 group: genericredis 127.0.0.1:6379> rename foo footestOKredis 127.0.0.1:6379>
18.EXPIRE
为一个KEY设置一个TTL过期时间
redis 127.0.0.1:6379> help expire EXPIRE key seconds summary: Set a key's time to live in seconds since: 1.0.0 group: genericredis 127.0.0.1:6379> expire footest 300(integer) 1
19.TTL
获取过期时间
redis 127.0.0.1:6379> help ttl TTL key summary: Get the time to live for a key since: 1.0.0 group: genericredis 127.0.0.1:6379> ttl footest(integer) 289redis 127.0.0.1:6379> ttl footest(integer) 285redis 127.0.0.1:6379> ttl footest(integer) 283redis 127.0.0.1:6379> ttl footest(integer) 282redis 127.0.0.1:6379> ttl footest(integer) 282redis 127.0.0.1:6379>
20.EXPIREAT
设置一个KEY的过期时间,以UNIX时间戳表示
redis 127.0.0.1:6379> help expireat EXPIREAT key timestamp summary: Set the expiration for a key as a UNIX timestamp since: 1.2.0 group: genericredis 127.0.0.1:6379> expireat foo 1431532800(integer) 1redis 127.0.0.1:6379> ttl foo(integer) 3210141
21.GET
获取一个KEY的值
redis 127.0.0.1:6379> help get GET key summary: Get the value of a key since: 1.0.0 group: stringredis 127.0.0.1:6379> get foo"test"
22.HGET
获取一个哈希字段的值
redis 127.0.0.1:6379> help hget HGET key field summary: Get the value of a hash field since: 2.0.0 group: hashredis 127.0.0.1:6379> hset myhash field1 "foo"(integer) 1redis 127.0.0.1:6379> hget myhash field1"foo"redis 127.0.0.1:6379> hget myhash field2(nil)redis 127.0.0.1:6379>
23.LASTSAVE
上次成功保存数据到磁盘的UNIX时间戳
redis 127.0.0.1:6379> help lastsave LASTSAVE - summary: Get the UNIX time stamp of the last successful save to disk since: 1.0.0 group: serverredis 127.0.0.1:6379> lastsave(integer) 1428373205redis 127.0.0.1:6379>
24.LPUSH
将一个或多个值附加到一个Redis列表中
redis 127.0.0.1:6379> help lpush LPUSH key value [value ...] summary: Prepend one or multiple values to a list since: 1.0.0 group: list redis 127.0.0.1:6379> lpush mylist a b c(integer) 6redis 127.0.0.1:6379> LRANGE mylist 0 -11) "c"2) "b"3) "a"4) "c"5) "b"6) "a"redis 127.0.0.1:6379> llen mylist(integer) 6
本文出自 “Linux SA John” 博客,请务必保留此出处http://john88wang.blog.51cto.com/2165294/1440267