+
+/**
+ * Callback function to count the number of results
+ */
+static memcached_return_t callback_counter(const memcached_st *ptr,
+ memcached_result_st *result,
+ void *context)
+{
+ (void)ptr;
+ (void)result;
+ unsigned int *counter= (unsigned int *)context;
+ *counter= *counter + 1;
+
+ return MEMCACHED_SUCCESS;
+}
+
+/**
+ * Try to run a large mget to get all of the keys
+ * @param memc memcached handle
+ * @param keys the keys to get
+ * @param key_length the length of the keys
+ * @param number_of the number of keys to try to get
+ * @return the number of keys received
+ */
+unsigned int execute_mget(memcached_st *memc,
+ const char * const *keys,
+ size_t *key_length,
+ unsigned int number_of)
+{
+ unsigned int retrieved= 0;
+ memcached_execute_fn callbacks[1]= { [0]= &callback_counter };
+ memcached_return_t rc;
+ rc= memcached_mget_execute(memc, keys, key_length,
+ (size_t)number_of, callbacks, &retrieved, 1);
+
+ likely (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_NOTFOUND ||
+ rc == MEMCACHED_BUFFERED || rc == MEMCACHED_END)
+ {
+ rc= memcached_fetch_execute(memc, callbacks, (void *)&retrieved, 1);
+ unlikely (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_NOTFOUND &&
+ rc != MEMCACHED_END)
+ {
+ fprintf(stderr, "Failed to execute mget: %s\n",
+ memcached_strerror(memc, rc));
+ memcached_quit(memc);
+ return 0;
+ }
+ }
+ else
+ {
+ fprintf(stderr, "Failed to execute mget: %s\n",
+ memcached_strerror(memc, rc));
+ memcached_quit(memc);
+ return 0;
+ }
+
+ return retrieved;
+}