+
+
+memcached_return memcached_set(memcached_st *ptr, const char *key, size_t key_length,
+ const char *value, size_t value_length,
+ time_t expiration,
+ uint32_t flags)
+{
+ memcached_return rc;
+ LIBMEMCACHED_MEMCACHED_SET_START();
+ rc= memcached_send(ptr, key, key_length,
+ key, key_length, value, value_length,
+ expiration, flags, 0, SET_OP);
+ LIBMEMCACHED_MEMCACHED_SET_END();
+ return rc;
+}
+
+memcached_return memcached_add(memcached_st *ptr,
+ const char *key, size_t key_length,
+ const char *value, size_t value_length,
+ time_t expiration,
+ uint32_t flags)
+{
+ memcached_return rc;
+ LIBMEMCACHED_MEMCACHED_ADD_START();
+ rc= memcached_send(ptr, key, key_length,
+ key, key_length, value, value_length,
+ expiration, flags, 0, ADD_OP);
+ LIBMEMCACHED_MEMCACHED_ADD_END();
+ return rc;
+}
+
+memcached_return memcached_replace(memcached_st *ptr,
+ const char *key, size_t key_length,
+ const char *value, size_t value_length,
+ time_t expiration,
+ uint32_t flags)
+{
+ memcached_return rc;
+ LIBMEMCACHED_MEMCACHED_REPLACE_START();
+ rc= memcached_send(ptr, key, key_length,
+ key, key_length, value, value_length,
+ expiration, flags, 0, REPLACE_OP);
+ LIBMEMCACHED_MEMCACHED_REPLACE_END();
+ return rc;
+}
+
+memcached_return memcached_prepend(memcached_st *ptr,
+ const char *key, size_t key_length,
+ const char *value, size_t value_length,
+ time_t expiration,
+ uint32_t flags)
+{
+ memcached_return rc;
+ rc= memcached_send(ptr, key, key_length,
+ key, key_length, value, value_length,
+ expiration, flags, 0, PREPEND_OP);
+ return rc;
+}
+
+memcached_return memcached_append(memcached_st *ptr,
+ const char *key, size_t key_length,
+ const char *value, size_t value_length,
+ time_t expiration,
+ uint32_t flags)
+{
+ memcached_return rc;
+ rc= memcached_send(ptr, key, key_length,
+ key, key_length, value, value_length,
+ expiration, flags, 0, APPEND_OP);
+ return rc;
+}
+
+memcached_return memcached_cas(memcached_st *ptr,
+ const char *key, size_t key_length,
+ const char *value, size_t value_length,
+ time_t expiration,
+ uint32_t flags,
+ uint64_t cas)
+{
+ memcached_return rc;
+ rc= memcached_send(ptr, key, key_length,
+ key, key_length, value, value_length,
+ expiration, flags, cas, CAS_OP);
+ return rc;
+}
+
+memcached_return memcached_set_by_key(memcached_st *ptr,
+ const char *master_key __attribute__((unused)),
+ size_t master_key_length __attribute__((unused)),
+ const char *key, size_t key_length,
+ const char *value, size_t value_length,
+ time_t expiration,
+ uint32_t flags)
+{
+ memcached_return rc;
+ LIBMEMCACHED_MEMCACHED_SET_START();
+ rc= memcached_send(ptr, master_key, master_key_length,
+ key, key_length, value, value_length,
+ expiration, flags, 0, SET_OP);
+ LIBMEMCACHED_MEMCACHED_SET_END();
+ return rc;
+}
+
+memcached_return memcached_add_by_key(memcached_st *ptr,
+ const char *master_key, size_t master_key_length,
+ const char *key, size_t key_length,
+ const char *value, size_t value_length,
+ time_t expiration,
+ uint32_t flags)
+{
+ memcached_return rc;
+ LIBMEMCACHED_MEMCACHED_ADD_START();
+ rc= memcached_send(ptr, master_key, master_key_length,
+ key, key_length, value, value_length,
+ expiration, flags, 0, ADD_OP);
+ LIBMEMCACHED_MEMCACHED_ADD_END();
+ return rc;
+}
+
+memcached_return memcached_replace_by_key(memcached_st *ptr,
+ const char *master_key, size_t master_key_length,
+ const char *key, size_t key_length,
+ const char *value, size_t value_length,
+ time_t expiration,
+ uint32_t flags)
+{
+ memcached_return rc;
+ LIBMEMCACHED_MEMCACHED_REPLACE_START();
+ rc= memcached_send(ptr, master_key, master_key_length,
+ key, key_length, value, value_length,
+ expiration, flags, 0, REPLACE_OP);
+ LIBMEMCACHED_MEMCACHED_REPLACE_END();
+ return rc;
+}
+
+memcached_return memcached_prepend_by_key(memcached_st *ptr,
+ const char *master_key, size_t master_key_length,
+ const char *key, size_t key_length,
+ const char *value, size_t value_length,
+ time_t expiration,
+ uint32_t flags)
+{
+ memcached_return rc;
+ rc= memcached_send(ptr, master_key, master_key_length,
+ key, key_length, value, value_length,
+ expiration, flags, 0, PREPEND_OP);
+ return rc;
+}
+
+memcached_return memcached_append_by_key(memcached_st *ptr,
+ const char *master_key, size_t master_key_length,
+ const char *key, size_t key_length,
+ const char *value, size_t value_length,
+ time_t expiration,
+ uint32_t flags)
+{
+ memcached_return rc;
+ rc= memcached_send(ptr, master_key, master_key_length,
+ key, key_length, value, value_length,
+ expiration, flags, 0, APPEND_OP);
+ return rc;
+}
+
+memcached_return memcached_cas_by_key(memcached_st *ptr,
+ const char *master_key, size_t master_key_length,
+ const char *key, size_t key_length,
+ const char *value, size_t value_length,
+ time_t expiration,
+ uint32_t flags,
+ uint64_t cas)
+{
+ memcached_return rc;
+ rc= memcached_send(ptr, master_key, master_key_length,
+ key, key_length, value, value_length,
+ expiration, flags, cas, CAS_OP);
+ return rc;
+}
+
+static memcached_return memcached_send_binary(memcached_server_st* server,
+ const char *key,
+ size_t key_length,
+ const char *value,
+ size_t value_length,
+ time_t expiration,
+ uint32_t flags,
+ uint64_t cas,
+ memcached_storage_action verb)
+{
+ char flush;
+ protocol_binary_request_set request= {.bytes= {0}};
+ size_t send_length= sizeof(request.bytes);
+
+ request.message.header.request.magic= PROTOCOL_BINARY_REQ;
+ switch (verb)
+ {
+ case SET_OP:
+ request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SET;
+ break;
+ case ADD_OP:
+ request.message.header.request.opcode= PROTOCOL_BINARY_CMD_ADD;
+ break;
+ case REPLACE_OP:
+ request.message.header.request.opcode= PROTOCOL_BINARY_CMD_REPLACE;
+ break;
+ case APPEND_OP:
+ request.message.header.request.opcode= PROTOCOL_BINARY_CMD_APPEND;
+ break;
+ case PREPEND_OP:
+ request.message.header.request.opcode= PROTOCOL_BINARY_CMD_PREPEND;
+ break;
+ case CAS_OP:
+ request.message.header.request.opcode= PROTOCOL_BINARY_CMD_REPLACE;
+ break;
+ }
+
+ request.message.header.request.keylen= htons((uint16_t)key_length);
+ request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
+ if (verb == APPEND_OP || verb == PREPEND_OP)
+ send_length -= 8; /* append & prepend does not contain extras! */
+ else
+ {
+ request.message.header.request.extlen= 8;
+ request.message.body.flags= htonl(flags);
+ request.message.body.expiration= htonl((uint32_t)expiration);
+ }
+
+ request.message.header.request.bodylen= htonl(key_length + value_length +
+ request.message.header.request.extlen);
+
+ if (cas)
+ request.message.header.request.cas= htonll(cas);
+
+ flush= ((server->root->flags & MEM_BUFFER_REQUESTS) && verb == SET_OP) ? 0 : 1;
+ /* write the header */
+ if ((memcached_do(server, (const char*)request.bytes, send_length, 0) != MEMCACHED_SUCCESS) ||
+ (memcached_io_write(server, key, key_length, 0) == -1) ||
+ (memcached_io_write(server, value, value_length, flush) == -1))
+ {
+ memcached_io_reset(server);
+ return MEMCACHED_WRITE_FAILURE;
+ }
+
+ if (flush == 0)
+ return MEMCACHED_BUFFERED;
+
+ return memcached_response(server, NULL, 0, NULL);
+}
+