f40c1a6b78c473a5145f53bfb63e50b73210b4df
[awesomized/libmemcached] / lib / memcached_flush.c
1 #include "common.h"
2
3 memcached_return memcached_flush(memcached_st *ptr, time_t expiration)
4 {
5 unsigned int x;
6 size_t send_length;
7 memcached_return rc;
8 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
9 LIBMEMCACHED_MEMCACHED_FLUSH_START();
10
11 rc= memcached_connect(ptr, 0);
12
13 if (rc == MEMCACHED_NO_SERVERS)
14 return rc;
15
16 if (rc != MEMCACHED_SUCCESS)
17 rc= MEMCACHED_SOME_ERRORS;
18
19 memset(buffer, 0, MEMCACHED_DEFAULT_COMMAND_SIZE);
20 for (x= 0; x < ptr->number_of_hosts; x++)
21 {
22 if (expiration)
23 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
24 "flush_all %llu\r\n", (unsigned long long)expiration);
25 else
26 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
27 "flush_all\r\n");
28
29 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
30 return MEMCACHED_WRITE_FAILURE;
31
32 rc= memcached_do(ptr, x, buffer, send_length, 1);
33 if (rc != MEMCACHED_SUCCESS)
34 goto error;
35
36 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, x);
37
38 if (rc != MEMCACHED_SUCCESS)
39 rc= MEMCACHED_SOME_ERRORS;
40 }
41
42 error:
43 LIBMEMCACHED_MEMCACHED_FLUSH_END();
44 return rc;
45 }