10 #include <memcached.h>
12 static memcached_return
memcached_send(memcached_st
*ptr
,
13 char *key
, size_t key_length
,
14 char *value
, size_t value_length
,
21 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
22 unsigned int server_key
;
24 rc
= memcached_connect(ptr
);
26 if (rc
!= MEMCACHED_SUCCESS
)
29 server_key
= memcached_generate_hash(key
, key_length
) % ptr
->number_of_hosts
;
31 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
32 "%s %.*s %u %u %u\r\n", verb
,
33 key_length
, key
, flags
, expiration
, value_length
);
34 if ((send(ptr
->hosts
[server_key
].fd
, buffer
, send_length
, 0) == -1))
36 fprintf(stderr
, "failed set on %.*s TCP\n", key_length
+1, key
);
38 return MEMCACHED_WRITE_FAILURE
;
41 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
44 if ((send(ptr
->hosts
[server_key
].fd
, buffer
, send_length
, 0) == -1))
46 fprintf(stderr
, "failed set on %.*s TCP\n", key_length
+1, key
);
48 return MEMCACHED_WRITE_FAILURE
;
51 send_length
= read(ptr
->hosts
[server_key
].fd
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
);
53 if (send_length
&& buffer
[0] == 'S') /* STORED */
54 return MEMCACHED_SUCCESS
;
55 else if (send_length
&& buffer
[0] == 'N') /* NOT_STORED */
56 return MEMCACHED_NOTSTORED
;
58 return MEMCACHED_READ_FAILURE
;
61 memcached_return
memcached_set(memcached_st
*ptr
, char *key
, size_t key_length
,
62 char *value
, size_t value_length
,
66 return memcached_send(ptr
, key
, key_length
, value
, value_length
,
67 expiration
, flags
, "set");
70 memcached_return
memcached_add(memcached_st
*ptr
, char *key
, size_t key_length
,
71 char *value
, size_t value_length
,
75 return memcached_send(ptr
, key
, key_length
, value
, value_length
,
76 expiration
, flags
, "add");
79 memcached_return
memcached_replace(memcached_st
*ptr
, char *key
, size_t key_length
,
80 char *value
, size_t value_length
,
84 return memcached_send(ptr
, key
, key_length
, value
, value_length
,
85 expiration
, flags
, "replace");