telnet から memcached をつないでみた

memcached とは

  • 簡単に言うと、メモリ上にデータを格納できる
  • 運用例
    • DB への問い合わせ結果を一時的に保存することでアクセス回数を減らし、高速化やスケーラビリティを向上させる

インストール

$ sudo port install memcached
--->  Computing dependencies for memcached
--->  Fetching libevent
--->  Attempting to fetch libevent-1.4.13-stable.tar.gz from http://distfiles.macports.org/libevent
--->  Verifying checksum(s) for libevent
--->  Extracting libevent
--->  Applying patches to libevent
--->  Configuring libevent
--->  Building libevent
--->  Staging libevent into destroot
--->  Installing libevent @1.4.13_0
--->  Activating libevent @1.4.13_0
--->  Cleaning libevent
--->  Fetching memcached
--->  Attempting to fetch memcached-1.4.4.tar.gz from http://memcached.googlecode.com/files/
--->  Verifying checksum(s) for memcached
--->  Extracting memcached
--->  Configuring memcached
--->  Building memcached
--->  Staging memcached into destroot
--->  Creating launchd control script
###########################################################
# A startup item has been generated that will aid in
# starting memcached with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo launchctl load -w /Library/LaunchDaemons/org.macports.memcached.plist
###########################################################
--->  Installing memcached @1.4.4_0
--->  Activating memcached @1.4.4_0
--->  Cleaning memcached

memcached のオプションを確認

$ memcached -h
memcached 1.2.8
-p <num>      TCP port number to listen on (default: 11211)
-U <num>      UDP port number to listen on (default: 11211, 0 is off)
-s <file>     unix socket path to listen on (disables network support)
-a <mask>     access mask for unix socket, in octal (default 0700)
-l <ip_addr>  interface to listen on, default is INDRR_ANY
-d            run as a daemon
-r            maximize core file limit
-u <username> assume identity of <username> (only when run as root)
-m <num>      max memory to use for items in megabytes, default is 64 MB
-M            return error on memory exhausted (rather than removing items)
-c <num>      max simultaneous connections, default is 1024
-k            lock down all paged memory.  Note that there is a
              limit on how much memory you may lock.  Trying to
              allocate more than that would fail, so be sure you
              set the limit correctly for the user you started
              the daemon with (not for -u <username> user;
              under sh this is done with 'ulimit -S -l NUM_KB').
-v            verbose (print errors/warnings while in event loop)
-vv           very verbose (also print client commands/reponses)
-h            print this help and exit
-i            print memcached and libevent license
-P <file>     save PID in <file>, only used with -d option
-f <factor>   chunk size growth factor, default 1.25
-n <bytes>    minimum space allocated for key+value+flags, default 48
-R            Maximum number of requests per event
              limits the number of requests process for a given con nection
              to prevent starvation.  default 20
-b            Set the backlog queue limit (default 1024)


起動

# 最大メモリ 64M, 127.0.0.1:11211 で起動
$ memcached -d -m 64  -l 127.0.0.1 -p 11211 

telnet で接続

$ telnet 127.0.0.1 11211
set key1 0 3600 6
value1
get key1
VALUE key1 0 6
value1
END
  • コマンドは以下のように指定する
    • [noreply]\r\n
  • set で値を保存する
    • 今回は キーを"key1", フラグを"0", 期限を"3600"(秒), 値を"value1"として保存
  • get で値を取得する

まとめ

  • とりあえずどうやって値の保存、取得をしているのかは理解できた
    • まぁ set, get をしてみただけなんですけどね〜

参考