11 #include "memcached_io.h"
17 } memcached_storage_action
;
20 #define storage_op_string(A) A == SET_OP ? "set" : ( A == REPLACE_OP ? "replace" : "add")
22 static memcached_return
memcached_send(memcached_st
*ptr
,
23 char *key
, size_t key_length
,
24 char *value
, size_t value_length
,
27 memcached_storage_action verb
)
32 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
33 unsigned int server_key
;
38 /* Leaving this assert in since only a library fubar could blow this */
39 if (ptr
->write_buffer_offset
!= 0)
41 WATCHPOINT_NUMBER(ptr
->write_buffer_offset
);
44 assert(ptr
->write_buffer_offset
== 0);
46 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
48 rc
= memcached_connect(ptr
, server_key
);
49 if (rc
!= MEMCACHED_SUCCESS
)
52 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
53 "%s %.*s %x %llu %zu\r\n", storage_op_string(verb
),
54 (int)key_length
, key
, flags
,
55 (unsigned long long)expiration
, value_length
);
56 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
58 rc
= MEMCACHED_WRITE_FAILURE
;
63 We have to flush after sending the command. Memcached is not smart enough
64 to just keep reading from the socket :(
66 if ((sent_length
= memcached_io_write(ptr
, server_key
, buffer
, write_length
, 0)) == -1)
68 rc
= MEMCACHED_WRITE_FAILURE
;
72 if ((sent_length
= memcached_io_write(ptr
, server_key
, value
, value_length
, 0)) == -1)
74 rc
= MEMCACHED_WRITE_FAILURE
;
78 if ((sent_length
= memcached_io_write(ptr
, server_key
, "\r\n", 2, 1)) == -1)
80 memcached_quit_server(ptr
, server_key
);
81 rc
= MEMCACHED_WRITE_FAILURE
;
85 if ((ptr
->flags
& MEM_NO_BLOCK
) && verb
== SET_OP
)
87 rc
= MEMCACHED_SUCCESS
;
88 memcached_server_response_increment(ptr
, server_key
);
92 rc
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, server_key
);
95 if (rc
== MEMCACHED_STORED
)
96 return MEMCACHED_SUCCESS
;
101 memcached_io_reset(ptr
, server_key
);
106 memcached_return
memcached_set(memcached_st
*ptr
, char *key
, size_t key_length
,
107 char *value
, size_t value_length
,
112 LIBMEMCACHED_MEMCACHED_SET_START();
113 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
114 expiration
, flags
, SET_OP
);
115 LIBMEMCACHED_MEMCACHED_SET_END();
119 memcached_return
memcached_add(memcached_st
*ptr
, char *key
, size_t key_length
,
120 char *value
, size_t value_length
,
125 LIBMEMCACHED_MEMCACHED_ADD_START();
126 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
127 expiration
, flags
, ADD_OP
);
128 LIBMEMCACHED_MEMCACHED_ADD_END();
132 memcached_return
memcached_replace(memcached_st
*ptr
, char *key
, size_t key_length
,
133 char *value
, size_t value_length
,
138 LIBMEMCACHED_MEMCACHED_REPLACE_START();
139 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
140 expiration
, flags
, REPLACE_OP
);
141 LIBMEMCACHED_MEMCACHED_REPLACE_END();