0c36aaebb172b40fcb6c8ed643b109db02aed602
[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 char to_write;
7 size_t send_length;
8 memcached_return rc;
9 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
10 unsigned int server_key;
11
12 LIBMEMCACHED_MEMCACHED_DELETE_START();
13
14 if (key_length == 0)
15 return MEMCACHED_NO_KEY_PROVIDED;
16
17 if (ptr->hosts == NULL || ptr->number_of_hosts == 0)
18 return MEMCACHED_NO_SERVERS;
19
20 server_key= memcached_generate_hash(ptr, key, key_length);
21
22 if (expiration)
23 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
24 "delete %.*s %llu\r\n", (int)key_length, key,
25 (unsigned long long)expiration);
26 else
27 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
28 "delete %.*s\r\n", (int)key_length, key);
29
30 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
31 {
32 rc= MEMCACHED_WRITE_FAILURE;
33 goto error;
34 }
35
36 if ((ptr->flags & MEM_NO_BLOCK))
37 to_write= 0;
38 else
39 to_write= 1;
40
41 rc= memcached_do(ptr, server_key, buffer, send_length, to_write);
42 if (rc != MEMCACHED_SUCCESS)
43 goto error;
44
45 if ((ptr->flags & MEM_NO_BLOCK))
46 {
47 rc= MEMCACHED_SUCCESS;
48 memcached_server_response_increment(ptr, server_key);
49 }
50 else
51 {
52 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
53 if (rc == MEMCACHED_DELETED)
54 rc= MEMCACHED_SUCCESS;
55 }
56
57 error:
58 LIBMEMCACHED_MEMCACHED_DELETE_END();
59 return rc;
60 }