memcached函数的学习程序

本文展示了如何将Libmemcached的C语言示例程序转换为D语言,并提供了详细的编译步骤及程序代码。通过本示例可以了解如何在D语言中实现缓存操作,包括增加、获取、删除等基本功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

尝试把 libmemcached的示例程序改成D语言的。
源程序 和示例程序可以从 这里下载:
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下的示例代码吧。
源程序看起来比较痛苦。

[code]
/**
* 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);
}
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值