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.
17 #include "libmemcached/common.h"
21 unsigned int execute_set(memcached_st
*memc
, pairs_st
*pairs
, unsigned int number_of
)
23 memcached_return_t rc
;
25 unsigned int pairs_sent
;
27 for (x
= 0, pairs_sent
= 0; x
< number_of
; x
++)
29 rc
= memcached_set(memc
, pairs
[x
].key
, pairs
[x
].key_length
,
30 pairs
[x
].value
, pairs
[x
].value_length
,
32 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
33 fprintf(stderr
, "Failured on insert of %.*s\n",
34 (unsigned int)pairs
[x
].key_length
, pairs
[x
].key
);
43 Execute a memcached_get() on a set of pairs.
44 Return the number of rows retrieved.
46 unsigned int execute_get(memcached_st
*memc
, pairs_st
*pairs
, unsigned int number_of
)
48 memcached_return_t rc
;
50 unsigned int retrieved
;
53 for (retrieved
= 0,x
= 0; x
< number_of
; x
++)
58 unsigned int fetch_key
;
60 fetch_key
= (unsigned int)((unsigned int)random() % number_of
);
62 value
= memcached_get(memc
, pairs
[fetch_key
].key
, pairs
[fetch_key
].key_length
,
63 &value_length
, &flags
, &rc
);
65 if (rc
!= MEMCACHED_SUCCESS
)
66 fprintf(stderr
, "Failured on read of %.*s\n",
67 (unsigned int)pairs
[fetch_key
].key_length
, pairs
[fetch_key
].key
);
78 * Callback function to count the number of results
80 static memcached_return_t
callback_counter(memcached_st
*ptr
,
81 memcached_result_st
*result
,
86 unsigned int *counter
= (unsigned int *)context
;
87 *counter
= *counter
+ 1;
89 return MEMCACHED_SUCCESS
;
93 * Try to run a large mget to get all of the keys
94 * @param memc memcached handle
95 * @param keys the keys to get
96 * @param key_length the length of the keys
97 * @param number_of the number of keys to try to get
98 * @return the number of keys received
100 unsigned int execute_mget(memcached_st
*memc
,
101 const char * const *keys
,
103 unsigned int number_of
)
105 unsigned int retrieved
= 0;
106 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
107 memcached_return_t rc
;
108 rc
= memcached_mget_execute(memc
, keys
, key_length
,
109 (size_t)number_of
, callbacks
, &retrieved
, 1);
111 likely (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOTFOUND
||
112 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_END
)
114 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&retrieved
, 1);
115 unlikely (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_NOTFOUND
&&
118 fprintf(stderr
, "Failed to execute mget: %s\n",
119 memcached_strerror(memc
, rc
));
120 memcached_quit(memc
);
126 fprintf(stderr
, "Failed to execute mget: %s\n",
127 memcached_strerror(memc
, rc
));
128 memcached_quit(memc
);