3 static memcached_return
memcached_auto(memcached_st
*ptr
,
5 const char *key
, size_t key_length
,
11 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
12 unsigned int server_key
;
14 unlikely (key_length
== 0)
15 return MEMCACHED_NO_KEY_PROVIDED
;
17 unlikely (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
18 return MEMCACHED_NO_SERVERS
;
20 if ((ptr
->flags
& MEM_VERIFY_KEY
) && (memcachd_key_test((char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
21 return MEMCACHED_BAD_KEY_PROVIDED
;
23 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
25 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
26 "%s %s%.*s %u\r\n", verb
,
30 unlikely (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
31 return MEMCACHED_WRITE_FAILURE
;
33 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, send_length
, 1);
34 if (rc
!= MEMCACHED_SUCCESS
)
37 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
40 So why recheck responce? Because the protocol is brain dead :)
41 The number returned might end up equaling one of the string
42 values. Less chance of a mistake with strncmp() so we will
43 use it. We still called memcached_response() though since it
44 worked its magic for non-blocking IO.
46 if (!strncmp(buffer
, "ERROR\r\n", 7))
49 rc
= MEMCACHED_PROTOCOL_ERROR
;
51 else if (!strncmp(buffer
, "NOT_FOUND\r\n", 11))
54 rc
= MEMCACHED_NOTFOUND
;
58 *value
= (uint64_t)strtoll(buffer
, (char **)NULL
, 10);
59 rc
= MEMCACHED_SUCCESS
;
65 memcached_return
memcached_increment(memcached_st
*ptr
,
66 const char *key
, size_t key_length
,
72 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
73 rc
= memcached_auto(ptr
, "incr", key
, key_length
, offset
, value
);
74 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
79 memcached_return
memcached_decrement(memcached_st
*ptr
,
80 const char *key
, size_t key_length
,
86 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
87 rc
= memcached_auto(ptr
, "decr", key
, key_length
, offset
, value
);
88 LIBMEMCACHED_MEMCACHED_DECREMENT_END();