11 #include "memcached_io.h"
20 } memcached_storage_action
;
23 static char *storage_op_string(memcached_storage_action verb
)
44 static inline memcached_return
memcached_send(memcached_st
*ptr
,
45 char *master_key
, size_t master_key_length
,
46 char *key
, size_t key_length
,
47 char *value
, size_t value_length
,
51 memcached_storage_action verb
)
56 memcached_return rc
[MEMCACHED_MAX_REPLICAS
];
57 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
58 unsigned int server_key
;
61 unlikely (key_length
== 0)
62 return MEMCACHED_NO_KEY_PROVIDED
;
64 unlikely (ptr
->number_of_hosts
== 0)
65 return MEMCACHED_NO_SERVERS
;
67 if ((ptr
->flags
& MEM_VERIFY_KEY
) && (memcachd_key_test(&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
68 return MEMCACHED_BAD_KEY_PROVIDED
;
70 server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
73 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
74 "%s %.*s %u %llu %zu %llu\r\n", storage_op_string(verb
),
75 (int)key_length
, key
, flags
,
76 (unsigned long long)expiration
, value_length
,
77 (unsigned long long)cas
);
79 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
80 "%s %.*s %u %llu %zu\r\n", storage_op_string(verb
),
81 (int)key_length
, key
, flags
,
82 (unsigned long long)expiration
, value_length
);
84 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
85 return MEMCACHED_WRITE_FAILURE
;
87 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
)
94 rc
[replicas
]= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
96 if (rc
[replicas
] != MEMCACHED_SUCCESS
)
99 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0)) == -1)
101 rc
[replicas
]= MEMCACHED_WRITE_FAILURE
;
105 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
)) == -1)
107 rc
[replicas
]= MEMCACHED_WRITE_FAILURE
;
112 return MEMCACHED_BUFFERED
;
114 rc
[replicas
]= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
116 /* On error we just jump to the next potential server */
118 if (replicas
> 1 && ptr
->distribution
== MEMCACHED_DISTRIBUTION_CONSISTENT
)
120 if (server_key
== (ptr
->number_of_hosts
- 1))
125 } while ((++replicas
) < ptr
->number_of_replicas
);
127 /* As long as one object gets stored, we count this as a success */
130 if (rc
[replicas
] == MEMCACHED_STORED
)
131 return MEMCACHED_SUCCESS
;
137 memcached_return
memcached_set(memcached_st
*ptr
, char *key
, size_t key_length
,
138 char *value
, size_t value_length
,
143 LIBMEMCACHED_MEMCACHED_SET_START();
144 rc
= memcached_send(ptr
, key
, key_length
,
145 key
, key_length
, value
, value_length
,
146 expiration
, flags
, 0, SET_OP
);
147 LIBMEMCACHED_MEMCACHED_SET_END();
151 memcached_return
memcached_add(memcached_st
*ptr
,
152 char *key
, size_t key_length
,
153 char *value
, size_t value_length
,
158 LIBMEMCACHED_MEMCACHED_ADD_START();
159 rc
= memcached_send(ptr
, key
, key_length
,
160 key
, key_length
, value
, value_length
,
161 expiration
, flags
, 0, ADD_OP
);
162 LIBMEMCACHED_MEMCACHED_ADD_END();
166 memcached_return
memcached_replace(memcached_st
*ptr
,
167 char *key
, size_t key_length
,
168 char *value
, size_t value_length
,
173 LIBMEMCACHED_MEMCACHED_REPLACE_START();
174 rc
= memcached_send(ptr
, key
, key_length
,
175 key
, key_length
, value
, value_length
,
176 expiration
, flags
, 0, REPLACE_OP
);
177 LIBMEMCACHED_MEMCACHED_REPLACE_END();
181 memcached_return
memcached_prepend(memcached_st
*ptr
,
182 char *key
, size_t key_length
,
183 char *value
, size_t value_length
,
188 rc
= memcached_send(ptr
, key
, key_length
,
189 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
,
202 key
, key_length
, value
, value_length
,
203 expiration
, flags
, 0, APPEND_OP
);
207 memcached_return
memcached_cas(memcached_st
*ptr
,
208 char *key
, size_t key_length
,
209 char *value
, size_t value_length
,
215 rc
= memcached_send(ptr
, key
, key_length
,
216 key
, key_length
, value
, value_length
,
217 expiration
, flags
, cas
, CAS_OP
);
221 memcached_return
memcached_set_by_key(memcached_st
*ptr
,
222 char *master_key
, size_t master_key_length
,
223 char *key
, size_t key_length
,
224 char *value
, size_t value_length
,
229 LIBMEMCACHED_MEMCACHED_SET_START();
230 rc
= memcached_send(ptr
, key
, key_length
,
231 key
, key_length
, value
, value_length
,
232 expiration
, flags
, 0, SET_OP
);
233 LIBMEMCACHED_MEMCACHED_SET_END();
237 memcached_return
memcached_add_by_key(memcached_st
*ptr
,
238 char *master_key
, size_t master_key_length
,
239 char *key
, size_t key_length
,
240 char *value
, size_t value_length
,
245 LIBMEMCACHED_MEMCACHED_ADD_START();
246 rc
= memcached_send(ptr
, key
, key_length
,
247 key
, key_length
, value
, value_length
,
248 expiration
, flags
, 0, ADD_OP
);
249 LIBMEMCACHED_MEMCACHED_ADD_END();
253 memcached_return
memcached_replace_by_key(memcached_st
*ptr
,
254 char *master_key
, size_t master_key_length
,
255 char *key
, size_t key_length
,
256 char *value
, size_t value_length
,
261 LIBMEMCACHED_MEMCACHED_REPLACE_START();
262 rc
= memcached_send(ptr
, key
, key_length
,
263 key
, key_length
, value
, value_length
,
264 expiration
, flags
, 0, REPLACE_OP
);
265 LIBMEMCACHED_MEMCACHED_REPLACE_END();
269 memcached_return
memcached_prepend_by_key(memcached_st
*ptr
,
270 char *master_key
, size_t master_key_length
,
271 char *key
, size_t key_length
,
272 char *value
, size_t value_length
,
277 rc
= memcached_send(ptr
, key
, key_length
,
278 key
, key_length
, value
, value_length
,
279 expiration
, flags
, 0, PREPEND_OP
);
283 memcached_return
memcached_append_by_key(memcached_st
*ptr
,
284 char *master_key
, size_t master_key_length
,
285 char *key
, size_t key_length
,
286 char *value
, size_t value_length
,
291 rc
= memcached_send(ptr
, key
, key_length
,
292 key
, key_length
, value
, value_length
,
293 expiration
, flags
, 0, APPEND_OP
);
297 memcached_return
memcached_cas_by_key(memcached_st
*ptr
,
298 char *master_key
, size_t master_key_length
,
299 char *key
, size_t key_length
,
300 char *value
, size_t value_length
,
306 rc
= memcached_send(ptr
, key
, key_length
,
307 key
, key_length
, value
, value_length
,
308 expiration
, flags
, cas
, CAS_OP
);