2 +--------------------------------------------------------------------+
3 | libmemcached-awesome - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020-2021 Michael Wallner https://awesome.co/ |
13 +--------------------------------------------------------------------+
16 #include "libmemcached/common.h"
18 static memcached_return_t
memcached_flush_binary(Memcached
*ptr
, time_t expiration
,
20 protocol_binary_request_flush request
= {};
22 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_FLUSH
;
23 request
.message
.header
.request
.extlen
= 4;
24 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
25 request
.message
.header
.request
.bodylen
= htonl(request
.message
.header
.request
.extlen
);
26 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
28 memcached_return_t rc
= MEMCACHED_SUCCESS
;
30 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++) {
31 memcached_instance_st
*instance
= memcached_instance_fetch(ptr
, x
);
32 initialize_binary_request(instance
, request
.message
.header
);
35 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_FLUSH
;
37 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_FLUSHQ
;
40 libmemcached_io_vector_st vector
[] = {{NULL
, 0}, {request
.bytes
, sizeof(request
.bytes
)}};
42 memcached_return_t rrc
;
43 if (memcached_failed(rrc
= memcached_vdo(instance
, vector
, 2, true))) {
44 if (instance
->error_messages
== NULL
or instance
->root
->error_messages
== NULL
) {
45 memcached_set_error(*instance
, rrc
, MEMCACHED_AT
);
47 rc
= MEMCACHED_SOME_ERRORS
;
51 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++) {
52 memcached_instance_st
*instance
= memcached_instance_fetch(ptr
, x
);
54 if (instance
->response_count() > 0) {
55 (void) memcached_response(instance
, NULL
, 0, NULL
);
62 static memcached_return_t
memcached_flush_textual(Memcached
*ptr
, time_t expiration
,
64 char buffer
[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH
+ 1 + 1];
67 send_length
= snprintf(buffer
, sizeof(buffer
), "%lld", (long long) expiration
);
70 if (size_t(send_length
) >= sizeof(buffer
) or send_length
< 0) {
71 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
72 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
75 memcached_return_t rc
= MEMCACHED_SUCCESS
;
76 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++) {
77 memcached_instance_st
*instance
= memcached_instance_fetch(ptr
, x
);
79 libmemcached_io_vector_st vector
[] = {
81 {memcached_literal_param("flush_all ")},
82 {buffer
, size_t(send_length
)},
83 {" noreply", reply
? 0 : memcached_literal_param_size(" noreply")},
84 {memcached_literal_param("\r\n")}};
86 memcached_return_t rrc
= memcached_vdo(instance
, vector
, 5, true);
87 if (memcached_success(rrc
) and reply
== true) {
88 char response_buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
89 rrc
= memcached_response(instance
, response_buffer
, sizeof(response_buffer
), NULL
);
92 if (memcached_failed(rrc
)) {
93 // If an error has already been reported, then don't add to it
94 if (instance
->error_messages
== NULL
or instance
->root
->error_messages
== NULL
) {
95 memcached_set_error(*instance
, rrc
, MEMCACHED_AT
);
97 rc
= MEMCACHED_SOME_ERRORS
;
104 memcached_return_t
memcached_flush(memcached_st
*shell
, time_t expiration
) {
105 Memcached
*ptr
= memcached2Memcached(shell
);
106 memcached_return_t rc
;
107 if (memcached_failed(rc
= initialize_query(ptr
, true))) {
111 bool reply
= memcached_is_replying(ptr
);
113 LIBMEMCACHED_MEMCACHED_FLUSH_START();
114 if (memcached_is_binary(ptr
)) {
115 rc
= memcached_flush_binary(ptr
, expiration
, reply
);
117 rc
= memcached_flush_textual(ptr
, expiration
, reply
);
119 LIBMEMCACHED_MEMCACHED_FLUSH_END();