3 memcached_return
memcached_delete(memcached_st
*ptr
, const char *key
, size_t key_length
,
6 return memcached_delete_by_key(ptr
, key
, key_length
,
7 key
, key_length
, expiration
);
10 static inline memcached_return
binary_delete(memcached_st
*ptr
,
11 unsigned int server_key
,
16 memcached_return
memcached_delete_by_key(memcached_st
*ptr
,
17 const char *master_key
, size_t master_key_length
,
18 const char *key
, size_t key_length
,
24 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
25 unsigned int server_key
;
27 LIBMEMCACHED_MEMCACHED_DELETE_START();
29 rc
= memcached_validate_key_length(key_length
,
30 ptr
->flags
& MEM_BINARY_PROTOCOL
);
31 unlikely (rc
!= MEMCACHED_SUCCESS
)
34 unlikely (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
35 return MEMCACHED_NO_SERVERS
;
37 server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
38 to_write
= (ptr
->flags
& MEM_BUFFER_REQUESTS
) ? 0 : 1;
39 bool no_reply
= (ptr
->flags
& MEM_NOREPLY
);
41 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
42 rc
= binary_delete(ptr
, server_key
, key
, key_length
, to_write
);
46 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
47 "delete %s%.*s %u%s\r\n",
50 (uint32_t)expiration
, no_reply
? " noreply" :"" );
52 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
53 "delete %s%.*s%s\r\n",
55 (int)key_length
, key
, no_reply
? " noreply" :"");
57 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
59 rc
= MEMCACHED_WRITE_FAILURE
;
63 if (ptr
->flags
& MEM_USE_UDP
&& !to_write
)
65 if (send_length
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
66 return MEMCACHED_WRITE_FAILURE
;
67 if (send_length
+ ptr
->hosts
[server_key
].write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
68 memcached_io_write(&ptr
->hosts
[server_key
], NULL
, 0, 1);
71 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, send_length
, to_write
);
74 if (rc
!= MEMCACHED_SUCCESS
)
77 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
))
78 rc
= MEMCACHED_BUFFERED
;
81 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
82 if (rc
== MEMCACHED_DELETED
)
83 rc
= MEMCACHED_SUCCESS
;
86 if (rc
== MEMCACHED_SUCCESS
&& ptr
->delete_trigger
)
87 ptr
->delete_trigger(ptr
, key
, key_length
);
90 LIBMEMCACHED_MEMCACHED_DELETE_END();
94 static inline memcached_return
binary_delete(memcached_st
*ptr
,
95 unsigned int server_key
,
100 protocol_binary_request_delete request
= {.bytes
= {0}};
102 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
103 if (ptr
->flags
& MEM_NOREPLY
)
104 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
106 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETE
;
107 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
108 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
109 request
.message
.header
.request
.bodylen
= htonl(key_length
);
111 if (ptr
->flags
& MEM_USE_UDP
&& !flush
)
113 size_t cmd_size
= sizeof(request
.bytes
) + key_length
;
114 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
115 return MEMCACHED_WRITE_FAILURE
;
116 if (cmd_size
+ ptr
->hosts
[server_key
].write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
117 memcached_io_write(&ptr
->hosts
[server_key
], NULL
, 0, 1);
120 if ((memcached_do(&ptr
->hosts
[server_key
], request
.bytes
,
121 sizeof(request
.bytes
), 0) != MEMCACHED_SUCCESS
) ||
122 (memcached_io_write(&ptr
->hosts
[server_key
], key
,
123 key_length
, flush
) == -1))
125 memcached_io_reset(&ptr
->hosts
[server_key
]);
126 return MEMCACHED_WRITE_FAILURE
;
129 return MEMCACHED_SUCCESS
;