11 #include "memcached_io.h"
20 } memcached_storage_action
;
23 char *storage_op_string(memcached_storage_action verb
)
44 static inline memcached_return
memcached_send(memcached_st
*ptr
,
45 char *key
, size_t key_length
,
46 char *value
, size_t value_length
,
50 memcached_storage_action verb
)
56 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
57 unsigned int server_key
;
59 WATCHPOINT_ASSERT(!(value
== NULL
&& value_length
> 0));
60 WATCHPOINT_ASSERT(!(value
&& value_length
== 0));
63 return MEMCACHED_NO_KEY_PROVIDED
;
65 if (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
66 return MEMCACHED_NO_SERVERS
;
68 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
70 rc
= memcached_connect(ptr
, server_key
);
71 if (rc
!= MEMCACHED_SUCCESS
)
76 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
77 "%s %.*s %u %llu %zu %llu\r\n", storage_op_string(verb
),
78 (int)key_length
, key
, flags
,
79 (unsigned long long)expiration
, value_length
,
80 (unsigned long long)cas
);
82 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
83 "%s %.*s %u %llu %zu\r\n", storage_op_string(verb
),
84 (int)key_length
, key
, flags
,
85 (unsigned long long)expiration
, value_length
);
87 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
89 rc
= MEMCACHED_WRITE_FAILURE
;
94 We have to flush after sending the command. Memcached is not smart enough
95 to just keep reading from the socket :(
97 if ((sent_length
= memcached_io_write(ptr
, server_key
, buffer
, write_length
, 0)) == -1)
99 rc
= MEMCACHED_WRITE_FAILURE
;
103 if ((sent_length
= memcached_io_write(ptr
, server_key
, value
, value_length
, 0)) == -1)
105 rc
= MEMCACHED_WRITE_FAILURE
;
109 if ((ptr
->flags
& MEM_NO_BLOCK
) && verb
== SET_OP
)
114 if ((sent_length
= memcached_io_write(ptr
, server_key
, "\r\n", 2, to_write
)) == -1)
116 memcached_quit_server(ptr
, server_key
);
117 rc
= MEMCACHED_WRITE_FAILURE
;
121 if ((ptr
->flags
& MEM_NO_BLOCK
) && verb
== SET_OP
)
123 rc
= MEMCACHED_SUCCESS
;
124 memcached_server_response_increment(ptr
, server_key
);
128 rc
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, server_key
);
130 if (rc
== MEMCACHED_STORED
)
131 return MEMCACHED_SUCCESS
;
136 memcached_io_reset(ptr
, server_key
);
141 memcached_return
memcached_set(memcached_st
*ptr
, char *key
, size_t key_length
,
142 char *value
, size_t value_length
,
147 LIBMEMCACHED_MEMCACHED_SET_START();
148 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
149 expiration
, flags
, 0, SET_OP
);
150 LIBMEMCACHED_MEMCACHED_SET_END();
154 memcached_return
memcached_add(memcached_st
*ptr
,
155 char *key
, size_t key_length
,
156 char *value
, size_t value_length
,
161 LIBMEMCACHED_MEMCACHED_ADD_START();
162 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
163 expiration
, flags
, 0, ADD_OP
);
164 LIBMEMCACHED_MEMCACHED_ADD_END();
168 memcached_return
memcached_replace(memcached_st
*ptr
,
169 char *key
, size_t key_length
,
170 char *value
, size_t value_length
,
175 LIBMEMCACHED_MEMCACHED_REPLACE_START();
176 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
177 expiration
, flags
, 0, REPLACE_OP
);
178 LIBMEMCACHED_MEMCACHED_REPLACE_END();
182 memcached_return
memcached_prepend(memcached_st
*ptr
,
183 char *key
, size_t key_length
,
184 char *value
, size_t value_length
,
189 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
190 expiration
, flags
, 0, PREPEND_OP
);
194 memcached_return
memcached_append(memcached_st
*ptr
,
195 char *key
, size_t key_length
,
196 char *value
, size_t value_length
,
201 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
202 expiration
, flags
, 0, APPEND_OP
);
206 memcached_return
memcached_cas(memcached_st
*ptr
,
207 char *key
, size_t key_length
,
208 char *value
, size_t value_length
,
214 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
215 expiration
, flags
, cas
, APPEND_OP
);