2014/06/27

redis 試し

・can write to db automatically
・can store thing with four structures not only plain string key
=->redis can used to slove a wider range of problems
STINGS、LIST、SET、HASH、ZSET(順番あるET)

・publish/Subscribe 機能?raid,master/slave機能

・in-memory databases! write to hardDisk

Depending on how careful you want to be with your
data, append-only writing can be configured to never sync, sync once per second, or
sync at the completion of every operation.

・for most
databases, inserting rows is a very fast operation (inserts write to the end of an on-disk
file, not unlike Redis’s append-only log). But updating an existing row in a table is fairly
slow (it can cause a random read and may cause a random write).

===>何が良さそうだね。。


redis-cli
=->クライアントを起動する

set hello world
get hello
del hello

大文字と小文字は違う。。。

rpush list item
rpush list item2
rpush list item3

lrange list 0 -1 ==>全ての物を表示する
lrange list start end

lindex list 1 ==>items
lpop list==>item左側から吐き出す
==>stackだね。。。

In Redis, SETs are similar to LISTs in that
they’re a sequence of strings, but unlike
LISTs, Redis SETs use a hash table to keep
all strings unique


sadd set item1
sadd set item2
sadd set item3
smembers==>item1 item2 item3
sismember set item3
srem set item1


Whereas LISTs and SETs in Redis hold
sequences of items, Redis HASHes store a
mapping of keys to values.

HSET hashName key value
Hget hashName key
HDEL hashName key
HGETALL


The keys (called members)
are unique, and the values (called scores) are limited to floating-point numbers. ZSETs
have the unique property in Redis of being able to be accessed by member (like a
HASH), but items can also be accessed by the sorted order and values of the scores
==>得点あり score

zadd zset 333 item1
zadd zset 444 item2
zadd zset 555 item3

zrange zset 0 -1
zrange zset 0 -1 withscores
zrangebyscore zset 0 335 withscores



conn = redis.Redis()
conn.get('key')
conn.incr('key')
conn.decr('key')

★snapshots persistence
redis-cli

=>bgsave,save(専務)
dbfilename dump.rdb
dir /var/lib/redis
==>/var/lib/redis/dump.rdbで保存される
設定ファイルでの設定:
save 900 1
save 300 10
save 60 10000
==>
Redis will automatically
trigger a BGSAVE operation if 10,000 writes have occurred within 60
seconds since the last successful save has started

When Redis receives a request to shut down by the SHUTDOWN command, or it
receives a standard TERM signal, Redis will perform a SAVE


if we’re
running on a virtual machine, letting a BGSAVE occur may cause the system to pause
for extended periods of time, or may cause heavy use of system virtual memory, which
could degrade Redis’s performance to the point where it’s unusable.

★append-only file persistence

file.write();
file.flush();