3 static memcached_return
memcached_auto(memcached_st
*ptr
,
5 char *key
, size_t key_length
,
9 size_t send_length
, sent_length
;
11 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
12 unsigned int server_key
;
14 if (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
15 return MEMCACHED_NO_SERVERS
;
17 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
19 if ((rc
= memcached_connect(ptr
, server_key
)) != MEMCACHED_SUCCESS
)
22 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
23 "%s %.*s %u\r\n", verb
,
26 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
27 return MEMCACHED_WRITE_FAILURE
;
28 sent_length
= memcached_io_write(ptr
, server_key
, buffer
, send_length
, 1);
30 if (sent_length
== -1 || sent_length
!= send_length
)
31 return MEMCACHED_WRITE_FAILURE
;
33 memset(buffer
, 0, MEMCACHED_DEFAULT_COMMAND_SIZE
);
35 rc
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, server_key
);
38 So why recheck responce? Because the protocol is brain dead :)
39 The number returned might end up equaling one of the string
40 values. Less chance of a mistake with memcmp() so we will
41 use it. We still called memcached_response() though since it
42 worked its magic for non-blocking IO.
44 if (!memcmp(buffer
, "ERROR\r\n", MEMCACHED_DEFAULT_COMMAND_SIZE
))
47 rc
= MEMCACHED_PROTOCOL_ERROR
;
49 else if (!memcmp(buffer
, "NOT_FOUND\r\n", MEMCACHED_DEFAULT_COMMAND_SIZE
))
52 rc
= MEMCACHED_NOTFOUND
;
56 *value
= (uint64_t)strtoll(buffer
, (char **)NULL
, 10);
57 rc
= MEMCACHED_SUCCESS
;
63 memcached_return
memcached_increment(memcached_st
*ptr
,
64 char *key
, size_t key_length
,
70 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
71 rc
= memcached_auto(ptr
, "incr", key
, key_length
, offset
, value
);
72 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
77 memcached_return
memcached_decrement(memcached_st
*ptr
,
78 char *key
, size_t key_length
,
84 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
85 rc
= memcached_auto(ptr
, "decr", key
, key_length
, offset
, value
);
86 LIBMEMCACHED_MEMCACHED_DECREMENT_END();