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
)
33 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
34 unsigned int server_key
;
36 WATCHPOINT_ASSERT(value
);
37 WATCHPOINT_ASSERT(value_length
);
39 /* Leaving this WATCHPOINT_ASSERT in since only a library fubar could blow this */
41 if (!(ptr
->flags
& MEM_NO_BLOCK
) && ptr
->write_buffer_offset
!= 0)
45 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
47 rc
= memcached_connect(ptr
, server_key
);
48 if (rc
!= MEMCACHED_SUCCESS
)
51 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
52 "%s %.*s %x %llu %zu\r\n", storage_op_string(verb
),
53 (int)key_length
, key
, flags
,
54 (unsigned long long)expiration
, value_length
);
55 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
57 rc
= MEMCACHED_WRITE_FAILURE
;
62 We have to flush after sending the command. Memcached is not smart enough
63 to just keep reading from the socket :(
65 if ((sent_length
= memcached_io_write(ptr
, server_key
, buffer
, write_length
, 0)) == -1)
67 rc
= MEMCACHED_WRITE_FAILURE
;
71 if ((sent_length
= memcached_io_write(ptr
, server_key
, value
, value_length
, 0)) == -1)
73 rc
= MEMCACHED_WRITE_FAILURE
;
77 if ((ptr
->flags
& MEM_NO_BLOCK
) && verb
== SET_OP
)
82 if ((sent_length
= memcached_io_write(ptr
, server_key
, "\r\n", 2, to_write
)) == -1)
84 memcached_quit_server(ptr
, server_key
);
85 rc
= MEMCACHED_WRITE_FAILURE
;
89 if ((ptr
->flags
& MEM_NO_BLOCK
) && verb
== SET_OP
)
91 rc
= MEMCACHED_SUCCESS
;
92 memcached_server_response_increment(ptr
, server_key
);
96 rc
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, server_key
);
99 if (rc
== MEMCACHED_STORED
)
100 return MEMCACHED_SUCCESS
;
105 memcached_io_reset(ptr
, server_key
);
110 memcached_return
memcached_set(memcached_st
*ptr
, char *key
, size_t key_length
,
111 char *value
, size_t value_length
,
116 LIBMEMCACHED_MEMCACHED_SET_START();
117 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
118 expiration
, flags
, SET_OP
);
119 LIBMEMCACHED_MEMCACHED_SET_END();
123 memcached_return
memcached_add(memcached_st
*ptr
, char *key
, size_t key_length
,
124 char *value
, size_t value_length
,
129 LIBMEMCACHED_MEMCACHED_ADD_START();
130 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
131 expiration
, flags
, ADD_OP
);
132 LIBMEMCACHED_MEMCACHED_ADD_END();
136 memcached_return
memcached_replace(memcached_st
*ptr
, char *key
, size_t key_length
,
137 char *value
, size_t value_length
,
142 LIBMEMCACHED_MEMCACHED_REPLACE_START();
143 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
144 expiration
, flags
, REPLACE_OP
);
145 LIBMEMCACHED_MEMCACHED_REPLACE_END();