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
&& rc
!= MEMCACHED_BUFFERED
)
32 fprintf(stderr
, "Failured on insert of %.*s\n",
33 (unsigned int)pairs
[x
].key_length
, pairs
[x
].key
);
45 Execute a memcached_get() on a set of pairs.
46 Return the number of rows retrieved.
48 unsigned int execute_get(memcached_st
*memc
, pairs_st
*pairs
, unsigned int number_of
)
50 memcached_return_t rc
;
52 unsigned int retrieved
;
55 for (retrieved
= 0,x
= 0; x
< number_of
; x
++)
60 unsigned int fetch_key
;
62 fetch_key
= (unsigned int)((unsigned int)random() % number_of
);
64 value
= memcached_get(memc
, pairs
[fetch_key
].key
, pairs
[fetch_key
].key_length
,
65 &value_length
, &flags
, &rc
);
67 if (rc
!= MEMCACHED_SUCCESS
)
68 fprintf(stderr
, "Failured on read of %.*s\n",
69 (unsigned int)pairs
[fetch_key
].key_length
, pairs
[fetch_key
].key
);
80 * Callback function to count the number of results
82 static memcached_return_t
callback_counter(const memcached_st
*ptr
,
83 memcached_result_st
*result
,
88 unsigned int *counter
= (unsigned int *)context
;
89 *counter
= *counter
+ 1;
91 return MEMCACHED_SUCCESS
;
95 * Try to run a large mget to get all of the keys
96 * @param memc memcached handle
97 * @param keys the keys to get
98 * @param key_length the length of the keys
99 * @param number_of the number of keys to try to get
100 * @return the number of keys received
102 unsigned int execute_mget(memcached_st
*memc
,
103 const char * const *keys
,
105 unsigned int number_of
)
107 unsigned int retrieved
= 0;
108 memcached_execute_fn callbacks
[]= { callback_counter
};
109 memcached_return_t rc
;
110 rc
= memcached_mget_execute(memc
, keys
, key_length
,
111 (size_t)number_of
, callbacks
, &retrieved
, 1);
113 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOTFOUND
||
114 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_END
)
116 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&retrieved
, 1);
117 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_END
)
119 fprintf(stderr
, "Failed to execute mget: %s\n",
120 memcached_strerror(memc
, rc
));
121 memcached_quit(memc
);
127 fprintf(stderr
, "Failed to execute mget: %s\n",
128 memcached_strerror(memc
, rc
));
129 memcached_quit(memc
);