memcached 中一个命令flush_all 执行前后,使用stats查看的状态完全一样,刚开始以为是没执行成功,学习完memcached的工作原理后发现了flush_all执行完的效果是这样的:
“flush_all”命令有一个可选的数字参数。它总是执行成功,服务器会发送“OK\r\n”回应。它的效果是使已经存在的项目立即失效(缺省),或在指定的时间后。此后执行取回命令,将不会有任何内容返回(除非重新存储同样的键名)。flush_all 实际上没有立即释放项目所占用的内存,而是在随后陆续有新的项目被储存时执行。flush_all 效果具体如下:它导致所有更新时间早于flush_all所设定时间的项目,在被执行取回命令时命令被忽略。
一下是一端c语言测试memcached的一段代码:
04 | #include “libmemcached/memcached.h” |
06 | int main( int argc, char *argv[]) |
10 | memcached_server_st *servers; |
13 | memc = memcached_create(NULL); |
14 | servers = memcached_server_list_append(NULL, “localhost”, 11211, &rc); |
16 | rc = memcached_server_push(memc, servers); |
17 | memcached_server_free(servers); |
20 | char *keys[]= {“key1″, “key2″, “key3″}; |
21 | size_t key_length[]= {4, 4, 4}; |
22 | char *values[] = {“This is c first value”, “This is c second value”, “This is c third value”}; |
23 | size_t val_length[]= {21, 22, 21}; |
26 | rc = memcached_set(memc, keys[i], |
27 | key_length[i], values[i], val_length[i], ( time_t )180, |
29 | if (rc == MEMCACHED_SUCCESS) |
31 | printf (“Save key:%s data:”%s” success.n”,keys[i], values[i]); |
35 | char return_key[MEMCACHED_MAX_KEY]; |
36 | size_t return_key_length; |
38 | size_t return_value_length; |
40 | rc = memcached_mget(memc, keys, key_length, 3); |
41 | while ((return_value = memcached_fetch(memc, return_key,&return_key_length, &return_value_length, &flags, &rc))) { |
42 | if (rc == MEMCACHED_SUCCESS) |
44 | printf (“Fetch key:%s data:%sn”, return_key, return_value); |
50 | rc = memcached_set(memc, keys[i], key_length[i], |
51 | values[i], val_length[i], ( time_t )180, (uint32_t)0); |
52 | rc = memcached_delete(memc, keys[i], |
53 | key_length[i], ( time_t )0); |
54 | if (rc == MEMCACHED_SUCCESS) |
56 | printf (“Delete %s successn”, keys[i], values[i]); |