■インストール
wget http://redis.googlecode.com/files/redis-1.2.6.tar.gz
tar xvzf redis-1.2.6.tar.gz
cd redis-1.2.6
make
make完了!
エラー
以下のようなエラーが出るかもしれない。
cc -c -std=c99 -pedantic -O2 -Wall -W -g -rdynamic -ggdb adlist.c
make: cc: コマンドが見つかりませんでした
make: *** [adlist.o] エラー 127
対策
gccのインストールだ。
yum install gcc
/usr/local/libに移動することにした。
mv redis-1.02 /usr/local/lib/redis-1.02
mv redis-1.2.6 /usr/local/lib/redis-1.2.6
ln -s /usr/local/lib/redis-1.2.6/ /usr/local/lib/redis
■操作
起動
以下のコマンドでRedisサーバが起動する。
/usr/local/lib/redis/redis-server
操作
以下のコマンドでRedisを操作する。
/usr/local/lib/redis-cli set mykey somevalue
#OK
/usr/local/lib/redis-cli get mykey
#somevaalue
停止
以下のコマンドでRedisを停止できる。
/usr/local/lib/redis-cli shutdown
redis-2.4.16
久しぶりにインストールしてみたら結構変わっていたのでメモしておく。
wget http://redis.googlecode.com/files/redis-2.4.16.tar.gz
tar xvzf redis-2.4.16.tar.gz
cd redis-2.4.16
make
make test
sudo make install
sudo ./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
which: no redis-server in (/sbin:/bin:/usr/sbin:/usr/bin)
Please select the redis executable path [] /usr/local/bin/redis-server
which: no redis-cli in (/sbin:/bin:/usr/sbin:/usr/bin)
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
大体デフォルトで大丈夫だが、redis-serverのパスだけ指定する必要がある。このスクリプトを実行すると起動まで行うがちょっと余計なお世話。
/etc/redis/6379.conf
パスワードを設定するには以下のディレクティブを使う。
requirepass "hogehoge"
■PHPからの使用
phpredis
http://github.com/owlient/phpredis
phpizeが必要になるかもしれないがインストールされていないかもしれない。そんな時は以下のコマンドを実行する。
yum install php-devel
以下のコマンドを実行するとインストールできる。
wget http://github.com/owlient/phpredis/tarball/master
tar xvzf owlient-phpredis-1.2.0-46-g66ac97b.tar.gz
cd owlient-phpredis-66ac97b/
phpize
./configure
make
make install
以下のように表示されるが、まだ使用不可能である。
Installing shared extensions: /usr/lib/php/modules/
ちゃんとPHPに組み込む。
cp /etc/php.d/dbase.ini /etc/php.d/redis.ini
/etc/php.d/redis.iniには以下のように記述する。
; Enable redis extension module
extension=redis.so
apacheを再起動する。
/etc/init.d/httpd restart
以下のコマンドを実行して確かめる。
php -r 'var_dump(get_declared_classes());'
array(54) {
[0]=>
string(8) "stdClass"
[1]=>
string(9) "Exception"
[2]=>
string(14) "ErrorException"
[3]=>
string(11) "LibXMLError"
[4]=>
string(22) "__PHP_Incomplete_Class"
[5]=>
string(15) "php_user_filter"
[6]=>
string(9) "Directory"
[7]=>
string(16) "SimpleXMLElement"
[8]=>
string(25) "RecursiveIteratorIterator"
[9]=>
string(16) "IteratorIterator"
[10]=>
string(14) "FilterIterator"
[11]=>
string(23) "RecursiveFilterIterator"
[12]=>
string(14) "ParentIterator"
[13]=>
string(13) "LimitIterator"
[14]=>
string(15) "CachingIterator"
[15]=>
string(24) "RecursiveCachingIterator"
[16]=>
string(16) "NoRewindIterator"
[17]=>
string(14) "AppendIterator"
[18]=>
string(16) "InfiniteIterator"
[19]=>
string(13) "EmptyIterator"
[20]=>
string(11) "ArrayObject"
[21]=>
string(13) "ArrayIterator"
[22]=>
string(22) "RecursiveArrayIterator"
[23]=>
string(11) "SplFileInfo"
[24]=>
string(17) "DirectoryIterator"
[25]=>
string(26) "RecursiveDirectoryIterator"
[26]=>
string(13) "SplFileObject"
[27]=>
string(17) "SplTempFileObject"
[28]=>
string(17) "SimpleXMLIterator"
[29]=>
string(14) "LogicException"
[30]=>
string(24) "BadFunctionCallException"
[31]=>
string(22) "BadMethodCallException"
[32]=>
string(15) "DomainException"
[33]=>
string(24) "InvalidArgumentException"
[34]=>
string(15) "LengthException"
[35]=>
string(19) "OutOfRangeException"
[36]=>
string(16) "RuntimeException"
[37]=>
string(20) "OutOfBoundsException"
[38]=>
string(17) "OverflowException"
[39]=>
string(14) "RangeException"
[40]=>
string(18) "UnderflowException"
[41]=>
string(24) "UnexpectedValueException"
[42]=>
string(16) "SplObjectStorage"
[43]=>
string(19) "ReflectionException"
[44]=>
string(10) "Reflection"
[45]=>
string(18) "ReflectionFunction"
[46]=>
string(19) "ReflectionParameter"
[47]=>
string(16) "ReflectionMethod"
[48]=>
string(15) "ReflectionClass"
[49]=>
string(16) "ReflectionObject"
[50]=>
string(18) "ReflectionProperty"
[51]=>
string(19) "ReflectionExtension"
[52]=>
string(5) "Redis"
[53]=>
string(14) "RedisException"
}
(^○^)お!(^□^)め!(^◇^)で!(^▽^)と!(^・^)う!
サンプルコード
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
■起動スクリプト
/usr/local/lib/redis/redis.conf
Redisはデフォルトではデーモンとして動作しないので、設定ファイルを以下のように編集する。
daemonize yes
スクリプト
http://blog.fulltext-search.biz/archives/2010/04/redis-server-initialize-script-for-centos.html
■ベンチマーク
ちょっとだけMySQLと比較してみる。テーブルはInnoDBでカラムはid, valueの2つである。
$start = microtime(true);
$dbh = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('benchmark', $dbh);
for($i = 0; $i < 10000; $i++){
mysql_query("INSERT INTO `list`(`value`) VALUES('abcdefghijklmnopqrstuvwxyz')");
print($i . "\n");
}
$end = microtime(true);
print($end - $start);// 5.5481600761414
10000レコードの挿入を10回繰り返してみた。多少のバラつきはある。
$start = microtime(true);
$redis = new Redis();
$redis->connect('localhost', 6379);
for($i = 0; $i < 10000; $i++){
$redis->set($i, 'abcdefghijklmnopqrstuvwxyz');
print($i . "\n");
}
$end = microtime(true);
print($end - $start);// 0.77555513381958
大体1/5~1/8程度の時間で終わるようだ。全く同じ条件というものは再現できないので参考程度。用途に応じた選択が必要である。