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 rc
= memcached_connect(ptr
);
16 if (rc
!= MEMCACHED_SUCCESS
)
19 server_key
= memcached_generate_hash(key
, key_length
) % ptr
->number_of_hosts
;
21 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
22 "%s %.*s %u\r\n", verb
,
25 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
26 return MEMCACHED_WRITE_FAILURE
;
27 sent_length
= write(ptr
->hosts
[server_key
].fd
, buffer
, send_length
);
28 if (sent_length
== -1)
30 fprintf(stderr
, "error %s: write: %m\n", __FUNCTION__
);
31 return MEMCACHED_WRITE_FAILURE
;
33 if (sent_length
!= send_length
)
35 fprintf(stderr
, "error %s: short write %d %d: %m\n",
36 __FUNCTION__
, sent_length
, send_length
);
37 return MEMCACHED_WRITE_FAILURE
;
40 memset(buffer
, 0, MEMCACHED_DEFAULT_COMMAND_SIZE
);
41 send_length
= read(ptr
->hosts
[server_key
].fd
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
);
43 if (!memcmp(buffer
, "ERROR\r\n", MEMCACHED_DEFAULT_COMMAND_SIZE
))
46 rc
= MEMCACHED_PROTOCOL_ERROR
;
48 else if (!memcmp(buffer
, "NOT_FOUND\r\n", MEMCACHED_DEFAULT_COMMAND_SIZE
))
51 rc
= MEMCACHED_NOTFOUND
;
55 *value
= strtol(buffer
, (char **)NULL
, 10);
56 rc
= MEMCACHED_SUCCESS
;
62 memcached_return
memcached_increment(memcached_st
*ptr
,
63 char *key
, size_t key_length
,
67 return memcached_auto(ptr
, "incr", key
, key_length
, offset
, value
);
70 memcached_return
memcached_decrement(memcached_st
*ptr
,
71 char *key
, size_t key_length
,
75 return memcached_auto(ptr
, "decr", key
, key_length
, offset
, value
);