memcached flush_al的作用
memcached 中一个命令flush_all 执行前后,使用stats查看的状态完全一样,刚开始以为是没执行成功,学习完memcached的工作原理后发现了flush_all执行完的效果是这样的:
“flush_all”命令有一个可选的数字参数。它总是执行成功,服务器会发送“OK\r\n”回应。它的效果是使已经存在的项目立即失效(缺省),或在指定的时间后。此后执行取回命令,将不会有任何内容返回(除非重新存储同样的键名)。flush_all 实际上没有立即释放项目所占用的内存,而是在随后陆续有新的项目被储存时执行。flush_all 效果具体如下:它导致所有更新时间早于flush_all所设定时间的项目,在被执行取回命令时命令被忽略。
一下是一端c语言测试memcached的一段代码:
01
#include “stdio.h”
02
#include “stdlib.h”
03
#include “string.h”
04
#include “libmemcached/memcached.h”
05
//gcc -o cc cc.c -L /usr/local/lib -lmemcached
06
int
main(
int
argc,
char
*argv[])
07
{
08
memcached_st *memc;
09
memcached_return rc;
10
memcached_server_st *servers;
11
char
value[8191];
12
//connect multi server
13
memc = memcached_create(NULL);
14
servers = memcached_server_list_append(NULL, “localhost”, 11211, &rc);
15
//servers = memcached_server_list_append(servers, “localhost”, 11212, &rc);
16
rc = memcached_server_push(memc, servers);
17
memcached_server_free(servers);
18
//Save multi data
19
size_t
i;
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};
24
for
(i=0; i <3; i++)
25
{
26
rc = memcached_set(memc, keys[i],
27
key_length[i], values[i], val_length[i], (
time_t
)180,
28
(uint32_t)0);
29
if
(rc == MEMCACHED_SUCCESS)
30
{
31
printf
(“Save key:%s data:”%s” success.n”,keys[i], values[i]);
32
}
33
}
34
35
char
return_key[MEMCACHED_MAX_KEY];
36
size_t
return_key_length;
37
char
*return_value;
38
size_t
return_value_length;
39
uint32_t flags;
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)
43
{
44
printf
(“Fetch key:%s data:%sn”, return_key, return_value);
45
}
46
}
47
//Delete multi data
48
for
(i=0; i <3; i++)
49
{
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)
55
{
56
printf
(“Delete %s successn”, keys[i], values[i]);
57
}
58
}
59
//free
60
memcached_free(memc);
61
return
0;
62
}