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
];
23 rc
= memcached_connect(ptr
);
25 if (rc
!= MEMCACHED_SUCCESS
)
28 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
29 "%s %.*s %u %u %u\r\n", verb
,
30 key_length
, key
, flags
, expiration
, value_length
);
31 if ((send(ptr
->fd
, buffer
, send_length
, 0) == -1))
33 fprintf(stderr
, "failed set on %.*s TCP\n", key_length
+1, key
);
35 return MEMCACHED_WRITE_FAILURE
;
38 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
41 if ((send(ptr
->fd
, buffer
, send_length
, 0) == -1))
43 fprintf(stderr
, "failed set on %.*s TCP\n", key_length
+1, key
);
45 return MEMCACHED_WRITE_FAILURE
;
48 send_length
= read(ptr
->fd
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
);
50 if (send_length
&& buffer
[0] == 'S') /* STORED */
51 return MEMCACHED_SUCCESS
;
52 else if (send_length
&& buffer
[0] == 'N') /* NOT_STORED */
53 return MEMCACHED_NOTSTORED
;
55 return MEMCACHED_READ_FAILURE
;
58 memcached_return
memcached_set(memcached_st
*ptr
, char *key
, size_t key_length
,
59 char *value
, size_t value_length
,
63 return memcached_send(ptr
, key
, key_length
, value
, value_length
,
64 expiration
, flags
, "set");
67 memcached_return
memcached_add(memcached_st
*ptr
, char *key
, size_t key_length
,
68 char *value
, size_t value_length
,
72 return memcached_send(ptr
, key
, key_length
, value
, value_length
,
73 expiration
, flags
, "add");
76 memcached_return
memcached_replace(memcached_st
*ptr
, char *key
, size_t key_length
,
77 char *value
, size_t value_length
,
81 return memcached_send(ptr
, key
, key_length
, value
, value_length
,
82 expiration
, flags
, "replace");