FAT47の底辺インフラ議事録

学んだことのメモ帳です

RedisのデータへアクセスできるApacheモジュール「mod_redis」を入れてみた

apache mod_redis
データ永続化が可能なKVS「Redis」にRESTfulなインタフェースを提供してくれる激アツなapacheモジュールがでてきました。URLやメソッドごとにRedisへデータ追加・取得・削除ができるので、Javascript等からRedisのデータが非常に扱いやすくなると思います。

とりあえずインストール手順とデータの取得をやってみます。


Github
https://github.com/sneakybeaky/mod_redis


Redisのインストール

wget http://redis.googlecode.com/files/redis-2.4.10.tar.gz
tar zxvf redis-2.4.10.tar.gz 
cd redis-2.4.10; make; make install;

Redis起動

redis-server

mod_redisの入手

git clone https://github.com/sneakybeaky/mod_redis.git

これも

curl https://github.com/antirez/hiredis/zipball/master -L -o hiredis.zip
unzip hiredis.zip

変数をセット
※hiredis-xxxxxの部分はバージョンにより異なります

HIREDIS_HOME="`pwd`/antirez-hiredis-857b269"

hiredis libraryのコンパイル

pushd $HIREDIS_HOME ; make ; popd

mod_redisコンパイルとインストール

cd mod_redis
apxs -c -I $HIREDIS_HOME $HIREDIS_HOME/libhiredis.a mod_redis.c
apxs -i -a mod_redis.la

こんな感じのが出力されます

/usr/lib64/httpd/build/instdso.sh SH_LIBTOOL='/usr/lib64/apr-1/build/libtool' mod_redis.la /usr/lib64/httpd/modules
/usr/lib64/apr-1/build/libtool --mode=install cp mod_redis.la /usr/lib64/httpd/modules/
cp .libs/mod_redis.so /usr/lib64/httpd/modules/mod_redis.so
cp .libs/mod_redis.lai /usr/lib64/httpd/modules/mod_redis.la
cp .libs/mod_redis.a /usr/lib64/httpd/modules/mod_redis.a
chmod 644 /usr/lib64/httpd/modules/mod_redis.a
ranlib /usr/lib64/httpd/modules/mod_redis.a
PATH="$PATH:/sbin" ldconfig -n /usr/lib64/httpd/modules
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/lib64/httpd/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
chmod 755 /usr/lib64/httpd/modules/mod_redis.so
[activating module `redis' in /etc/httpd/conf/httpd.conf]

http.confに追記。完了したらapache再起動。

<Location ~ /redis/*>
SetHandler redis
</Location>

<IfModule redis_module>
RedisIPAddress 127.0.0.1
RedisPort 6379
RedisAlias ^ping$ "PING"
RedisAlias ^([^/]+)$ "GET $1"
RedisAlias ^([^/]+)$ "SET $1 %{DATA}" PUT
RedisAlias ^([^/]+)$ "DEL $1" DELETE
</IfModule>


この設定により/redis以下を叩くとredisのデータへアクセスすることができます。
RedisAliasに記述することでデータの取得方法などを指定していきます。

公式より引用
%{DATA} The body content of a PUT or POST request
${FORM:field} The value of a form field taken from PUT or POST request. The request must have include the Content-Type header as application/x-www-form-urlencoded
${QSA:field} The value of a query string argument taken from the request URL

動作確認
http://localhost/redis/ping.json?callback=alert

{ "status" : "PONG" }

別ターミナルからredisに値をセットしてみる

redis-cli
set foo bar
get foo
"bar"

ブラウザから確認
http://localhost/redis/foo.json?callback=alert

{ "string" : "bar" }

http://localhost/redis/foo
これでも値取れます

GET以外をしてみる

PUT,DELETEなどはそれぞれのHTTPメソッドで行います。
今回は面倒なのでこのrest-clientというクライアントを使います。
実行にはJAVA環境が必要です。

http://code.google.com/p/rest-client/
restclient-cli-2.4-jar-with-dependencies.jar

※図1

こんなかんじでメソッドが選択できるので、
先程セットしたfooを消してみます。

URLを入力
http://localhost/redis/foo
methodでDELETE選択し>>ボタンを押す。

図1の下画面にあるBodyタブを見るとresponseに1が帰ってきているのがわかります。

ターミナルから確認

redis-cli
get foo
nil

消えた。


PUTで値をSETしたいけど500エラーが出てできない…。
だれか教えてくだしあ。