2 #include "memcached_io.h"
4 char *memcached_fetch(memcached_st
*ptr
, char *key
, size_t *key_length
,
7 memcached_return
*error
)
9 memcached_result_st
*result_buffer
= &ptr
->result
;
11 if (ptr
->flags
& MEM_USE_UDP
)
13 *error
= MEMCACHED_NOT_SUPPORTED
;
17 result_buffer
= memcached_fetch_result(ptr
, result_buffer
, error
);
19 if (*error
!= MEMCACHED_SUCCESS
|| result_buffer
== NULL
)
25 *value_length
= memcached_string_length(&result_buffer
->value
);
29 strncpy(key
, result_buffer
->key
, result_buffer
->key_length
);
30 *key_length
= result_buffer
->key_length
;
33 if (result_buffer
->flags
)
34 *flags
= result_buffer
->flags
;
38 return memcached_string_c_copy(&result_buffer
->value
);
41 memcached_result_st
*memcached_fetch_result(memcached_st
*ptr
,
42 memcached_result_st
*result
,
43 memcached_return
*error
)
45 memcached_server_st
*server
;
47 if (ptr
->flags
& MEM_USE_UDP
)
49 *error
= MEMCACHED_NOT_SUPPORTED
;
54 if ((result
= memcached_result_create(ptr
, NULL
)) == NULL
)
57 while ((server
= memcached_io_get_readable_server(ptr
)) != NULL
) {
58 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
59 *error
= memcached_response(server
, buffer
, sizeof(buffer
), result
);
61 if (*error
== MEMCACHED_SUCCESS
)
63 else if (*error
== MEMCACHED_END
)
64 memcached_server_response_reset(server
);
69 /* We have completed reading data */
70 if (result
->is_allocated
)
71 memcached_result_free(result
);
73 memcached_string_reset(&result
->value
);
78 memcached_return
memcached_fetch_execute(memcached_st
*ptr
,
79 memcached_execute_function
*callback
,
81 unsigned int number_of_callbacks
)
83 memcached_result_st
*result
= &ptr
->result
;
84 memcached_return rc
= MEMCACHED_FAILURE
;
87 while ((result
= memcached_fetch_result(ptr
, result
, &rc
)) != NULL
) {
88 if (rc
== MEMCACHED_SUCCESS
)
90 for (x
= 0; x
< number_of_callbacks
; x
++)
92 rc
= (*callback
[x
])(ptr
, result
, context
);
93 if (rc
!= MEMCACHED_SUCCESS
)