Professional Documents
Culture Documents
, , .
, , , (Karl Seguin),
.
.
- ,
. / ,
. Redis
. Redis
- mogade.com.
The Little MongoDB Book, MongoDB.
http://openmymind.net,
Twitter: @karlseguin.
, .
. .
. Twitter.
Redis
. .
:
- Dmitry Scriptin
-
- polinom
.
.
2
. ,
, ,
.
, , Redis .
? -, .
, ,
Redis. -, ,
. ? Redis
, . Redis
, . , Redis -, ,
, .
, Redis. , ,
, Redis
, , -
- . ,
. Redis .
Lucene, ,
. , Redis
.
- , Redis.
Redis
. ,
.
-: - , - ,
- . Redis , . Redis , ,
. ,
Redis .
Windows
Redis Windows,
. ,
- .
https://github.com/dmajkic/redis/downloads
( ).
3
, ,
64bit 32bit.
*nix MacOSX
*nix Mac, Redis .
, , http://redis.io/
download. 2.4.6.
:
wget http://redis.googlecode.com/files/redis-2.4.6.tar.gz
tar xzf redis-2.4.6.tar.gz
cd redis-2.4.6
make
( , Redis .
, MacOSX Homebrew
brew install redis.) (
- . .)
,
src. , cd src.
Redis
, Redis .
Redis .
(DOS- ). . Windows,
redis-server. *nix/MacOSX ./redis-server
.
, ,
redis.conf . Redis ,
.
, Redis redis-cli (Windows)
./redis-cli (*nix/MacOSX).
6389- TCP- .
, , info .
-,
.
4
,
Redis.
Redis
, API Redis
. , ,
, . ,
,
. ,
.
1 -
Redis ? ?
? , , ,
- Redis.
Redis (
- . .) -
.
, .
Redis,
, ( ),
.
-. ,
, Redis .
, Redis ,
-. ,
, ,
, Redis.
, .
, ,
- .
. , ,
. , .
, , , .
,
? , , ,
(, , ). , ,
.
?
, ?
- , .
( : , , , - . .)
, Redis. , ,
, , , ?
exists(key)
O(1) ( ,
)?
Redis .
. -
6
.
Redis ,
0. , select.
select 1. Redis OK
- redis 127.0.0.1:6379[1]>.
, select
0.
,
, Redis , -,
, ,
. , ,
.
- , .
, , : users:leto.
leto.
,
.
- , . .
, , (
JSON, XML ). , Redis
, . ,
-. ,
(string), (integer) JSON.
. :
set users:leto "{name: leto, planet: dune, likes: [spice]}"
Redis . ,
set, . set : ,
, , . , ,
( ). ,
? , ( , ):
get users:leto
. , - get set.
, .
7
Redis, . - ,
- . , , Redis
. , ,
dune.
. ,
, Redis
. . Redis
. ,
Redis (- ). ,
, .
. ,
Redis.
- Redis .
-, .
, Redis
. , , Redis
, .
, X - ,
Y . , Redis
60 , 1000 , 15 ,
9 .
, ( ), Redis
(append mode). , ,
, (
). 60
.
. Redis . 5
- (slave) .
, Redis .
Redis:
.
, ,
. 5,5 .
, , ,
/ . (
/) ,
8
, .
- Redis,
. , , ,
, /,
.
Redis . ,
( Redis)
.
, 5,5 2 . Redis
, , , ,
,
/ .
.
,
Redis, . ,
, Redis .
, - .
: , Redis , .
. , Redis
, .
? :
. Redis
. redis-benchmark
( , redis-server redis-cli), .
, ,
Redis. , , 5
. 150 Redis.
, ,
, .
Redis, ,
. SQL
. , Redis.
, , ,
Redis .
,
.
9
, Redis,
. , - - , , .
, ,
.
:
- , ()
- , Redis
Redis (
)
Redis ,
10
2 -
Redis. ,
, ,
.
, Redis , .
. Redis
, set?
. ,
set, (string). hset,
(hash). Redis,
.
Redis .
. ,
.
, .
, flushdb,
- !
(Strings)
Redis.
-, . (),
. , .
:
. ,
:
, Redis .
, strlen <> ,
; getrange <> <> <> ; append <>
<> (
, ). .
:
.
,
, .
append getrange
- ( / - . .)
incr, incrby, decr decrby.
/ (/ ) (
,
(scalars) ,
- . .):
> incr stats:page:about
(integer) 1
> incr stats:page:about
(integer) 2
> incrby ratings:video:12333 5
(integer) 5
> incrby ratings:video:12333 3
(integer) 8
, Redis .
users:leto (-) , (
).
setbit getbit. ,
Spool (Spool - , - . .)
?. 128
50 16 .
12
, Spool ,
, Redis ,
. ,
, : ( ) .
, ,
.
(Hashes)
- , Redis -
. .
, - (fields).
set get :
,
, :
, , . ,
,
. ,
.
, ,
, . ,
. , ,
.
, .
(Lists)
.
,
.
.
13
newusers,
:
(Sets)
- , . ,
.
:
sadd friends:leto ghanima paul chani jessica
sadd friends:duncan paul jessica alia
, , (O(1))
, userX userY , .
sismember friends:leto jessica
sismember friends:leto vladimir
14
, , :
sinter friends:leto friends:duncan
:
sinterstore friends:leto_duncan friends:leto friends:duncan
(tagging, - . .) ,
( , ,
).
(Sorted Sets)
.
, , ,
.
. ,
:
zadd friends:leto 100 ghanima 95 paul 95 chani 75 jessica 1 vladimir
, 90 leto?
zcount friends:leto 90 100
chani?
zrevrank friends:leto chani
zrevrank zrank, Redis
( ).
. ,
, ,
,
.
, (
),
.
15
Redis.
Redis , ,
. , ,
. ,
, Redis .
, ,
Redis .
.
16
3 -
, .
, ,
.
( O)
(
) ( ) O(N) O(1).
, ,
. Redis ,
,
. ( . Redis,
, , , ,
- . .)
Redis
. , .
.
O(1) - . ,
5 5 . ,
. sismember, ,
, O(1).
sismember - ,
, , . Redis
.
- O(log(N)) - ,
.
, ,
. zadd O(log(N)),
N - , .
- O(N).
, ltrim
. , ltrim N -
, . ltrim 1
, 10 ,
. ( , , ,
.)
zremrangebyscore,
,
17
O(log(N)+M). . ,
, N - , M -
. , , ,
, .
sort, ,
O(N+M*log(M)). , , ,
Redis.
, - O(N2)
O(CN). N, N. (
, - . .) Redis
.
, O .
, , O(N),
, ,
.
-
, ,
. ,
( ,
) ( ).
:
, ,
.
, Redis ,
( ). Redis (API).
( , ,
) , , Redis
- .
, :
-,
, . ,
get:
get users:9001
,
hget, get ( Ruby):
id = redis.hget('users:lookup:email', 'leto@dune.gov')
user = redis.get("users:#{id}")
, , , .
, , ,
.
, .
, , ,
, .
.
, ,
, . Redis
.
, :
,
. , chani
? , :
, , ,
,
.
, -
( ).
19
, ,
. ,
.
( ,
).
Redis .
. , , .
,
Redis. ,
, .
, ,
, .
mget, :
sadd, :
Redis . ,
Redis, , .
,
.
.
Redis ,
.
, , , ,
. ,
50 , , ,
.
, ,
. Ruby pipelined:
20
redis.pipelined do
9001.times do
redis.incr('powerlevel')
end
end
,
!
Redis , , .
, Redis .
, , Redis ,
() .
, . (
.) , , .
:
incr - , , get set
getset
setnx , ,
,
, ,
. ,
multi, , ,
exec , , discard,
. Redis ?
, (
)
,
, , . ,
.
21
multi
hincrby groups:1percent balance -9000000000
hincrby groups:99percent balance 9000000000
exec
, Redis ( ) ,
, () (-). ,
.
incr,
, exec. :
redis.multi()
current = redis.get('powerlevel)
redis.set('powerlevel', current + 1)
redis.exec()
redis.watch('powerlevel')
current = redis.get('powerlevel')
redis.multi()
redis.set('powerlevel', current + 1)
redis.exec()
powerlevel , watch
, . ,
. , .
-
,
. .
, - keys.
, .
, .
?
. , .
?
, - -
( ). id_
22
bug:id_:id_. -
(
), , , ( -) keys:
keys bug:1233:*
. ,
, :
hkeys bugs:1233.
hdel bugs:1233 2, - del bugs:1233.
, ,
Redis . ,
,
,
, ,
.
23
4 -
, Redis,
, . : info,
select, flushdb, multi, exec, discard, watch keys.
.
Redis .
Unix (Unix timestamp, ,
1 1970 ) .
, ,
.
expire pages:about 30
expireat pages:about 1356933600
( ) 30 .
12:00, 31 2012 .
Redis . ,
, ttl,
persist:
ttl pages:about
persist pages:about
, setex,
(
):
( ,
- . .)
Redis blpop brpop, ,
, , ,
.
.
24
, Redis
( , , ..
- . .). , redis-cli
. ( warnings):
subscribe warnings
. , ,
warnings:
, ,
warnings.
(subscribe channel1 channel2 channel3 ...)
, (psubscribe warnings:*),
unsubscribe punsubscribe
.
, , publish 1.
, .
monitor , Redis .
,
Redis. redis-cli ( ,
unsubscribe )
monitor. (, get set).
.
monitor ,
. ,
. .
monitor, Redis slowlog,
( - . .).
, ,
. , Redis,
, :
, .
, :
slowlog get
slowlog get 10
, slowlog
len
:
( Unix),
,
.
1024 .
Redis - sort.
, (
, ).
, :
rpush users:leto:guesses 5 9 10 2 4 10 19 2
sort users:leto:guesses
, .
:
, ( limit),
( desc), ,
( alpha).
26
sort .
, ,
Redis. sort
. , (), .
,
:
( ),
. ,
Redis, . ,
, :
set
set
set
set
severity:12339 3
severity:1382 2
severity:338 5
severity:9338 4
,
:
Redis (
) * . , Redis
.
Redis, ,
. ,
. , ,
:
,
severity () priority (),
sort, :
, Redis
->
. get,
, (details) .
sort . ,
sort :
( store) sort
, , .
, . ,
.
,
, ,
. , . ,
. ,
, .
28
5 -
Redis.
.
, Redis.
Redis, , redis.conf
.
Redis. redis.conf
release- Redis. ,
,
. https://github.com/antirez/redis/
raw/2.4.6/redis.conf.
Redis 2.4.6. 2.4.6
URL . ,
info.
,
.
Redis redis.conf, config set
.
,
slowlog-log-slower-than 0.
config get,
. .
, :
config get *log*
Redis , .
requirepass ( redis.conf config
set). requirepass - (
), auth password .
,
. flushall, .
:
rename-command CONFIG 5ec4db169f9d4dddacbfb0c26ea7e5ef
rename-command FLUSHALL 1041285018a942a4922cbf76623b741e
29
,
.
Redis, :
?. ,
(, )
. ,
.
Redis , , ,
Redis ( master)
( slave). slave- slaveof
(,
master-).
, .
, ..
slave-.
, .
, Redis
. master- ,
slave-. ,
master-,
.
Redis Redis
(Amazon S3, FTP, ). , Redis dump.rdb.
.
master- slave-.
master- slave-
, .
Redis Cluster
,
. , ( sort),
slave-
.
30
, - Redis
Redis ( -
, Redis ). , ,
- (
). ( - , -
- .
.) ,
. ,
, ,
.
, Redis Cluster.
,
,
.
,
. ,
Redis Cluster .
, Redis,
,
. , ,
, . Redis Cluster,
,
.
31
Redis .
, .
Redis .
, Redis .
, : Redis
. , ,
. , Redis
, , ,
Redis.
32