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
)
22 memcached_return_t rc
;
24 unsigned int pairs_sent
;
26 for (x
= 0, pairs_sent
= 0; x
< number_of
; x
++)
28 rc
= memcached_set(memc
, pairs
[x
].key
, pairs
[x
].key_length
,
29 pairs
[x
].value
, pairs
[x
].value_length
,
31 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
32 fprintf(stderr
, "Failured on insert of %.*s\n",
33 (unsigned int)pairs
[x
].key_length
, pairs
[x
].key
);
42 Execute a memcached_get() on a set of pairs.
43 Return the number of rows retrieved.
45 unsigned int execute_get(memcached_st
*memc
, pairs_st
*pairs
, unsigned int number_of
)
47 memcached_return_t rc
;
49 unsigned int retrieved
;
52 for (retrieved
= 0,x
= 0; x
< number_of
; x
++)
57 unsigned int fetch_key
;
59 fetch_key
= (unsigned int)((unsigned int)random() % number_of
);
61 value
= memcached_get(memc
, pairs
[fetch_key
].key
, pairs
[fetch_key
].key_length
,
62 &value_length
, &flags
, &rc
);
64 if (rc
!= MEMCACHED_SUCCESS
)
65 fprintf(stderr
, "Failured on read of %.*s\n",
66 (unsigned int)pairs
[fetch_key
].key_length
, pairs
[fetch_key
].key
);
77 * Callback function to count the number of results
79 static memcached_return_t
callback_counter(const memcached_st
*ptr
,
80 memcached_result_st
*result
,
85 unsigned int *counter
= (unsigned int *)context
;
86 *counter
= *counter
+ 1;
88 return MEMCACHED_SUCCESS
;
92 * Try to run a large mget to get all of the keys
93 * @param memc memcached handle
94 * @param keys the keys to get
95 * @param key_length the length of the keys
96 * @param number_of the number of keys to try to get
97 * @return the number of keys received
99 unsigned int execute_mget(memcached_st
*memc
,
100 const char * const *keys
,
102 unsigned int number_of
)
104 unsigned int retrieved
= 0;
105 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
106 memcached_return_t rc
;
107 rc
= memcached_mget_execute(memc
, keys
, key_length
,
108 (size_t)number_of
, callbacks
, &retrieved
, 1);
110 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOTFOUND
||
111 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_END
)
113 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&retrieved
, 1);
114 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_END
)
116 fprintf(stderr
, "Failed to execute mget: %s\n",
117 memcached_strerror(memc
, rc
));
118 memcached_quit(memc
);
124 fprintf(stderr
, "Failed to execute mget: %s\n",
125 memcached_strerror(memc
, rc
));
126 memcached_quit(memc
);