11 #include "memcached_io.h"
13 static memcached_return
memcached_send(memcached_st
*ptr
,
14 char *key
, size_t key_length
,
15 char *value
, size_t value_length
,
23 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
24 unsigned int server_key
;
29 memset(buffer
, 0, MEMCACHED_DEFAULT_COMMAND_SIZE
);
31 rc
= memcached_connect(ptr
);
32 if (rc
!= MEMCACHED_SUCCESS
)
35 /* Leaveing this assert in since only a library fubar could blow this */
36 assert(ptr
->write_buffer_offset
== 0);
38 server_key
= memcached_generate_hash(key
, key_length
) % ptr
->number_of_hosts
;
40 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
41 "%s %.*s %x %llu %zu\r\n", verb
,
42 (int)key_length
, key
, flags
,
43 (unsigned long long)expiration
, value_length
);
44 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
46 rc
= MEMCACHED_WRITE_FAILURE
;
50 if ((sent_length
= memcached_io_write(ptr
, server_key
, buffer
, write_length
)) == -1)
52 rc
= MEMCACHED_WRITE_FAILURE
;
57 We have to flush after sending the command. Memcached is not smart enough
58 to just keep reading from the socket :(
60 if ((sent_length
= memcached_io_flush(ptr
, server_key
)) == -1)
61 return MEMCACHED_WRITE_FAILURE
;
63 if ((sent_length
= memcached_io_write(ptr
, server_key
, value
, value_length
)) == -1)
65 rc
= MEMCACHED_WRITE_FAILURE
;
69 if ((sent_length
= memcached_io_write(ptr
, server_key
, "\r\n", 2)) == -1)
71 rc
= MEMCACHED_WRITE_FAILURE
;
75 if ((sent_length
= memcached_io_flush(ptr
, server_key
)) == -1)
76 return MEMCACHED_WRITE_FAILURE
;
78 rc
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, server_key
);
80 if (rc
== MEMCACHED_STORED
)
81 return MEMCACHED_SUCCESS
;
86 memcached_io_reset(ptr
, server_key
);
91 memcached_return
memcached_set(memcached_st
*ptr
, char *key
, size_t key_length
,
92 char *value
, size_t value_length
,
97 LIBMEMCACHED_MEMCACHED_SET_START();
98 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
99 expiration
, flags
, "set");
100 LIBMEMCACHED_MEMCACHED_SET_END();
104 memcached_return
memcached_add(memcached_st
*ptr
, char *key
, size_t key_length
,
105 char *value
, size_t value_length
,
110 LIBMEMCACHED_MEMCACHED_ADD_START();
111 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
112 expiration
, flags
, "add");
113 LIBMEMCACHED_MEMCACHED_ADD_END();
117 memcached_return
memcached_replace(memcached_st
*ptr
, char *key
, size_t key_length
,
118 char *value
, size_t value_length
,
123 LIBMEMCACHED_MEMCACHED_REPLACE_START();
124 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
125 expiration
, flags
, "replace");
126 LIBMEMCACHED_MEMCACHED_REPLACE_END();