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 while (ptr
->cursor_server
< ptr
->number_of_hosts
)
19 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
21 if (memcached_server_response_count(&ptr
->hosts
[ptr
->cursor_server
]) == 0)
27 *error
= memcached_response(&ptr
->hosts
[ptr
->cursor_server
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, result_buffer
);
29 if (*error
== MEMCACHED_END
) /* END means that we move on to the next */
31 memcached_server_response_reset(&ptr
->hosts
[ptr
->cursor_server
]);
35 else if (*error
== MEMCACHED_SUCCESS
)
37 *value_length
= memcached_string_length(&result_buffer
->value
);
41 strncpy(key
, result_buffer
->key
, result_buffer
->key_length
);
42 *key_length
= result_buffer
->key_length
;
45 if (result_buffer
->flags
)
46 *flags
= result_buffer
->flags
;
50 return memcached_string_c_copy(&result_buffer
->value
);
59 ptr
->cursor_server
= 0;
64 memcached_result_st
*memcached_fetch_result(memcached_st
*ptr
,
65 memcached_result_st
*result
,
66 memcached_return
*error
)
68 memcached_server_st
*server
;
70 if (ptr
->flags
& MEM_USE_UDP
)
72 *error
= MEMCACHED_NOT_SUPPORTED
;
77 if ((result
= memcached_result_create(ptr
, NULL
)) == NULL
)
80 while ((server
= memcached_io_get_readable_server(ptr
)) != NULL
) {
81 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
82 *error
= memcached_response(server
, buffer
, sizeof(buffer
), result
);
84 if (*error
== MEMCACHED_SUCCESS
)
86 else if (*error
== MEMCACHED_END
)
87 memcached_server_response_reset(server
);
92 /* We have completed reading data */
93 if (result
->is_allocated
)
94 memcached_result_free(result
);
96 memcached_string_reset(&result
->value
);