Redisインストール

Redis2.6.13インストールのメモ。

インストール

cd /usr/local/src
wget -c http://redis.googlecode.com/files/redis-2.6.13.tar.gz
tar zxvf redis-2.6.13.tar.gz
cd redis-2.6.13
make && make install

設定

mkdir -p /usr/local/redis/dump
cp -pi redis.conf /usr/local/redis/.
cd /usr/local/redis
vi redis.conf
# /usr/local/redis/redis.conf
daemonize yes
pidfile /var/run/redis.pid
port 6379
timeout 0
tcp-keepalive 0
loglevel notice
logfile /var/log/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /usr/local/redis/dump
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
maxmemory 100m
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10

Redis起動/停止

# redis-server /usr/local/redis/redis.conf
# redis-cli
redis 127.0.0.1:6379> select 1
OK
redis 127.0.0.1:6379[1]> set "key" "value"
OK
redis 127.0.0.1:6379[1]> keys *
1) "key"
redis 127.0.0.1:6379[1]> get "key"
"value"
redis 127.0.0.1:6379[1]> del "key"
(integer) 1
redis 127.0.0.1:6379[1]> exists "key"
(integer) 0
redis 127.0.0.1:6379[1]> keys *
(empty list or set)
redis 127.0.0.1:6379[1]> set "key2" "value2"
OK
redis 127.0.0.1:6379[1]> keys *
1) "key2"
redis 127.0.0.1:6379[1]> quit
# redis-cli bgsave
Background saving started
# ls -l /usr/local/redis/dump/
合計 4
-rw-r--r-- 1 root root 46  5月 11 00:16 2013 dump.rdb
# redis-check-dump /usr/local/redis/dump/dump.rdb 
==== Processed 5 valid opcodes (in 29 bytes) ===================================
CRC64 checksum is OK
# redis-cli shutdown

Rubyから使う

gem install redis
[1] pry(main)> require 'redis'
=> true
[2] pry(main)> redis = Redis.new(host: "localhost", port: 6379)
=> #<Redis client v3.0.4 for redis://localhost:6379/0>
[3] pry(main)> redis.set "key", "value"
=> "OK"
[4] pry(main)> redis.get "key"
=> "value"
[5] pry(main)> puts redis.get "key"
value
=> nil
[6] pry(main)>