Rewrote internal connect function to do by demand (aka only open up
[awesomized/libmemcached] / lib / memcached_delete.c
1 #include "common.h"
2
3 memcached_return memcached_delete(memcached_st *ptr, char *key, size_t key_length,
4 time_t expiration)
5 {
6 size_t send_length, sent_length;
7 memcached_return rc;
8 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
9 unsigned int server_key;
10
11 LIBMEMCACHED_MEMCACHED_DELETE_START();
12
13 server_key= memcached_generate_hash(ptr, key, key_length);
14
15 if ((rc= memcached_connect(ptr, server_key)) != MEMCACHED_SUCCESS)
16 return rc;
17
18
19 if (expiration)
20 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
21 "delete %.*s %llu\r\n", (int)key_length, key,
22 (unsigned long long)expiration);
23 else
24 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
25 "delete %.*s\r\n", (int)key_length, key);
26
27 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
28 {
29 rc= MEMCACHED_WRITE_FAILURE;
30 goto error;
31 }
32
33 sent_length= memcached_io_write(ptr, server_key, buffer, send_length, 1);
34
35 if (sent_length == -1 || sent_length != send_length)
36 {
37 rc= MEMCACHED_WRITE_FAILURE;
38 goto error;
39 }
40
41 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
42
43 if (rc == MEMCACHED_DELETED)
44 rc= MEMCACHED_SUCCESS;
45
46 LIBMEMCACHED_MEMCACHED_DELETE_END();
47
48 error:
49 return rc;
50 }