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 memset(buffer
, 0, MEMCACHED_DEFAULT_COMMAND_SIZE
);
40 /* Leaveing this assert in since only a library fubar could blow this */
41 assert(ptr
->write_buffer_offset
== 0);
43 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
45 rc
= memcached_connect(ptr
, server_key
);
46 if (rc
!= MEMCACHED_SUCCESS
)
49 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
50 "%s %.*s %x %llu %zu\r\n", storage_op_string(verb
),
51 (int)key_length
, key
, flags
,
52 (unsigned long long)expiration
, value_length
);
53 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
55 rc
= MEMCACHED_WRITE_FAILURE
;
60 We have to flush after sending the command. Memcached is not smart enough
61 to just keep reading from the socket :(
63 if ((sent_length
= memcached_io_write(ptr
, server_key
, buffer
, write_length
, 1)) == -1)
65 rc
= MEMCACHED_WRITE_FAILURE
;
69 if ((sent_length
= memcached_io_write(ptr
, server_key
, value
, value_length
, 0)) == -1)
71 rc
= MEMCACHED_WRITE_FAILURE
;
75 if ((sent_length
= memcached_io_write(ptr
, server_key
, "\r\n", 2, 1)) == -1)
77 rc
= MEMCACHED_WRITE_FAILURE
;
81 if ((ptr
->flags
& MEM_NO_BLOCK
) && verb
== SET_OP
)
83 rc
= MEMCACHED_SUCCESS
;
84 ptr
->stack_responses
++;
88 rc
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, server_key
);
91 if (rc
== MEMCACHED_STORED
)
92 return MEMCACHED_SUCCESS
;
97 memcached_io_reset(ptr
, server_key
);
102 memcached_return
memcached_set(memcached_st
*ptr
, char *key
, size_t key_length
,
103 char *value
, size_t value_length
,
108 LIBMEMCACHED_MEMCACHED_SET_START();
109 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
110 expiration
, flags
, SET_OP
);
111 LIBMEMCACHED_MEMCACHED_SET_END();
115 memcached_return
memcached_add(memcached_st
*ptr
, char *key
, size_t key_length
,
116 char *value
, size_t value_length
,
121 LIBMEMCACHED_MEMCACHED_ADD_START();
122 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
123 expiration
, flags
, ADD_OP
);
124 LIBMEMCACHED_MEMCACHED_ADD_END();
128 memcached_return
memcached_replace(memcached_st
*ptr
, char *key
, size_t key_length
,
129 char *value
, size_t value_length
,
134 LIBMEMCACHED_MEMCACHED_REPLACE_START();
135 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
136 expiration
, flags
, REPLACE_OP
);
137 LIBMEMCACHED_MEMCACHED_REPLACE_END();