您正在查看: Redis 分类下的文章

Redis学习笔记 数据类型操作

redis的数据类型:

[ ~/redis-2.0.0]$ ./redis-cli -h

usage: redis-cli [-h host] [-p port] [-a authpw] [-r repeat_times] [-n db_num] [-i] cmd arg1 arg2
arg3 ... argN

usage: echo "argN" | redis-cli -c [-h host] [-p port] [-a authpw] [-r repeat_times] [-n db_num] cmd
arg1 arg2 ... arg(N-1)

Redis根据5种不同的数据类型来操作数据对象:

操作String类型的值:

Command Parameters Description

SET key value Set a key to a string value

GET key Return the string value of the key

GETSET key value Set a key to a string returning the old value of the
key

MGET key1 key2 ... keyN Multi-get, return the strings values of the keys

SETNX key value Set a key to a string value if the key does not exist

SETEX key time value Set+Expire combo command

MSET key1 value1 key2 value2 ... keyN valueN Set multiple keys to multiple
values in a single atomic operation

MSETNX key1 value1 key2 value2 ... keyN valueN Set multiple keys to multiple
values in a single atomic operation if none of the keys already exist

INCR key Increment the integer value of key

INCRBY key integer Increment the integer value of key by integer

DECR key Decrement the integer value of key

DECRBY key integer Decrement the integer value of key by integer

APPEND key value Append the specified string to the string stored at
key

SUBSTR key start end Return a substring of a larger string

操作方法:

SET操作

[ ~/redis-2.0.0]$ ./redis-cli -n 0 set uid_001 Redis.C

OK

[ ~/redis-2.0.0]$ ./redis-cli -n 0 set uid_002 Falcon

OK

表示向数据库0中插入字符串key为uid_001,value为Redis.C的字符串

GET操作

[ ~/redis-2.0.0]$ ./redis-cli -n 0 get uid_001

"Redis.C"

[ ~/redis-2.0.0]$ ./redis-cli get uid_001

"Redis.C"

表示获取数据库为0,key为uid_001的字符串,因为在不指定数据编号的情况下,默认连
接的是0数据库,所以可以省略-n参数

GETSET操作

[ ~/redis-2.0.0]$ ./redis-cli getset uid_002 "falcom520@gmail.com"

"Falcon"

[ ~/redis-2.0.0]$ ./redis-cli get uid_002

falcom520@gmail.com

表示返回指定key的原始值,并指定一个新值给他

MGET操作

[ ~/redis-2.0.0]$ ./redis-cli mget uid_001 uid_002

  1. "Redis.C"

  2. "falcom520@gmail.com"

表示获取多个key的值

SETNX操作

[ ~/redis-2.0.0]$ ./redis-cli setnx uid_001_email "falcom520@126.com"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli get uid_001_email

"falcom520@126.com"

[ ~/redis-2.0.0]$ ./redis-cli setnx uid_001_email "falcom520@126.com"

(integer) 0

表示当一个指定的key不存在时,设置这个key指定的value,如果存在,则设置不成功

SETEX操作

[ ~/redis-2.0.0]$ ./redis-cli setex uid_001_msn 5 "falcom520@126.com"

OK

[ ~/redis-2.0.0]$ ./redis-cli get uid_001_msn

"falcom520@126.com"

[ ~/redis-2.0.0]$ ./redis-cli get uid_001_msn

(nil)

表示设置一个key指定的value保存5秒后失效,设置key/value的有效期

MSET操作

[ ~/redis-2.0.0]$ ./redis-cli mset uid0001 "0001" uid0002 "0002"
uid0003 "0003"

OK

[ ~/redis-2.0.0]$ ./redis-cli mget uid0001 uid0002 uid0003

  1. "0001"

  2. "0002"

  3. "0003"

表示多键值对的数据保存,在保证原子操作性的情况下

MSETNX操作

[ ~/redis-2.0.0]$ ./redis-cli msetnx uid0003 "0003" uid0004 "0004"
uid0005 "0005"

(integer) 0

[ ~/redis-2.0.0]$ ./redis-cli msetnx uid0004 "0004" uid0005 "0005"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli mget uid0001 uid0002 uid0003 uid0004
uid0005

  1. "0001"

  2. "0002"

  3. "0003"

  4. "0004"

  5. "0005"

表示在单原子操作性的情况下,keys不存在的前提下插入多个values值,如果存在其中一个
keys则插入失败

INCR操作

[ ~/redis-2.0.0]$ ./redis-cli incr uid

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli incr uid

(integer) 2

[ ~/redis-2.0.0]$ ./redis-cli incr uid

(integer) 3

表示对给定key的value进行递增的操作

INCRBY操作

[ ~/redis-2.0.0]$ ./redis-cli incrby uid 5

(integer) 8

[ ~/redis-2.0.0]$ ./redis-cli incrby uid 5

(integer) 13

表示对给定key的value进行指定步长的递增操作

DECR操作

[ ~/redis-2.0.0]$ ./redis-cli decr uid

(integer) 12

[ ~/redis-2.0.0]$ ./redis-cli decr uid

(integer) 11

表示对给定的key的value进行递减操作

DECRBY操作

[ ~/redis-2.0.0]$ ./redis-cli decrby uid 3

(integer) 8

[ ~/redis-2.0.0]$ ./redis-cli decrby uid 3

(integer) 5

表示对给定key的value做指定步长的递减操作

APPEND操作

[ ~/redis-2.0.0]$ ./redis-cli append content "01234"

(integer) 5

[ ~/redis-2.0.0]$ ./redis-cli get content

"01234"

[ ~/redis-2.0.0]$ ./redis-cli append content "56789"

(integer) 10

[ ~/redis-2.0.0]$ ./redis-cli get content

"0123456789"

表示追加一个value到指定的key中,如果key不存在,则新建key

SUBSTR操作

[ ~/redis-2.0.0]$ ./redis-cli substr content 0 4

"01234"

[ ~/redis-2.0.0]$ ./redis-cli substr content 5 10

"56789"

表示返回指定key的value的部分字符串

操作lists类型的值:(列表)

Command Parameters Description

RPUSH key value Append an element to the tail of the List value at key

LPUSH key value Append an element to the head of the List value at key

LLEN key Return the length of the List value at key

LRANGE key start end Return a range of elements from the List at key

LTRIM key start end Trim the list at key to the specified range of elements

LINDEX key index Return the element at index position from the List at key

LSET key index value Set a new value as the element at index position of the
List at key

LREM key count value Remove the first-N, last-N, or all the elements matching
value from the List at key

LPOP key Return and remove (atomically) the first element of the
List at key

RPOP key Return and remove (atomically) the last element of the
List at key

BLPOP key1 key2 ... keyN timeout Blocking LPOP

BRPOP key1 key2 ... keyN timeout Blocking RPOP

RPOPLPUSH srckey dstkey Return and remove (atomically) the last element of the
source List stored at srckey and push the same element to the destination List stored at dstkey

RPUSH操作

[ ~/redis-2.0.0]$ ./redis-cli rpush list_001 0000001

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli rpush list_001 0000002

(integer) 2

[ ~/redis-2.0.0]$ ./redis-cli rpush list_001 0000003

(integer) 3

[ ~/redis-2.0.0]$ ./redis-cli lrange list_001 0 3

  1. "0000001"

  2. "0000002"

  3. "0000003"

表示向指定key的list的后面(右边)追加指定的value

LPUSH操作

[ ~/redis-2.0.0]$ ./redis-cli lpush list_001 000099

(integer) 4

[ ~/redis-2.0.0]$ ./redis-cli lpush list_001 000098

(integer) 5

[ ~/redis-2.0.0]$ ./redis-cli lpush list_001 000097

(integer) 6

[ ~/redis-2.0.0]$ ./redis-cli lrange list_001 0 8

  1. "000097"

  2. "000098"

  3. "000099"

  4. "0000001"

  5. "0000002"

  6. "0000003"

表示向指定key的list的前面(左边)追加指定的value

LLEN操作

[ ~/redis-2.0.0]$ ./redis-cli llen list_001

(integer) 6

表示返回指定key list的长度

LRANGE操作

[ ~/redis-2.0.0]$ ./redis-cli lrange list_001 2 4

  1. "000099"

  2. "0000001"

  3. "0000002"

表示返回指定key list里面的位置的范围value

LTRIM操作

[ ~/redis-2.0.0]$ ./redis-cli ltrim list_001 0 2

OK

[ ~/redis-2.0.0]$ ./redis-cli lrange list_001 0 4

  1. "000097"

  2. "000098"

  3. "000099"

表示删除指定key 的值范围以外的数据

LINDEX操作

[ ~/redis-2.0.0]$ ./redis-cli lrange list_001 0 9

  1. "000097"

  2. "000098"

  3. "000099"

[ ~/redis-2.0.0]$ ./redis-cli lindex list_001 2

"000099"

[ ~/redis-2.0.0]$ ./redis-cli lindex list_001 1

"000098"

表示返回指定key list里面索引的值

LSET操作

[ ~/redis-2.0.0]$ ./redis-cli lrange list_001 0 9

  1. "000097"

  2. "000098"

  3. "000099"

[ ~/redis-2.0.0]$ ./redis-cli lset list_001 0 "100097"

OK

[ ~/redis-2.0.0]$ ./redis-cli lrange list_001 0 9

  1. "100097"

  2. "000098"

  3. "000099"

表示给指定key 的list里面指定索引的值修改为一个新值

LREM操作

[ ~/redis-2.0.0]$ ./redis-cli lpush list_001 000099

(integer) 4

[ ~/redis-2.0.0]$ ./redis-cli lpush list_001 000099

(integer) 5

[ ~/redis-2.0.0]$ ./redis-cli lpush list_001 000099

(integer) 6

[ ~/redis-2.0.0]$ ./redis-cli lrange list_001 0 9

  1. "000099"

  2. "000099"

  3. "000099"

  4. "100097"

  5. "000098"

  6. "000099"

[ ~/redis-2.0.0]$ ./redis-cli lrem list_001 2 000099

(integer) 2

[ ~/redis-2.0.0]$ ./redis-cli lrange list_001 0 9

  1. "000099"

  2. "100097"

  3. "000098"

  4. "000099"

表示删除指定key 的list里面值为value的指定个数

LPOP操作

[ ~/redis-2.0.0]$ ./redis-cli lrange list_001 0 9

  1. "000099"

  2. "100097"

  3. "000098"

  4. "000099"

[ ~/redis-2.0.0]$ ./redis-cli lpop list_001

"000099"

[ ~/redis-2.0.0]$ ./redis-cli lrange list_001 0 9

  1. "100097"

  2. "000098"

  3. "000099"

表示删除指定key的list里面最前面(左边)的值,并返回该值

RPOP操作

[ ~/redis-2.0.0]$ ./redis-cli lrange list_001 0 9

  1. "100097"

  2. "000098"

  3. "000099"

[ ~/redis-2.0.0]$ ./redis-cli rpop list_001

"000099"

[ ~/redis-2.0.0]$ ./redis-cli lrange list_001 0 9

  1. "100097"

  2. "000098"

表示删除指定key的list里面最后面(右边)的值,并返回该值

BLPOP和BRPOP操作

在阻塞的模式下执行LPOP和RPOP操作

RPOPLPUSH操作

[ ~/redis-2.0.0]$ ./redis-cli lrange list_001 0 9

  1. "100097"

  2. "000098"

[ ~/redis-2.0.0]$ ./redis-cli rpoplpush list_001 list_999

"000098"

[ ~/redis-2.0.0]$ ./redis-cli lrange list_001 0 9

  1. "100097"

[ ~/redis-2.0.0]$ ./redis-cli lrange list_999 0 6

  1. "000098"

表示将原key的list后面(右边)的值删掉,并保存到指定的目的key中,并返回该值

操作sets类型的值:(sets集合)

Command Parameters Description

SADD key member Add the specified member to the Set value at key

SREM key member Remove the specified member from the Set value at key

SPOP key Remove and return (pop) a random element from the Set

value at key

SMOVE srckey dstkey member Move the specified member from one Set to another

atomically

SCARD key Return the number of elements (the cardinality) of the Set at

key

SISMEMBER key member Test if the specified value is a member of the Set at key

SINTER key1 key2 ... keyN Return the intersection between the Sets stored at key1,

key2, ..., keyN

SINTERSTORE dstkey key1 key2 ... keyN Compute the intersection between the Sets stored at key1,
key2, ..., keyN, and store the resulting Set at dstkey

SUNION key1 key2 ... keyN Return the union between the Sets stored at key1, key2, ...,

keyN

SUNIONSTORE dstkey key1 key2 ... keyN Compute the union between the Sets stored at

key1, key2, ..., keyN, and store the resulting Set at dstkey

SDIFF key1 key2 ... keyN Return the difference between the Set stored at key1 and all
the Sets key2, ..., keyN

SDIFFSTORE dstkey key1 key2 ... keyN Compute the difference between the Set key1

and all the Sets key2, ..., keyN, and store the resulting Set at
dstkey

SMEMBERS key Return all the members of the Set value at key

SRANDMEMBER key Return a random member of the Set value at key

SADD操作

[ ~/redis-2.0.0]$ ./redis-cli sadd s_001 "Redis.C"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli sadd s_001 "Falcon"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli smembers s_001

  1. "Falcon"

  2. "Redis.C"

表示向指定key的集合中添加成员

SREM操作

[ ~/redis-2.0.0]$ ./redis-cli smembers s_001

  1. "Falcon"

  2. "Redis.C"

[ ~/redis-2.0.0]$ ./redis-cli srem s_001 Falcon

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli smembers s_001

  1. "Redis.C"

表示删除指定key的指定Value成员值

SPOP操作

[ ~/redis-2.0.0]$ ./redis-cli smembers s_001

  1. "Redis.C"

[ ~/redis-2.0.0]$ ./redis-cli sadd s_001 "www.linuxtone.org"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli sadd s_001 "bbs.linuxtone.org"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli sadd s_001 "uc.linuxtone.org"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli smembers s_001

  1. "www.linuxtone.org"

  2. "Redis.C"

  3. "bbs.linuxtone.org"

  4. "uc.linuxtone.org"

[ ~/redis-2.0.0]$ ./redis-cli spop s_001

"www.linuxtone.org"

[ ~/redis-2.0.0]$ ./redis-cli smembers s_001

  1. "Redis.C"

  2. "bbs.linuxtone.org"

  3. "uc.linuxtone.org"

表示从指定key的set集中随机删除一个成员value并返回

SMOVE操作

[ ~/redis-2.0.0]$ ./redis-cli smembers s_001

  1. "Redis.C"

  2. "bbs.linuxtone.org"

[ ~/redis-2.0.0]$ ./redis-cli smove s_001 s_002 bbs.linuxtone.org

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli smembers s_001

  1. "Redis.C"

[ ~/redis-2.0.0]$ ./redis-cli smembers s_002

  1. "bbs.linuxtone.org"

表示从一个指定的key中移动一个指定的value 成员到另一个指定的key中,这些操作是具
有原子性的

SCARD操作

[ ~/redis-2.0.0]$ ./redis-cli scard s_001

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli scard s_002

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli smembers s_001

  1. "Redis.C"

表示返回指定key 的set集的value成员个数

SISMEMBER操作

[ ~/redis-2.0.0]$ ./redis-cli smembers s_001

  1. "Redis.C"

[ ~/redis-2.0.0]$ ./redis-cli sismember s_001 Falcon

(integer) 0

[ ~/redis-2.0.0]$ ./redis-cli sismember s_001 Redis.C

(integer) 1

表示判断指定的key的成员是否存在于sets集中

SINTER操作

[ ~/redis-2.0.0]$ ./redis-cli sadd s_001 "000001"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli sadd s_001 "000002"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli smembers s_001

  1. "Redis.C"

  2. "000001"

  3. "000002"

[ ~/redis-2.0.0]$ ./redis-cli smembers s_002

  1. "bbs.linuxtone.org"

[ ~/redis-2.0.0]$ ./redis-cli sadd s_002 "000001"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli sadd s_002 "000002"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli smembers s_002

  1. "000001"

  2. "bbs.linuxtone.org"

  3. "000002"

[ ~/redis-2.0.0]$ ./redis-cli sinter s_001 s_002

  1. "000001"

  2. "000002"

表示对指定的key的sets集执行交集操作,返回指定sets集合中相同的value成员

SINTERSTORE操作

[ ~/redis-2.0.0]$ ./redis-cli sadd s_001 "000003"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli sadd s_001 "00000099"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli smembers s_001

  1. "000003"

  2. "Redis.C"

  3. "000001"

  4. "000002"

  5. "00000099"

[ ~/redis-2.0.0]$ ./redis-cli sadd s_002 "000003"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli sadd s_002 "00000099"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli smembers s_002

  1. "000003"

  2. "000001"

  3. "bbs.linuxtone.org"

  4. "000002"

  5. "00000099"

[ ~/redis-2.0.0]$ ./redis-cli smembers s_002

  1. "000003"

  2. "000001"

  3. "bbs.linuxtone.org"

  4. "000002"

  5. "00000099"

[ ~/redis-2.0.0]$ ./redis-cli sinterstore s_003 s_001 s_002

(integer) 4

[ ~/redis-2.0.0]$ ./redis-cli smembers s_003

  1. "000003"

  2. "000001"

  3. "00000099"

  4. "000002"

表示将指定的key的sets集做交集,并将结果保存到指定的key中

SUNION操作

[ ~/redis-2.0.0]$ ./redis-cli smembers s_001

  1. "000003"

  2. "Redis.C"

  3. "000001"

  4. "000002"

  5. "00000099"

[ ~/redis-2.0.0]$ ./redis-cli smembers s_002

  1. "000003"

  2. "bbs.linuxtone.org"

  3. "000001"

  4. "000002"

  5. "00000099"

[ ~/redis-2.0.0]$ ./redis-cli sunion s_001 s_002

  1. "000003"

  2. "Redis.C"

  3. "000001"

  4. "bbs.linuxtone.org"

  5. "000002"

  6. "00000099"

表示对指定的key的sets集合做并集

SUNIONSTORE操作

[ ~/redis-2.0.0]$ ./redis-cli smembers s_001

  1. "000003"

  2. "Redis.C"

  3. "000001"

  4. "000002"

  5. "00000099"

[ ~/redis-2.0.0]$ ./redis-cli smembers s_002

  1. "000003"

  2. "bbs.linuxtone.org"

  3. "000001"

  4. "000002"

  5. "00000099"

[ ~/redis-2.0.0]$ ./redis-cli sunionstore s_004 s_001 s_002

(integer) 6

[ ~/redis-2.0.0]$ ./redis-cli smembers s_004

  1. "000003"

  2. "Redis.C"

  3. "000001"

  4. "bbs.linuxtone.org"

  5. "000002"

  6. "00000099"

表示对指定的key的sets集做并集,并将结果保存到指定的key中

SDIFF操作

[ ~/redis-2.0.0]$ ./redis-cli smembers s_001

  1. "000003"

  2. "Redis.C"

  3. "000001"

  4. "000002"

  5. "00000099"

[ ~/redis-2.0.0]$ ./redis-cli smembers s_002

  1. "000003"

  2. "bbs.linuxtone.org"

  3. "000001"

  4. "000002"

  5. "00000099"

[ ~/redis-2.0.0]$ ./redis-cli sdiff s_001 s_002 s_003 s_004

(empty list or set)

[ ~/redis-2.0.0]$ ./redis-cli sdiff s_001 s_002

  1. "Redis.C"

[ ~/redis-2.0.0]$ ./redis-cli sdiff s_001 s_003

  1. "Redis.C"

表示对给定的第一个key的sets集合与其他的key的sets集合的value进行对比,并返回不
同的value的成员

SDIFFSTORE操作

Sdiffstore与sdiff操作一样,只是把不同的sets集合成员保存到一个给定的key的sets集合

SMEMBERS操作

[ ~/redis-2.0.0]$ ./redis-cli smembers s_004

  1. "000003"

  2. "Redis.C"

  3. "000001"

  4. "bbs.linuxtone.org"

  5. "000002"

  6. "00000099"

[ ~/redis-2.0.0]$ ./redis-cli smembers s_003

  1. "000003"

  2. "000001"

  3. "00000099"

  4. "000002"

表示返回指定key的所有sets集合的成员

SRANDMEMBER操作

[ ~/redis-2.0.0]$ ./redis-cli smembers s_003

  1. "000003"

  2. "000001"

  3. "00000099"

  4. "000002"

[ ~/redis-2.0.0]$ ./redis-cli srandmember s_003

"000001"

[ ~/redis-2.0.0]$ ./redis-cli srandmember s_003

"000002"

[ ~/redis-2.0.0]$ ./redis-cli srandmember s_003

"000002"

表示返回一个给定key的sets集合中随机的一个成员

操作zsets类型的值:(排序后的sets集合)

Command Parameters Description

ZADD key score member Add the specified member to the Sorted Set value at key

or update the score if it already exist

ZREM key member Remove the specified member from the Sorted Set value

at key

ZINCRBY key increment member If the member already exists increment its score by

increment, otherwise add the member setting increment

as score

ZRANK key member Return the rank (or index) or member in the sorted set at

key, with scores being ordered from low to high

ZREVRANK key member Return the rank (or index) or member in the sorted set at

key, with scores being ordered from high to low

ZRANGE key start end Return a range of elements from the sorted set at key

ZREVRANGE key start end Return a range of elements from the sorted set at key,

exactly like ZRANGE, but the sorted set is ordered in

traversed in reverse order, from the greatest to the

smallest score

ZRANGEBYSCORE key min max Return all the elements with score >= min and score <=

max (a range query) from the sorted set

ZCOUNT key min max Return the number of elements with score >= min and

score <= max in the sorted set

ZCARD key Return the cardinality (number of elements) of the sorted

set at key

ZSCORE key element Return the score associated with the specified element of

the sorted set at key

ZREMRANGEBYRANK key min max Remove all the elements with rank >= min and rank <=

max from the sorted set

ZREMRANGEBYSCORE key min max Remove all the elements with score >= min and score <=

max from the sorted set

ZUNIONSTORE / ZINTERSTORE dstkey N key1 ... keyN WEIGHTS w1 ... wN AGGREGATE
SUM|MIN|MAX Perform a union or intersection over a number of sorted sets with optional
weight and aggregate

ZADD操作

[ ~/redis-2.0.0]$ ./redis-cli zadd z_001 1 "Redis.C"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli zadd z_001 1 "Falcon"

(integer) 0

[ ~/redis-2.0.0]$ ./redis-cli zadd z_001 1 "LinuxTone"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli zrange z_001 0 4

  1. "Falcon"

  2. "Redis.C"

  3. "LinuxTone"

表示通过给定的积分顺序插入成员值到指定的key的顺序sets集合中,如果成员存在则插入
失败

ZREM操作

[ ~/redis-2.0.0]$ ./redis-cli zrange z_001 0 4

  1. "Falcon"

  2. "Redis.C"

  3. "LinuxTone"

[ ~/redis-2.0.0]$ ./redis-cli zrem z_001 Falcon

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli zrange z_001 0 4

  1. "Redis.C"

  2. "LinuxTone"

表示从给定的key的顺序sets集合中删除指定的成员

ZINCRBY操作

[ ~/redis-2.0.0]$ ./redis-cli zrange z_001 0 4

  1. "Redis.C"

  2. "LinuxTone"

[ ~/redis-2.0.0]$ ./redis-cli zincrby z_001 2 Redis.C

"3"

[ ~/redis-2.0.0]$ ./redis-cli zrange z_001 0 8

  1. "LinuxTone"

  2. "Redis.C"

表示给指定的key的成员的排序积分进行给定的递增值的递增,如果积分为零,则将递增值
作为积分排序

ZRANK操作

[ ~/redis-2.0.0]$ ./redis-cli zrange z_001 0 8

  1. "LinuxTone"

  2. "0"

  3. "Redis.C"

[ ~/redis-2.0.0]$ ./redis-cli zrank z_001 Redis.C

(integer) 2

表示获取当前指定key的成员在排序sets集合中的排名,从0开始计数(正序)

ZREVRANK操作

与ZRANK一样,从0开始计数(倒序)

ZRANGE操作

[ ~/redis-2.0.0]$ ./redis-cli zrange z_001 0 8

  1. "LinuxTone"

  2. "0"

  3. "Redis.C"

表示通过开始值和结束值来获取指定key的排序sets集合中的成员范围

ZREVRANGE操作

[ ~/redis-2.0.0]$ ./redis-cli zrange z_001 0 2

  1. "LinuxTone"

  2. "0"

  3. "Redis.C"

[ ~/redis-2.0.0]$ ./redis-cli zrange z_001 0 1

  1. "LinuxTone"

  2. "0"

[ ~/redis-2.0.0]$ ./redis-cli zrevrange z_001 0 1

  1. "Redis.C"

  2. "0"

表示通过对指定key的排序sets集合倒序后在获取指定范围的成员集

ZRANGEBYSCORE操作

[ ~/redis-2.0.0]$ ./redis-cli zadd z_001 1 "linuxtone.org"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli zadd z_001 2 "qq.com"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli zadd z_001 3 "google.com"

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli zrange z_001 0 10

  1. "linuxtone.org"

  2. "LinuxTone"

  3. "qq.com"

  4. "google.com"

  5. "0"

  6. "Redis.C"

[ ~/redis-2.0.0]$ ./redis-cli zrangebyscore z_001 2 4

  1. "LinuxTone"

  2. "qq.com"

  3. "google.com"

表示获取指定key的积分范围的排序sets集合的成员

ZCOUNT操作

[ ~/redis-2.0.0]$ ./redis-cli zrange z_001 0 10

  1. "linuxtone.org"

  2. "LinuxTone"

  3. "qq.com"

  4. "google.com"

  5. "0"

  6. "Redis.C"

[ ~/redis-2.0.0]$ ./redis-cli zcount z_001 2 4

(integer) 3

表示获取指定key的积分范围的排序sets集合的成员数量

ZCARD操作

[ ~/redis-2.0.0]$ ./redis-cli zrange z_001 0 10

  1. "linuxtone.org"

  2. "LinuxTone"

  3. "qq.com"

  4. "google.com"

  5. "0"

  6. "Redis.C"

[ ~/redis-2.0.0]$ ./redis-cli zcard z_001

(integer) 6

表示获取指定key的排序sets集合的成员基数

ZSCORE操作

[ ~/redis-2.0.0]$ ./redis-cli zrange z_001 0 10

  1. "linuxtone.org"

  2. "LinuxTone"

  3. "qq.com"

  4. "google.com"

  5. "0"

  6. "Redis.C"

[ ~/redis-2.0.0]$ ./redis-cli zscore z_001 0

"5"

[ ~/redis-2.0.0]$ ./redis-cli zscore z_001 qq.com

"2"

表示获取指定key的排序sets集合中成员的积分

ZREMRANGEBYRANK操作

[ ~/redis-2.0.0]$ ./redis-cli zrange z_001 0 10

  1. "linuxtone.org"

  2. "LinuxTone"

  3. "qq.com"

  4. "google.com"

  5. "0"

  6. "Redis.C"

[ ~/redis-2.0.0]$ ./redis-cli zremrangebyrank z_001 4 5

(integer) 2

[ ~/redis-2.0.0]$ ./redis-cli zrange z_001 0 10

  1. "linuxtone.org"

  2. "LinuxTone"

  3. "qq.com"

  4. "google.com"

表示删除指定key的排序sets集合中成员的排名范围的成员(通过排名范围删除成员)

ZREMRANGEBYSCORE操作

[ ~/redis-2.0.0]$ ./redis-cli zrange z_001 0 10

  1. "linuxtone.org"

  2. "LinuxTone"

  3. "qq.com"

  4. "google.com"

[ ~/redis-2.0.0]$ ./redis-cli zscore z_001 linuxtone.org

"1"

[ ~/redis-2.0.0]$ ./redis-cli zscore z_001 LinuxTone

"2"

[ ~/redis-2.0.0]$ ./redis-cli zscore z_001 qq.com

"2"

[ ~/redis-2.0.0]$ ./redis-cli zscore z_001 google.com

"3"

[ ~/redis-2.0.0]$ ./redis-cli zremrangebyscore z_001 2 2

(integer) 2

[ ~/redis-2.0.0]$ ./redis-cli zrange z_001 0 10

  1. "linuxtone.org"

  2. "google.com"

表示删除通过指定key的排序sets集合中给定范围积分的成员(通过积分范围删除成员)

ZUNIONSTORE/ZINTERSTORE操作

表示通过指定的keys做交际或者并集,并将结果保存到指定的结果集中

操作hash类型的值:

Command Parameters Description

HSET key field value Set the hash field to the specified value. Creates the

hash if needed.

HGET key field Retrieve the value of the specified hash field.

HMGET key field1 ... fieldN Get the hash values associated to the specified

fields.

HMSET key field1 value1 ... fieldN valueN Set the hash fields to their respective

values.

HINCRBY key field integer Increment the integer value of the hash at key on

field with integer.

HEXISTS key field Test for existence of a specified field in a hash

HDEL key field Remove the specified field from a hash

HLEN key Return the number of items in a hash.

HKEYS key Return all the fields in a hash.

HVALS key Return all the values in a hash.

HGETALL key Return all the fields and associated values in a hash.

HSET操作

[ ~/redis-2.0.0]$ ./redis-cli hset h_uid uid001 'Redis.C'

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli hset h_uid uid002 'NetSeek'

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli hset h_uid uid003 'LinuxTone'

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli hkeys h_uid

  1. "uid001"

  2. "uid002"

  3. "uid003"

[ ~/redis-2.0.0]$ ./redis-cli hvals h_uid

  1. "Redis.C"

  2. "NetSeek"

  3. "LinuxTone"

表示给指定的hash字段设置值,如果不存在则创建

HGET操作

[ ~/redis-2.0.0]$ ./redis-cli hget h_uid uid001

"Redis.C"

[ ~/redis-2.0.0]$ ./redis-cli hget h_uid uid002

"NetSeek"

[ ~/redis-2.0.0]$ ./redis-cli hget h_uid uid003

"LinuxTone"

表示获取指定hash字段的值

HMGET操作

[ ~/redis-2.0.0]$ ./redis-cli hmget h_uid uid001 uid002 uid003

  1. "Redis.C"

  2. "NetSeek"

  3. "LinuxTone"

表示批量获取指定hash字段的值

HMSET操作

[ ~/redis-2.0.0]$ ./redis-cli hmset h_uid uid004 'linuxtone.org' uid005
'qq.com'

OK

[ ~/redis-2.0.0]$ ./redis-cli hkeys h_uid

  1. "uid001"

  2. "uid002"

  3. "uid003"

  4. "uid004"

  5. "uid005"

[ ~/redis-2.0.0]$ ./redis-cli hvals h_uid

  1. "Redis.C"

  2. "NetSeek"

  3. "LinuxTone"

  4. "linuxtone.org"

  5. "qq.com"

表示批量设置hash字段的值

HINCRBY操作

[ ~/redis-2.0.0]$ ./redis-cli hincrby h_uid_incr uid 1

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli hincrby h_uid_incr uid 1

(integer) 2

[ ~/redis-2.0.0]$ ./redis-cli hincrby h_uid_incr uid 3

(integer) 5

[ ~/redis-2.0.0]$ ./redis-cli hvals h_uid_incr

  1. "5"

表示对指定的hash字段的值进行递增操作

HEXISTS操作

[ ~/redis-2.0.0]$ ./redis-cli hexists h_uid uid001

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli hexists h_uid uid0011

(integer) 0

表示判断指定的hash字段是否存在

HDEL操作

[ ~/redis-2.0.0]$ ./redis-cli hset h_uid uid 1

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli hvals h_uid

  1. "Redis.C"

  2. "NetSeek"

  3. "LinuxTone"

  4. "linuxtone.org"

  5. "qq.com"

  6. "1"

[ ~/redis-2.0.0]$ ./redis-cli hdel h_uid uid

(integer) 1

[ ~/redis-2.0.0]$ ./redis-cli hvals h_uid

  1. "Redis.C"

  2. "NetSeek"

  3. "LinuxTone"

  4. "linuxtone.org"

  5. "qq.com"

表示通过指定的hash字段删除hash值

HLEN操作

[ ~/redis-2.0.0]$ ./redis-cli hlen h_uid

(integer) 5

表示返回hash长度

LKEYS操作

[ ~/redis-2.0.0]$ ./redis-cli hkeys h_uid

  1. "uid001"

  2. "uid002"

  3. "uid003"

  4. "uid004"

  5. "uid005"

表示返回指定hash的所有key

HVALS操作

[ ~/redis-2.0.0]$ ./redis-cli hvals h_uid

  1. "Redis.C"

  2. "NetSeek"

  3. "LinuxTone"

  4. "linuxtone.org"

  5. "qq.com"

表示返回指定hash的所有value

HGETALL操作

[ ~/redis-2.0.0]$ ./redis-cli hgetall h_uid

  1. "uid001"

  2. "Redis.C"

  3. "uid002"

  4. "NetSeek"

  5. "uid003"

  6. "LinuxTone"

  7. "uid004"

  8. "linuxtone.org"

  9. "uid005"

  10. "qq.com"

表示返回指定hash的所有字段及关联的值

公共操作命令部分:(不受数据类型的影响)

Command Parameters Description

SORT key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA Sort a

Set or a List accordingly to the specified parameters

数据持久化控制命令

SAVE - Synchronously save the DB on disk

BGSAVE - Asynchronously save the DB on disk

LASTSAVE - Return the UNIX time stamp of the last successfully saving

of the dataset on disk

SHUTDOWN - Synchronously save the DB on disk, then shutdown the

server

BGREWRITEAOF - Rewrite the append only file in background when it gets

too big

远程服务器控制命令

INFO - Provide information and statistics about the server

MONITOR - Dump all the received requests in real time

SLAVEOF - Change the replication settings

CONFIG - Configure a Redis server at runtime

Redis的master/slave复制:

Redis的master/slave数据复制方式可以是一主一从或者是一主多从的方式,Redis在
master是非阻塞模式,也就是说在slave执行数据同步的时候,master是可以接受客户端的
请求的,并不影响同步数据的一致性,然而在slave端是阻塞模式的,slave在同步master
数据时,并不能够响应客户端的查询

Redis的master/slave模式下,master提供数据读写服务,而slave只提供读服务

Redis的master/slave的配置方式是在slave主机的Redis目录下的redis.conf配置文件中
添加:

slaveof master_ip master_port

例如:

我们配置我们的slave为:redis-slave.conf

daemonize yes

pidfile redis-slave.pid

port 6380

timeout 300

loglevel verbose

logfile stdout

databases 16

save 900 1

save 300 10

save 60 10000

rdbcompression yes

dbfilename dump-slave.rdb

dir /home/falcon/redis-2.0.0/

slaveof 127.0.0.1 6379

appendonly no

appendfsync everysec

vm-enabled no

vm-swap-file logs/redis-slave.swap

vm-max-memory 0

vm-page-size 32

vm-pages 134217728

vm-max-threads 4

glueoutputbuf yes

hash-max-zipmap-entries 64

hash-max-zipmap-value 512

activerehashing yes

启动slave:

[ ~/redis-2.0.0]$ ./redis-server redis-slave.conf

查看状态信息:

[ ~/redis-2.0.0]$ ./redis-cli -p 6380 info

redis_version:1.3.17

redis_git_sha1:00000000

redis_git_dirty:0

arch_bits:32

multiplexing_api:epoll

process_id:10772

uptime_in_seconds:249

uptime_in_days:0

connected_clients:2

connected_slaves:0

blocked_clients:0

used_memory:1756868

used_memory_human:1.68M

changes_since_last_save:0

bgsave_in_progress:0

last_save_time:1281654285

bgrewriteaof_in_progress:0

total_connections_received:13

total_commands_processed:9

expired_keys:0

hash_max_zipmap_entries:64

hash_max_zipmap_value:512

pubsub_channels:0

pubsub_patterns:0

vm_enabled:0

role:slave

master_host:127.0.0.1

master_port:6379

master_link_status:up

master_last_io_seconds_ago:248

db0:keys=23,expires=0

[ ~/redis-2.0.0]$ ./redis-cli -p 6379 info

redis_version:1.3.17

redis_git_sha1:00000000

redis_git_dirty:0

arch_bits:32

multiplexing_api:epoll

process_id:7663

uptime_in_seconds:16787

uptime_in_days:0

connected_clients:1

connected_slaves:1

blocked_clients:0

used_memory:1757232

used_memory_human:1.68M

changes_since_last_save:0

bgsave_in_progress:0

last_save_time:1281654286

bgrewriteaof_in_progress:0

total_connections_received:835

total_commands_processed:55953

expired_keys:1

hash_max_zipmap_entries:64

hash_max_zipmap_value:512

pubsub_channels:0

pubsub_patterns:0

vm_enabled:0

role:master

db0:keys=23,expires=0

解释:

1、 红色标注为master/slave端口和运行模式
2、 蓝色标注为master/slave数据是否同步,目前显示为keys 23个,0个过期

扩展思维:

Redis可以做一主一从,也可以做一主多从,更可以做一主一从,在从下面挂从,大家
可以根据需求做这样的试验

Redis学习笔记

  1. 50个并发,请求100000次

  2. 读和写大小为256bytes的字符串

  3. linux2.6 Xeon X3320 2.5GHz的服务器

  4. 通过本机的loopback interface接口上执行

地址:http://code.google.com/p/redis/wiki/Benchmarks

redis的功能:

1、Redis的Sharding:Redis支持客户端的Sharding功能,通过一致性hash算法实现,
当前Redis不支持故障冗余,在集群中不能在线增加或删除Redis

2、Redis的master/slave复制:

  1. 一个master支持多个slave
  2. Slave可以接受其他slave的连接来替代他连接master
  3. 复制在master是非阻塞的,而在slave是阻塞的
  4. 复制被利用来提供可扩展性,在slave端只提供查询功能及数据的冗余

3、Redis的Virtual Memory功能:vm是Redis2.0新增的一个非常稳定和可靠的功能,

vm的引入是为了提高Redis的性能,也就是把很少使用的value保存到disk,而key保存在
内存中。实际上就是如果你有10w的keys在内存中,而只有仅仅10%左右的key经常使用,
那么Redis可以通过开启VM尝试将不经常使用的Value转换到disk上保存

4、Redis的附加档案(AOF)功能:Redis通过配置的策略将数据集保存到aof中,当
Redis挂掉后能够通过aof恢复到挂掉前的状态

Redis的安装及配置:

下载Redis:wget http://redis.googlecode.com/files/redis-2.0.0-rc4.tar.gz

[ ~/src]$ tar xvzf redis-2.0.0-rc4.tar.gz

[ ~/src]$ cd redis-2.0.0-rc4

[ ~/src/redis-2.0.0-rc4]$ make

[ ~/src/redis-2.0.0-rc4]$ mkdir ~/redis-2.0.0

[ ~/src/redis-2.0.0-rc4]$cp redis-server ~/redis-2.0.0

[ ~/src/redis-2.0.0-rc4]$cp redis-benchmark ~/redis-2.0.0

[ ~/src/redis-2.0.0-rc4]$cp redis-cli ~/redis-2.0.0

[ ~/src/redis-2.0.0-rc4]$cp redis.conf ~/redis-2.0.0

[ ~/src/redis-2.0.0-rc4]$ cd ~/redis-2.0.0/

配置redis.conf配置文件:

#是否作为守护进程运行

daemonize yes

#配置pid的存放路径及文件名,默认为当前路径下

pidfile redis.pid

#Redis默认监听端口

port 6379

#客户端闲置多少秒后,断开连接

timeout 300

#日志显示级别

loglevel verbose

#指定日志输出的文件名,也可指定到标准输出端口

logfile stdout

#设置数据库的数量,默认连接的数据库是0,可以通过select N来连接不同的数据库

databases 16

#保存数据到disk的策略

#当有一条Keys数据被改变是,900秒刷新到disk一次

save 900 1

#当有10条Keys数据被改变时,300秒刷新到disk一次

save 300 10

#当有1w条keys数据被改变时,60秒刷新到disk一次

save 60 10000

#当dump .rdb数据库的时候是否压缩数据对象

rdbcompression yes

#dump数据库的数据保存的文件名

dbfilename dump.rdb

#Redis的工作目录

dir /home/falcon/redis-2.0.0/

########### Replication #####################

#Redis的复制配置

slaveof

masterauth <master-password>

############## SECURITY ###########

requirepass foobared

############### LIMITS ##############

#最大客户端连接数

maxclients 128

#最大内存使用率

maxmemory

########## APPEND ONLY MODE #########

#是否开启日志功能

appendonly no

刷新日志到disk的规则

appendfsync always

appendfsync everysec

appendfsync no

################ VIRTUAL MEMORY ###########

#是否开启VM功能

vm-enabled no

vm-enabled yes

vm-swap-file logs/redis.swap

vm-max-memory 0

vm-page-size 32

vm-pages 134217728

vm-max-threads 4

############# ADVANCED CONFIG ###############

glueoutputbuf yes

hash-max-zipmap-entries 64

hash-max-zipmap-value 512

#是否重置Hash表

activerehashing yes

启动Redis

[ ~/redis-2.0.0]$ ./redis-server redis.conf

检测Redis是否启动:

[ ~/redis-2.0.0]$ netstat -an -t

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address Foreign Address State

tcp 0 0 0.0.0.0:10022 0.0.0.0:* LISTEN

tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN

……..

[ ~/redis-2.0.0]$ ps -ef|grep redis-server

falcon 7663 1 0 02:29 ? 00:00:00 ./redis-server redis.conf

Redis提供的原生监控

当前链接的客户端数和连接数

redis-cli --stat查看当前连接的客户端数,连接数等

------- data ------ --------------------- load -------------------- - child -
keys       mem      clients blocked requests            connections
4          1.27M    6       0       17340 (+0)          111
4          1.27M    6       0       17341 (+1)          111
4          1.27M    6       0       17342 (+1)          111
4          1.27M    6       0       17343 (+1)          111

内存最大的键值和平均的键值数据

redis-cli --bigkeys 对当前占用内存最大的键值和平均的键值数据,也可以通过指定-i参数定时查看当前的视图情况。

# Scanning the entire keyspace to find biggest keys as well as
# average sizes per key type.  You can use -i 0.1 to sleep 0.1 sec
# per 100 SCAN commands (not usually needed).

[00.00%] Biggest string found so far 'asdf.js' with 3 bytes
[00.00%] Biggest string found so far 'wabg-tokeneyJhbGciOiJIUzI1NiJ9.NA.UGGRiB2I42rP-33cIMrcoPub7AzHgDlqHacAKFw1pfE' with 328 bytes
[00.00%] Biggest string found so far 'wabg-token-province' with 231042 bytes

-------- summary -------

Sampled 4 keys in the keyspace!
Total key length in bytes is 180 (avg len 45.00)

Biggest string found 'wabg-token-province' has 231042 bytes

4 strings with 231819 bytes (100.00% of keys, avg size 57954.75)
0 lists with 0 items (00.00% of keys, avg size 0.00)
0 sets with 0 members (00.00% of keys, avg size 0.00)
0 hashs with 0 fields (00.00% of keys, avg size 0.00)
0 zsets with 0 members (00.00% of keys, avg size 0.00)

查看当前的键值情况

redis-cli --scan提供和keys *相似的功能,查看当前的键值情况,可以通过正则表达

$ redis-cli --scan

sess:K4xh-bxOBrcXpy9kEW87oiy-u7I2sAA5
asdf.js
sess:1tGNZSXW8GyoEQsbtpqkA5tMmSFp_ZIn
wabg-tokeneyJhbGciOiJIUzI1NiJ9.NA.UGGRiB2I42rP-33cIMrcoPub7AzHgDlqHacAKFw1pfE
sess:3e4NGIJd0wf1-RONeTt-FsXQj4EaVNjk
wabg-token-province
sess:UuCLAX2sWZ50fiIO1qvDgulf0XIZRd98
wabg-tokeneyJhbGciOiJIUzI1NiJ9.MQ.6z44GClzAsUED1M_UyxqdREdDKcYFnL9tSqd5ZhLhsY
sess:2HEchaRLYUoaa44IF1bB6mpik7lZjBb4

原生的Monitor监控

redis-cli monitor打印出所有sever接收到的命令以及其对应的客户端地址

$ redis-cli monitor
OK
1472626566.218175 [0 127.0.0.1:62862] "info"
1472626571.220948 [0 127.0.0.1:62862] "exists" "aaa"
1472626571.223174 [0 127.0.0.1:62862] "set" "aaa" ""
1472626571.232126 [0 127.0.0.1:62862] "type" "aaa"
1472626571.243697 [0 127.0.0.1:62862] "pttl" "aaa"
1472626571.243717 [0 127.0.0.1:62862] "object" "ENCODING" "aaa"
1472626571.243726 [0 127.0.0.1:62862] "strlen" "aaa"

Redis事件通知发布订阅

基本服务操作

## 启动并后台运行
$ redis-server & nohup
## 查是否启动
$ redis-cli ping
## 关闭命令
$ redis-cli shutdown

基本Redis操作

## 命令行客户端启动
$ redis-cli
## 测试心跳
127.0.0.1:6379> ping
PONG
## 设置 mykey 键的值
127.0.0.1:6379> set mykey hello
OK
## 获取 mykey 键的值
127.0.0.1:6379> get mykey
"hello"
## 设置 mykey 失效事件
127.0.0.1:6379> expire mykey 2

开启通知

键空间事件通知默认被禁用,因为这个特性消耗CPU电量不是很明智。使用redis.confnotify-keyspace-events,或者通过CONFIG SET来开启通知。

## 设置配置开启通知功能
$ redis-cli config set notify-keyspace-events KEA
## 命令行监控所有通知
$ redis-cli --csv psubscribe '__key*__:*'
Reading messages... (press Ctrl-C to quit)
"psubscribe","__key*__:*",1

键值说明

K     Keyspace events, published with __keyspace@<db>__ prefix.  
E     Keyevent events, published with __keyevent@<db>__ prefix.  
g     Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...  
$     String commands  
l     List commands  
s     Set commands  
h     Hash commands  
z     Sorted set commands  
x     Expired events (events generated every time a key expires)  
e     Evicted events (events generated when a key is evicted for maxmemory)  
A     Alias for g$lshzxe, so that the "AKE" string means all the events. 

不同命令产生的事件(Events generated by different commands)

按照下面的清单,不同的命令产生不同类型的事件。 Redis Keyspace Notifications

  • DEL 为每一个被删除的键产生一个del事件。
  • RENAME 产生两个事件,为源键产生一个rename_from事件,为目标键产生一个rename_to事件。
  • EXPIRE 当为键设置过期时产生一个expire事件,或者每当设置了过期的键被删除时产生一个expired事件(查看EXPIRE文档获取更多信息)。
  • SORT 当STORE用于设置一个新键时产生一个sortstore事件。当结果列表为空,并且使用了STORE选项,并且已经有一个该名字的键存在,那么这个件键被删除,所以这种条件下或产生一个del事件。
  • SET及其所有变种(SETEX, SETNX,GETSET) 产生set事件。但是SETEX还会产生一个expire事件。
  • MSET 为每个键产生一个单独的set事件。
  • SETRANGE 产生一个setrange事件。
  • INCR, DECR, INCRBY, DECRBY 都产生incrby事件。
  • INCRBYFLOAT 产生一个incrbyfloat事件。
  • APPEND 产生一个append事件。
  • LPUSH和LPUSHX 产生单个lpush事件,即使在可变情况下(even in the variadic case)。
  • RPUSH和RPUSHX 产生单个rpush事件,即使在可变情况下(even in the variadic case)。
  • RPOP 产生一个rpop事件。如果键由于最后一个元素被从列表中弹出而导致删除,会又产生一个del事件。
  • LPOP 产生一个lpop事件。如果键由于最后一个元素被从列表中弹出而导致删除,会又产生一个del事件。
  • LINSERT 产生一个linsert事件。
  • LSET 产生一个lset事件。
  • LREM 产生一个lrem事件。如果结果列表为空并且键被删除,会又产生一个del事件。
  • LTRIM 产生一个ltrim事件。如果结果列表为空并且键被删除,会又产生一个del事件。
  • RPOPLPUSH和BRPOPLPUSH 产生一个rpop事件和一个lpush事件。两种情况下顺序都能保证 (lpush事件总是在rpop事件之后被传递) 如果结果列表长度为零并且键被删除,会又产生一个del事件。
  • HSET, HSETNX和HMSET 都产生单个hset事件。
  • HINCRBY 产生一个hincrby事件。
  • HINCRBYFLOAT 产生一个hincrbyfloat事件。
  • HDEL 产生单个hdel事件。如果结果哈希为空并且键被删除,会又产生一个del事件。
  • SADD 产生单个sadd事件,即使在可变情况下(even in the variadic case)。
  • SREM 产生单个srem事件。如果结果集合为空并且键被删除,会又产生一个del事件。
  • SMOVE 为源键产生一个srem事件为目标键产生一个sadd事件。
  • SPOP 产生一个spop事件。如果结果集合为空并且键被删除,会又产生一个del事件。
  • SINTERSTORE, SUNIONSTORE, SDIFFSTORE 分别产生sinterstore,sunionostore,sdiffstore事件。在特殊情况下,集合为空,且存储结果的键已经存在,由于键被删除,会产生一个del事件。
  • ZINCR 产生一个zincr事件。
  • ZADD产生单个zadd事件,即使添加了多个元素。.
  • ZREM 产生单个zrem事件,即使删除了多个元素。当结果有序集合为空,并且键被生成时,会产生一个额外的del事件。
  • ZREMBYSCORE 产生单个zrembyscore事件。当结果有序集合为空,并且键被生成时,会产生一个额外的del事件。
  • ZREMBYRANK 产生单个zrembyrank事件。当结果有序集合为空,并且键被生成时,会产生一个额外的del事件。
  • ZINTERSTORE和ZUNIONSTORE 分别产生zinterstore和zunionstore事件。在特殊情况下,集合为空,且存储结果的键已经存在,由于键被删除,会产生一个del事件。
  • 每当一个关联有生存事件的键由于过期而被从数据集中删除时会产生一个expired事件。
  • 每当一个键由于maxmemory策略而从数据集中被淘汰以节省内存时会产生一个evicted事件。

Redis开启远程登录连接

服务器状态

检查redis服务器系统进程

ps -aux|grep redis

使用 netstat 来查看端口占用情况,6379为默认Redis端口。

netstat -nlt|grep 6379
  • -t:指明显示 TCP 端口
  • -u:指明显示 UDP 端口
  • -l:仅显示监听套接字
  • -p:显示进程标识符和程序名称,每一个套接字/端口都属于一个程序。
  • -n:不进行 DNS 轮询,显示 IP (可以加速操作)

修改防火墙配置

修改防火墙配置 sudo vi /etc/sysconfig/iptables

-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT

修改配置文件

Redis protected-mode 是3.2 之后加入的新特性,在Redis.conf的注释中,我们可以了解到,他的具体作用和启用条件。可以在 sudo vi /etc/redis.conf 中编辑,修改配置文件。

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode yes

它启用的条件,有两个:

  1. 没有bind IP
  2. 没有设置访问密码

如果启用了,则只能够通过lookback ip(127.0.0.1)访问Redis cache,如果从外网访问,则会返回相应的错误信息:

(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the lookback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the --portected-mode no option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.