2 +--------------------------------------------------------------------+
3 | libmemcached - 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 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
16 #include "mem_config.h"
19 unsigned int execute_set(memcached_st
*memc
, pairs_st
*pairs
, unsigned int number_of
) {
21 for (; count
< number_of
; ++count
) {
22 memcached_return_t rc
= memcached_set(memc
, pairs
[count
].key
, pairs
[count
].key_length
,
23 pairs
[count
].value
, pairs
[count
].value_length
, 0, 0);
24 if (memcached_failed(rc
)) {
25 fprintf(stderr
, "%s:%d Failure on %u insert (%s) of %.*s\n", __FILE__
, __LINE__
, count
,
26 memcached_last_error_message(memc
), (unsigned int) pairs
[count
].key_length
,
29 // We will try to reconnect and see if that fixes the issue
40 Execute a memcached_get() on a set of pairs.
41 Return the number of rows retrieved.
43 unsigned int execute_get(memcached_st
*memc
, pairs_st
*pairs
, unsigned int number_of
) {
45 unsigned int retrieved
;
47 for (retrieved
= 0, x
= 0; x
< number_of
; x
++) {
51 unsigned int fetch_key
= (unsigned int) ((unsigned int) random() % number_of
);
53 memcached_return_t rc
;
54 char *value
= memcached_get(memc
, pairs
[fetch_key
].key
, pairs
[fetch_key
].key_length
,
55 &value_length
, &flags
, &rc
);
57 if (memcached_failed(rc
)) {
58 fprintf(stderr
, "%s:%d Failure on read(%s) of %.*s\n", __FILE__
, __LINE__
,
59 memcached_last_error_message(memc
), (unsigned int) pairs
[fetch_key
].key_length
,
60 pairs
[fetch_key
].key
);
72 * Callback function to count the number of results
74 static memcached_return_t
callback_counter(const memcached_st
*ptr
, memcached_result_st
*result
,
78 unsigned int *counter
= (unsigned int *) context
;
79 *counter
= *counter
+ 1;
81 return MEMCACHED_SUCCESS
;
85 * Try to run a large mget to get all of the keys
86 * @param memc memcached handle
87 * @param keys the keys to get
88 * @param key_length the length of the keys
89 * @param number_of the number of keys to try to get
90 * @return the number of keys received
92 unsigned int execute_mget(memcached_st
*memc
, const char *const *keys
, size_t *key_length
,
93 unsigned int number_of
) {
94 unsigned int retrieved
= 0;
95 memcached_execute_fn callbacks
[] = {callback_counter
};
96 memcached_return_t rc
;
97 rc
= memcached_mget_execute(memc
, keys
, key_length
, (size_t) number_of
, callbacks
, &retrieved
, 1);
99 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_BUFFERED
100 || rc
== MEMCACHED_END
)
102 rc
= memcached_fetch_execute(memc
, callbacks
, (void *) &retrieved
, 1);
103 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_END
) {
104 fprintf(stderr
, "%s:%d Failed to execute mget: %s\n", __FILE__
, __LINE__
,
105 memcached_strerror(memc
, rc
));
106 memcached_quit(memc
);
110 fprintf(stderr
, "%s:%d Failed to execute mget: %s\n", __FILE__
, __LINE__
,
111 memcached_strerror(memc
, rc
));
112 memcached_quit(memc
);