2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
13 Execute a memcached_set() a set of pairs.
14 Return the number of rows set.
20 unsigned int execute_set(memcached_st
*memc
, pairs_st
*pairs
, unsigned int number_of
)
23 unsigned int pairs_sent
;
25 for (x
= 0, pairs_sent
= 0; x
< number_of
; x
++)
27 memcached_return_t rc
= memcached_set(memc
, pairs
[x
].key
, pairs
[x
].key_length
,
28 pairs
[x
].value
, pairs
[x
].value_length
,
30 if (rc
!= MEMCACHED_SUCCESS
and rc
!= MEMCACHED_BUFFERED
)
32 fprintf(stderr
, "Failure on insert (%s) of %.*s\n",
33 memcached_last_error_message(memc
),
34 (unsigned int)pairs
[x
].key_length
, pairs
[x
].key
);
46 Execute a memcached_get() on a set of pairs.
47 Return the number of rows retrieved.
49 unsigned int execute_get(memcached_st
*memc
, pairs_st
*pairs
, unsigned int number_of
)
51 memcached_return_t rc
;
53 unsigned int retrieved
;
56 for (retrieved
= 0,x
= 0; x
< number_of
; x
++)
61 unsigned int fetch_key
;
63 fetch_key
= (unsigned int)((unsigned int)random() % number_of
);
65 value
= memcached_get(memc
, pairs
[fetch_key
].key
, pairs
[fetch_key
].key_length
,
66 &value_length
, &flags
, &rc
);
68 if (rc
!= MEMCACHED_SUCCESS
)
70 fprintf(stderr
, "Failure on read(%s) of %.*s\n",
71 memcached_last_error_message(memc
),
72 (unsigned int)pairs
[fetch_key
].key_length
, pairs
[fetch_key
].key
);
86 * Callback function to count the number of results
88 static memcached_return_t
callback_counter(const memcached_st
*ptr
,
89 memcached_result_st
*result
,
94 unsigned int *counter
= (unsigned int *)context
;
95 *counter
= *counter
+ 1;
97 return MEMCACHED_SUCCESS
;
101 * Try to run a large mget to get all of the keys
102 * @param memc memcached handle
103 * @param keys the keys to get
104 * @param key_length the length of the keys
105 * @param number_of the number of keys to try to get
106 * @return the number of keys received
108 unsigned int execute_mget(memcached_st
*memc
,
109 const char * const *keys
,
111 unsigned int number_of
)
113 unsigned int retrieved
= 0;
114 memcached_execute_fn callbacks
[]= { callback_counter
};
115 memcached_return_t rc
;
116 rc
= memcached_mget_execute(memc
, keys
, key_length
,
117 (size_t)number_of
, callbacks
, &retrieved
, 1);
119 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOTFOUND
||
120 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_END
)
122 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&retrieved
, 1);
123 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_END
)
125 fprintf(stderr
, "Failed to execute mget: %s\n",
126 memcached_strerror(memc
, rc
));
127 memcached_quit(memc
);
133 fprintf(stderr
, "Failed to execute mget: %s\n",
134 memcached_strerror(memc
, rc
));
135 memcached_quit(memc
);