12 static memcached_return
memcached_send(memcached_st
*ptr
,
13 char *key
, size_t key_length
,
14 char *value
, size_t value_length
,
22 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
23 unsigned int server_key
;
28 memset(buffer
, 0, MEMCACHED_DEFAULT_COMMAND_SIZE
);
30 rc
= memcached_connect(ptr
);
31 if (rc
!= MEMCACHED_SUCCESS
)
34 server_key
= memcached_generate_hash(key
, key_length
) % ptr
->number_of_hosts
;
36 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
37 "%s %.*s %x %llu %zu\r\n", verb
,
38 (int)key_length
, key
, flags
,
39 (unsigned long long)expiration
, value_length
);
40 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
41 return MEMCACHED_WRITE_FAILURE
;
42 if ((sent_length
= send(ptr
->hosts
[server_key
].fd
, buffer
, write_length
, 0)) == -1)
43 return MEMCACHED_WRITE_FAILURE
;
44 assert(write_length
== sent_length
);
46 if ((sent_length
= send(ptr
->hosts
[server_key
].fd
, value
, value_length
, 0)) == -1)
47 return MEMCACHED_WRITE_FAILURE
;
48 assert(value_length
== sent_length
);
50 if ((sent_length
= send(ptr
->hosts
[server_key
].fd
, "\r\n", 2, 0)) == -1)
51 return MEMCACHED_WRITE_FAILURE
;
52 assert(2 == sent_length
);
54 sent_length
= recv(ptr
->hosts
[server_key
].fd
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, 0);
56 if (sent_length
&& buffer
[0] == 'S') /* STORED */
57 return MEMCACHED_SUCCESS
;
58 else if (write_length
&& buffer
[0] == 'N') /* NOT_STORED */
59 return MEMCACHED_NOTSTORED
;
61 return MEMCACHED_READ_FAILURE
;
64 memcached_return
memcached_set(memcached_st
*ptr
, char *key
, size_t key_length
,
65 char *value
, size_t value_length
,
70 LIBMEMCACHED_MEMCACHED_SET_START();
71 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
72 expiration
, flags
, "set");
73 LIBMEMCACHED_MEMCACHED_SET_END();
77 memcached_return
memcached_add(memcached_st
*ptr
, char *key
, size_t key_length
,
78 char *value
, size_t value_length
,
83 LIBMEMCACHED_MEMCACHED_ADD_START();
84 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
85 expiration
, flags
, "add");
86 LIBMEMCACHED_MEMCACHED_ADD_END();
90 memcached_return
memcached_replace(memcached_st
*ptr
, char *key
, size_t key_length
,
91 char *value
, size_t value_length
,
96 LIBMEMCACHED_MEMCACHED_REPLACE_START();
97 rc
= memcached_send(ptr
, key
, key_length
, value
, value_length
,
98 expiration
, flags
, "replace");
99 LIBMEMCACHED_MEMCACHED_REPLACE_END();