redis相关记录

安装

brew install redis
brew services start redis

redis-cli

redis-cli, the Redis command line interface

redis-cli –help

交互模式

没有带参数的情况下,redis-cli进入交互模式

redis-cli
127.0.0.1:6379> help
redis-cli 4.0.2
To get help about 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

help @<category> shows all the commands about a given category. The categories are: @generic, @list, @set, @sorted_set, @hash, @pubsub, @transactions, @connection, @server, @scripting, @hyperloglog.
help <commandname> shows specific help for the command given as argument

命令行使用

$ redis-cli incr mycounter (显示type)
(integer) 1
$ redis-cli --scan
mycounter
$ redis-cli incr mycounter > /data/tmp/output.txt (不显示type)
$ cat /data/tmp/output.txt
2
$ redis-cli --raw incr mycounter (不显示type)
3
$ ps aux | grep redis
/usr/local/opt/redis/bin/redis-server 127.0.0.1:6379
$ redis-cli -h 127.0.0.1 -p 6379 ping
PONG

清空缓存

redis-cli flushdb // 删除当前数据库中的所有Key
redis-cli flushall // 删除所有数据库中的key

使用-n 选择使用的数据库

using the -n <dbnum> option
$ redis-cli flushall
OK
$ redis-cli -n 1 incr a
(integer) 1
$ redis-cli -n 1 incr a
(integer) 2
$ redis-cli -n 2 incr a
(integer) 1

-r 多次执行命令,-i 间隔多久执行

-r <count> and -i <delay>
redis-cli -r 5 incr foo

CSV output

$ redis-cli lpush mylist a b c d
(integer) 4
$ redis-cli --csv lrange mylist 0 -1
"d","c","b","a"

文本操作

$ cat /data/tmp/commands.txt
set foo 100
incr foo
append foo xxx
get foo
$ cat /data/tmp/commands.txt | redis-cli
OK
(integer) 101
(integer) 6
"101xxx"

实时信息统计

redis-cli --stat

------- data ------ --------------------- load -------------------- - child -
keys       mem      clients blocked requests            connections
0          991.09K  1       0       1 (+0)              2
0          991.09K  1       0       2 (+1)              2

获取key列表

redis-cli keys "*"
redis-cli --scan | head -10
redis-cli --scan --pattern 'user:*' | wc -l

批量删除Key

redis-cli keys "*" | xargs redis-cli del

redis 主从

  1. 主机和从机实例正常连接,主机在主数据集发生变化发送命令流给从机保持更新。(客户端写,key失效等
  2. 主机和从机因为网络问题或者超时连接断开,从机重连并试图通过部分重同步继续,这意味着从机尝试只是获得在断开期间的丢失那部分命令流。
  3. 当部分重同步无效,从机将会请求全量同步。这将需要一个复杂的过程,主机创建一个全部数据的快照,发送给从机,然后继续发送数据集变化的命令流。

复制的特性

  1. redis 使用异步复制
  2. 一个 master 可以拥有多个 slave
  3. slave 可以接收来自其他 slave 的连接
  4. redis 复制在 master 端是非阻塞的,也就是说在一个或多个slave同步数据的时候,master仍然可以执行查询。
  5. redis 复制在 slave 端也是非阻塞的。
  6. 复制是可扩展的,可以拥有多个只读查询的从机或只是简单为了数据安全
  7. 使用复制可以避免 master 因为需要把全部的数据集写入磁盘而造成的开销。因此可以把 master 中 save 配置项全部注释掉,不让它进行保存,然后配置 slave ,让 slave 保存。

redis数据格式和抽象

  • Binary-safe strings
  • Lists
  • Sets
  • Sorted sets
  • Hashes
  • Bit arrays (or simply bitmaps)
  • HyperLogLogs

Redis Strings

memcached只有strings唯一数据类型

值不能超过512 MB

> set mykey somevalue
OK
> get mykey
"somevalue"
> set mykey newval nx
(nil)
> set mykey newval xx
OK

多个值

> mset a 10 b 20 c 30
OK
> mget a b c
1) "10"
2) "20"
3) "30"

判断存在

> set mykey hello
OK
> exists mykey
(integer) 1
> del mykey
(integer) 1
> exists mykey
(integer) 0

del返回1或0

> set mykey x
OK
> type mykey
string
> del mykey
(integer) 1
> type mykey
none

Redis expires,key有限制时间存活

> set key some-value
OK
> expire key 5
(integer) 1
> get key (immediately)
"some-value"
> get key (after some time)
(nil)

Redis Lists

lpush在左边加入新值,rpush在右边加入新值

> rpush mylist A
(integer) 1
> rpush mylist B
(integer) 2
> lpush mylist first
(integer) 3
> lrange mylist 0 -1
1) "first"
2) "A"
3) "B"

lrange取出范围值

> rpush mylist 1 2 3 4 5 "foo bar"
(integer) 9
> lrange mylist 0 -1
1) "first"
2) "A"
3) "B"
4) "1"
5) "2"
6) "3"
7) "4"
8) "5"
9) "foo bar"
> rpush mylist a b c
(integer) 3
> rpop mylist
"c"
> rpop mylist
"b"
> rpop mylist
"a"

清空list

ltrim mylist 1 0  或者 del mylist
lrange mylist 0 -1
(empty list or set)
> rpush mylist 1 2 3 4 5
(integer) 5
> ltrim mylist 0 2
OK
> lrange mylist 0 -1
1) "1"
2) "2"
3) "3"

队列

LPUSH插入值,RPOP取出值

Redis Hashes

> hmset user:1000 username antirez birthyear 1977 verified 1
OK
> hget user:1000 username
"antirez"
> hget user:1000 birthyear
"1977"
> hgetall user:1000
1) "username"
2) "antirez"
3) "birthyear"
4) "1977"
5) "verified"
6) "1"
> hmget user:1000 username birthyear
1) "antirez"
2) "1977"
> hincrby user:1000 birthyear 10
(integer) 1987

Redis Sets

Set是无序Strings的集合,可以表达对象之间的关系

> sadd myset 1 2 3
(integer) 3
> smembers myset
1. 3
2. 1
3. 2

判断是否存在

> sismember myset 3
(integer) 1
> sismember myset 30
(integer) 0

Redis Sorted sets

> zadd hackers 1940 "Alan Kay"
(integer) 1
> zadd hackers 1957 "Sophie Wilson"
(integer) 1
> zadd hackers 1953 "Richard Stallman"
(integer) 1
> zadd hackers 1949 "Anita Borg"
(integer) 1
> zadd hackers 1965 "Yukihiro Matsumoto"
(integer) 1
> zadd hackers 1914 "Hedy Lamarr"
(integer) 1
> zadd hackers 1916 "Claude Shannon"
(integer) 1
> zadd hackers 1969 "Linus Torvalds"
(integer) 1
> zadd hackers 1912 "Alan Turing"
(integer) 1
> zrange hackers 0 -1
1) "Alan Turing"
2) "Hedy Lamarr"
3) "Claude Shannon"
4) "Alan Kay"
5) "Anita Borg"
6) "Richard Stallman"
7) "Sophie Wilson"
8) "Yukihiro Matsumoto"
9) "Linus Torvalds"
> zrevrange hackers 0 -1
1) "Linus Torvalds"
2) "Yukihiro Matsumoto"
3) "Sophie Wilson"
4) "Richard Stallman"
5) "Anita Borg"
6) "Alan Kay"
7) "Claude Shannon"
8) "Hedy Lamarr"
9) "Alan Turing"
> zrange hackers 0 -1 withscores
1) "Alan Turing"
2) "1912"
3) "Hedy Lamarr"
4) "1914"
5) "Claude Shannon"
6) "1916"
7) "Alan Kay"
8) "1940"
9) "Anita Borg"
10) "1949"
11) "Richard Stallman"
12) "1953"
13) "Sophie Wilson"
14) "1957"
15) "Yukihiro Matsumoto"
16) "1965"
17) "Linus Torvalds"
18) "1969"

> zrangebyscore hackers -inf 1950
1) "Alan Turing"
2) "Hedy Lamarr"
3) "Claude Shannon"
4) "Alan Kay"
5) "Anita Borg"

0 and -1从index 0 到最后的index -1的范围

Bitmaps

> setbit key 10 1
(integer) 1
> getbit key 10
(integer) 1
> getbit key 11
(integer) 0

HyperLogLogs

用于统计唯一事件

> pfadd hll a b c d
(integer) 1
> pfcount hll
(integer) 4

php操作redis

phpredis

brew install php70-redis
web64