ecd7d8dc1b203cad509f1616939981b1b0a1a48a
[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 rc= memcached_connect(ptr);
14
15 if (rc != MEMCACHED_SUCCESS)
16 return rc;
17
18 server_key= memcached_generate_hash(key, key_length) % ptr->number_of_hosts;
19
20 if (expiration)
21 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
22 "delete %.*s %llu\r\n", (int)key_length, key,
23 (unsigned long long)expiration);
24 else
25 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
26 "delete %.*s\r\n", (int)key_length, key);
27
28 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
29 {
30 rc= MEMCACHED_WRITE_FAILURE;
31 goto error;
32 }
33
34 sent_length= send(ptr->hosts[server_key].fd, buffer, send_length, 0);
35
36 if (sent_length == -1 || sent_length != send_length)
37 {
38 rc= MEMCACHED_WRITE_FAILURE;
39 goto error;
40 }
41
42 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
43
44 if (rc == MEMCACHED_DELETED)
45 rc= MEMCACHED_SUCCESS;
46
47 LIBMEMCACHED_MEMCACHED_DELETE_END();
48
49 error:
50 return rc;
51 }