源程序 和示例程序可以从 这里下载:
http://people.freebsd.org/~seanc/libmemcache/
也可以从附件下载
本程序使用了bcd程序来转换c语言的头文件。
转换后的memcache.h为 bcd.libmemcache.memcache
有几个函数和类型没有转换(?),手动添加进去的。
编译方法为:
先把libmemcache装上,默认装到/usr/local下
然后用:
dmd memcached.d /usr/local/lib/libmemcache.a
PS:libmemcache的错误捕捉方法,见 libmemcache包中的test目录下的相关程序
该lib包的文档不多,多阅读test下的示例代码吧。
源程序看起来比较痛苦。
/** * Memcached的应用程序 * Edit by Liu Dehong @ 2007/08/09 * Version: * 1.0.0 */ import std.stdio; import std.c.stdlib; import std.string; import std.socket; import bcd.libmemcache.memcache; void main() { /* Create a new memcache instance */ memcache *mc = mc_new(); /* Add a few servers */ mc_server_add(mc, "127.0.0.1", "11211"); mc_server_add(mc, "127.0.0.1", "11212"); mc_server_add4(mc, "127.0.0.1:11213"); char[] key = "key"; char[] val = "100"; int expire = 100; int flags = 0; /* Add a key */ mc_add(mc, toStringz(key), key.length, toStringz(val), val.length, expire, flags); /* Get a key, caller has to free(3) memory */ void *blah = mc_aget(mc, toStringz(key), key.length); printf("%s"\n, blah); auto myval = toString(cast(char*)blah).dup; writefln("%s", myval); free(blah); char[] key1 = "key1"; char[] key2 = "key2"; /* Perform a multi-key request */ memcache_req *req = mc_req_new(); mc_req_add(req, toStringz(key1), key1.length); mc_req_add(req, toStringz(key2), key2.length); mc_get(mc, req); /* Process the results (need a better interface to looping through results) */ /* Perform a multi-key request the easy way (this is my preferred way of getting data): */ req = mc_req_new(); auto res1 = mc_req_add(req, toStringz(key1), key1.length); auto res2 = mc_req_add(req, toStringz(key2), key2.length); mc_get(mc, req); /* Play with res1/res2 w/o any looping through req */ /* Complex multi-key get example: */ /* Grab the response object that will be used to store a given key */ char[] key3 = "key3"; memcache_res *res = mc_req_add(req, toStringz(key3), key3.length); res.size = 1024; /* Allocate our own memory a head of time (useful for loops) */ res.val = malloc(res.size); mc_res_free_on_delete(res, 1); /* Perform the get */ mc_get(mc, req); mc_res_free(req, res); /* Get stats from the whole cluster */ memcache_server_stats *s = mc_stats(mc); mc_server_stats_free(s); /* Storage commands: */ mc_add(mc, toStringz(key), key.length, toStringz(val), val.length, expire, flags); mc_replace(mc, toStringz(key), key.length, toStringz(val), val.length, expire, flags); //mc_replace(mc, key, key_len, val, bytes, expire, flags); mc_set(mc, toStringz(key), key.length, toStringz(val), val.length, expire, flags); int hold_timer = 0; /* Delete commands: */ mc_delete(mc, toStringz(key), key.length, hold_timer); /* Atomic opts: */ mc_incr(mc, toStringz(key), key.length, 10); mc_decr(mc, toStringz(key), key.length, 5); printf("%s"\n, mc_aget(mc, toStringz(key), key.length)); mc_free(mc); }