目次

Redis

公式
http://redis.io/

クイックスタート
http://redis.io/topics/quickstart

2.0.3日本語訳ドキュメント
http://redis.shibu.jp/

Install

from yum

yum -y --enablerepo=remi,eplp install redis php-redis

from src

# wget http://download.redis.io/redis-stable.tar.gz
# tar xvzf redis-stable.tar.gz
# cd redis-stable
# make
# make test
cd src && make test
make[1]: Entering directory `/root/redis-stable/src'
which: no tclsh8.5 in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
You need 'tclsh8.5' in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory `/root/redis-stable/src'
make: *** [test] Error 2

make testでエラーになるので、Tclをインストールすることに。

公式
http://www.tcl.tk/

Tcl/Tk - ウィキペディア - Wikipedia
http://ja.wikipedia.org/wiki/Tcl/Tk

# cd ..
# wget http://prdownloads.sourceforge.net/tcl/tcl8.5.14-src.tar.gz
# tar zxvf tcl8.5.14-src.tar.gz 
# cd tcl8.5.14/unix/
# ./configure
# make
# make install

再び、redisへ

# cd ../../redis-stable
# make test
~
\o/ All tests passed without errors!

Cleanup: may take some time... OK
make[1]: Leaving directory `/root/redis-stable/src'

インストール

# make install

起動スクリプト作成

# cd utils/
# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server


Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] 
s#^port [0-9]{4}$#port 6379#;s#^logfile .+$#logfile /var/log/redis_6379.log#;s#^dir .+$#dir /var/lib/redis/6379#;s#^pidfile .+$#pidfile /var/run/redis_6379.pid#;s#^daemonize no$#daemonize yes#;
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
./install_server.sh: line 178: update-rc.d: command not found
 exists, process is already running or crashed
Installation successful!

そのまはではエラーで使えないので編集

# cp /etc/rc.d/init.d/redis_6379 /etc/rc.d/init.d/redis_6379.org
# vi /etc/rc.d/init.d/redis_6379
:s/\\n/\r/g
#/bin/sh
# chkconfig:   - 85 15
# description: redis-server
# processname: redis-server
# vi /etc/redis/6379.conf
# chkconfig --add redis_6379
# chkconfig redis_6379 on
# chkconfig --list redis_6379
# /etc/init.d/redis_6379 start

ランキングのサンプル

<?php 
$server  = "localhost";
$port    = 6379; 
$timeout = 5;

$redis = new Redis();
$redis->connect($server,$port,$timeout);

// point
$userPoint = array(
    'osaka'    => 5000,
    'hyogo'    => 4000,
    'kyoto'    => 300,
    'nara'     => 200,
    'shiga'    => 100,
    'mie'      => 100,
    'aichi'    => 40
);

// pointをセットする
foreach( $userPoint as $user => $point ) {
    $redis->zAdd('test_rank', $point, $user );
}

// 一覧を取得・表示する
$ranking = $redis->zRevRange( 'test_rank', 0, -1, true );
foreach($ranking as $user => $score ) {
    $score++;
    echo "$user=".($redis->zCount('test_rank', $score, '+inf')+1)."\n";
}

ランキングのコマンドサンプル

# redis-cli
redis 127.0.0.1:6379> zadd test 1 taro
(integer) 1
redis 127.0.0.1:6379> zadd test 2 hanako
(integer) 1
redis 127.0.0.1:6379> zadd test 3 jiro
(integer) 1
redis 127.0.0.1:6379> zrange test 0 -1
1) "taro"
2) "hanako"
3) "jiro"
redis 127.0.0.1:6379> zrevrange test 0 -1
1) "jiro"
2) "hanako"
3) "taro"
redis 127.0.0.1:6379> zincrby test 5 taro
"6"
redis 127.0.0.1:6379> zrevrange test 0 -1
1) "taro"
2) "jiro"
3) "hanako"
redis 127.0.0.1:6379> zincrby test -3 taro
"3"
redis 127.0.0.1:6379> zrevrange test 0 -1
1) "taro"
2) "jiro"
3) "hanako"
redis 127.0.0.1:6379> zrevrange test 0 -1 WITHSCORES
1) "taro"
2) "3"
3) "jiro"
4) "3"
5) "hanako"
6) "2"
redis 127.0.0.1:6379> zscore test taro
"3"

コマンドヘルプ

http://redis.shibu.jp/commandreference/sortedsets.html