株式会社ミクシィの前坂です。第1回でmemcached 1.
バイナリプロトコルの扱い
バイナリプロトコルを扱うには、
今回の記事ではそれらのクライアントも含めて, C、
バイナリプロトコルを扱うデモコード
バイナリプロトコルを扱うにはバイナリモードに適応するというコードを1行追加するだけで, ほとんどの場合は従来のテキストプロトコルと扱いは変わりません。以下が今回の検証に使ったクライアントライブラリ集です。
デモコードはmemcachedがlocalhostのデフォルトポート
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libmemcached/memcached.h>
int main(int argc, char **argv) {
memcached_st *memc = NULL;
memcached_return rv;
const char *key = "some_key";
const char *val = "this is a value";
int data;
if ((memc = memcached_create(NULL)) == NULL) {
fprintf(stderr, "failed to allocate memory\n");
return 1;
}
rv = memcached_server_add(memc, "localhost", 11211);
if (rv != MEMCACHED_SUCCESS) {
fprintf(stderr, "failed to set server\n");
return 1;
}
rv = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, data);
if (rv != MEMCACHED_SUCCESS) {
fprintf(stderr, "failed to enable binary protocol\n");
return 1;
}
rv = memcached_set(memc, key, strlen(key), val, strlen(val), 0, 0);
if (rv != MEMCACHED_SUCCESS) {
fprintf(stderr, "failed to set record\n");
return 1;
}
char *result;
uint32_t flags;
size_t result_length;
result = memcached_get(memc, key, strlen(key), &result_length, &flags, &rv);
if (rv != MEMCACHED_SUCCESS) {
fprintf(stderr, "failed to fetch record\n");
return 1;
}
printf("Key: %s\n", key);
printf("Fetched: %s\n", result);
printf("Data Length: %d\n", (int)result_length);
free(result);
memcached_free(memc);
return 0;
}
import net.spy.memcached.AddrUtil;
import net.spy.memcached.BinaryConnectionFactory;
import net.spy.memcached.MemcachedClient;
public class BinaryProtocolExample {
public static void main(String[] args) {
MemcachedClient memc = null;
try {
memc = new MemcachedClient(new BinaryConnectionFactory(),
AddrUtil.getAddresses("localhost:11211"));
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
memc.set("some_key", 0, "this is a value");
System.out.println(memc.get("some_key"));
memc.shutdown();
}
}
#!/usr/bin/python
import sys
import pylibmc
memc = pylibmc.Client(["localhost:11211"])
if memc is None:
print "failed to create client"
exit(1)
memc.behaviors["binary_protocol"] = 1;
if memc.set("some_key", "this is a value") is None:
print "failed to set record"
exit(1)
print memc.get("some_key")
<?php
$memc = new Memcached();
if (!$memc) {
echo("failed to instantiate memcached object\n");
exit(1);
}
$memc->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$memc->addServer('localhost', 11211);
if (!$memc->set('some_key', 'this is a value')) {
echo("failed to set an item\n");
exit(1);
}
echo($memc->get('some_key') . "\n");
?>
バイナリプロトコル仕様
プロトコルには
バイナリプロトコルの詳細な話に関しては以前執筆させていただいた、
バイナリプロトコルとコネクション制約
memcachedは1つのコネクションに対して、
ライブラリ移行における運用面の注意点
memcachedのクライアントライブラリは世の中に多数存在しますが、
クライアントライブラリの移行を考えている場合は新しいライブラリの動作を調査し、
気になる性能
プロトコルの違いによるスループットの差をlibmemcached付属のmemslapというベンチマークツールで測定してみました。今回のベンチマークは10万レコードをキャッシュしているサーバに対して、
並走度 | バイナリプロトコル | テキストプロトコル |
---|---|---|
1 | 14. | 14. |
2 | 15. | 15. |
4 | 17. | 17. |
8 | 22. | 23. |
16 | 23. | 25. |
上記の結果を見る以上、
次回予告
今回はC言語、