memcachedは高性能な分散メモリキャッシュサーバです。

参考:

参考:クライアントライブラリ

監視ツール

[root@localhost ~]# /usr/local/src/memcached-1.4.2/scripts/memcached-tool

■アーカイブをget
[root@localhost src]# wget http://memcached.googlecode.com/files/memcached-1.4.2.tar.gz

[root@localhost src]# tar zxvf memcached-1.4.2.tar.gz
[root@localhost src]# cd memcached-1.4.2
[root@localhost memcached-1.4.2]# ./configure
If it's already installed, specify its path using --with-libevent=/dir/

★libeventのパスを指定しろと言われる。

[root@localhost memcached-1.4.2]# rpm -qa libevent
libevent-1.1a-3.2.1

★libeventはインストールされているのに。。。

■libevent-develをインストール
[root@localhost memcached-1.4.2]# yum install -y libevent-devel.i386
Installing : libevent-devel [1/1]


Installed: libevent-devel.i386 0:1.1a-3.2.1
Complete!

■改めてconfigure・インストール
[root@localhost memcached-1.4.2]# ./configure
[root@localhost memcached-1.4.2]# make
[root@localhost memcached-1.4.2]# make install


■起動
[root@localhost memcached-1.4.2]# memcached -d -m 1024 -l 127.0.0.1 -p 11211 -u nobody
※rootでは起動できないので、-u nobody で起動。
あるいは、専用ユーザ作る?

■接続
[root@localhost memcached-1.4.2]# telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
stats
STAT pid 7926
STAT uptime 209
STAT time 1256799888
STAT version 1.4.2
STAT pointer_size 32
STAT rusage_user 0.000000
STAT rusage_system 0.005999
STAT curr_connections 5
STAT total_connections 6
STAT connection_structures 6
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT bytes_read 7
STAT bytes_written 0
STAT limit_maxbytes 1073741824
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
END


試しに
★試しに、「123」を格納してみる
<コマンド> <キー> <圧縮フラグ(1 or 0)> <有効期間(単位:秒)> <サイズ(単位:Bytes)>
例) 「foo」というキーに「123」という3バイトのデータを非圧縮(0)/無期限(0)で格納する
・・・引用参考:Life with IT

set foo 0 0 3
123
STORED

★読み込んでみる
get foo
VALUE foo 0 3
123
END


quit
Connection closed by foreign host.

■起動スクリプトを作成
★/etc/sysconfig/memcached 確認
★なれけば作る。

[root@localhost ~]# ls -l /etc/sysconfig/memcached
-rw-r--r-- 1 root root 68 9月 11 2008 /etc/sysconfig/memcached

[root@localhost ~]# vi /etc/sysconfig/memcached

####[root@localhost ~]# cat /etc/sysconfig/memcached
PORT="11211"
USER="nobody"
MAXCONN="1024"
CACHESIZE="128"
OPTIONS=""

■inid確認
★あれば上書き
[root@localhost ~]# ls -l /etc/rc.d/init.d/memcached
-rwxr-xr-x 1 root root 1334 9月 11 2008 /etc/rc.d/init.d/memcached

[root@localhost ~]# vi /etc/rc.d/init.d/memcached
#!/bin/bash
#
# Init file for memcached
#
# Written by Dag Wieers
#
# chkconfig: - 80 12
# description: Distributed memory caching daemon
#
# processname: memcached
# config: /etc/sysconfig/memcached
# config: /etc/memcached.conf

source /etc/rc.d/init.d/functions

### Default variables
PORT="11211"
USER="nobody"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
SYSCONFIG="/etc/sysconfig/memcached"
DST_BIN="/usr/local/bin/memcached"

### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"

RETVAL=0
prog="memcached"
desc="Distributed memory caching"

start() {
echo -n $"Starting $desc ($prog): "
daemon $DST_BIN -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE $OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}

stop() {
echo -n $"Shutting down $desc ($prog): "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
return $RETVAL
}

restart() {
stop
start
}

reload() {
echo -n $"Reloading $desc ($prog): "
killproc $prog -HUP
RETVAL=$?
echo
return $RETVAL
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[ -e /var/lock/subsys/$prog ] && restart
RETVAL=$?
;;
reload)
reload
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
RETVAL=1
esac

exit $RETVAL
[root@localhost ~]#

■chkconfigに登録

[root@localhost ~]# chkconfig --add memcached
[root@localhost ~]# chkconfig --list memcached
[root@localhost ~]# chkconfig memcached on