何やら業務でちょっとしたmemcachedを使ったアレを作る事になりそうで、 かれこれ2年くらい使ってないので、慣らし運転的に、いつものVirtualBox上のUbuntuで。 libeventがーとかって昔mixiさんとかのアレで話題になってましたが、 普通にapt-getでw
$ sudo apt-get install memcached Reading package lists... Done Building dependency tree
素直に叩いてあげると、、
$ memcached
ちゃんと上がってますねー、と。
$ ps -ef | grep 11211 memcache 4386 1 0 13:19 ? 00:00:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
telnetで繋げて、setしてgetしてみます。
$ telnet localhost 11211 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. set mykey 0 60 4 data STORED get mykey VALUE mykey 0 4 data END
setはちょっとヤヤコシイのですが↓とのことです。
set <key> <flags> <exptime> <bytes> [noreply]rn<value>rn
んで、毎回telnetってのもなぁってことでツールをインストール
$ sudo apt-get install libmemcached-tools
↓みたいにズラズラって入りますが、
$ ls /usr/bin/mem<タブ> memcached memccat memcdump memcflush memcslap memdiskfind memccapable memccp memcerror memcrm memcstat
データ入れるのをどんな風に使うかって言うと↓こんな感じ。
$ /usr/bin/memccp -h memcp v1.0 Copy a set of files to a memcached cluster. Current options. A '=' means the option takes a value. --version Display the version of the application and then exit. --help Display this message and then exit. --verbose Give more details on the progression of the application. --debug Provide output only useful for debugging. --servers= List which servers you wish to connect to. --flag= Provide flag information for storage operation. --expire= Set the expire option for the object. --set Use set command with memcached when storing. --add Use add command with memcached when storing. --replace Use replace command with memcached when storing. --hash= Select hash type. --binary Switch to binary protocol. --username= Username to use for SASL authentication --password= Password to use for SASL authentication
突っ込む用にファイルを作って、
$ cat hage hoge
出し入れしてみます。
$ memccp --servers=localhost --set hage $ memccat --servers=localhost hage hoge
これはこれでナイスだけど、KeyとValueをカンマ区切りにして ズダーーっとみたいなコマンド無いのかな~? んで、チョロチョロっと叩いた後に↓こんな感じで状況知れたり。
$ memcstat --servers=localhost Server: localhost (11211) pid: 4386 uptime: 2646 time: 1352783011 version: 1.4.13 libevent: 2.0.16-stable pointer_size: 32 rusage_user: 0.000000 rusage_system: 0.132008 curr_connections: 5 total_connections: 14 connection_structures: 6 reserved_fds: 20 cmd_get: 7 cmd_set: 8 cmd_flush: 0 cmd_touch: 0 get_hits: 5 get_misses: 2
ダンプしたり。
$ memcdump --servers=localhost hage hoogee
フラッシュも出来やす。
$ memcflush --servers=localhost
で、コレいつ消えるかっていうと、次のダンプの時はまだ存在するけど
$ memcdump --servers=localhost hage hoogee
getしにいくと無くなってて、
$ memccat --servers=localhost hage memcat: hage not found $ memccat --servers=localhost hoogee memcat: hoogee not found
もっかいダンプ叩くと無くなってます。
$ memcdump --servers=localhost
めでたしめでたし。 これでシェルだけでやりくり出来るなーって思ったけど、 このmemcachedのコマンドツールって諸事情でインストール出来ないか…。 Rubyも入ってないのでまたJavaかのぅ…。 ■ MemcachedのJavaクライアントライブラリ ↓をみるとイロイロあるみたいだけど Client API's / libraries - Memcached - Google Project Hosting spymemcached が一番ガッツリ出てるのでソレで。 CouchBaseの人が作ったみたいです。 ■ Jar落としてきてコード書いて叩いてみる Maven(pom.xml)にspymemcachedのjarを取ってくる設定を入れます。
<repositories> <repository> <id>spy</id> <name>Spy Repository</name> <layout>default</layout> <url>http://files.couchbase.com/maven2/ <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <dependencies> <dependency> <groupId>spy</groupId> <artifactId>spymemcached</artifactId> <version>2.8.4</version> </dependency> </dependencies>
Memcachedにデータを入れて出すJavaのMain文のプログラム。
public static void main(String args[]) throws IOException { System.out.println("Start!!"); MemcachedClient memcachedClient = new MemcachedClient( new InetSocketAddress("localhost", 11211)); memcachedClient.set("HogeHogeID", 0, "HageHage"); System.out.println("Set done"); String result = memcachedClient.get("HogeHogeID").toString(); System.out.println(result); }
動かしてみると、、
Start!! 2012-11-13 15:44:03.526 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue 2012-11-13 15:44:03.534 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@bb6ab6 Set done HageHage
めでたしめでたし(二回目)