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
;
13 bool no_reply
= (ptr
->flags
& MEM_NOREPLY
);
15 unlikely (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
16 return MEMCACHED_NO_SERVERS
;
18 if ((ptr
->flags
& MEM_VERIFY_KEY
) && (memcached_key_test((char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
19 return MEMCACHED_BAD_KEY_PROVIDED
;
21 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
23 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
24 "%s %s%.*s %u%s\r\n", verb
,
27 offset
, no_reply
? " noreply" : "");
28 unlikely (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
29 return MEMCACHED_WRITE_FAILURE
;
31 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, send_length
, 1);
32 if (no_reply
|| rc
!= MEMCACHED_SUCCESS
)
35 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
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 strncmp() so we will
41 use it. We still called memcached_response() though since it
42 worked its magic for non-blocking IO.
44 if (!strncmp(buffer
, "ERROR\r\n", 7))
47 rc
= MEMCACHED_PROTOCOL_ERROR
;
49 else if (!strncmp(buffer
, "NOT_FOUND\r\n", 11))
52 rc
= MEMCACHED_NOTFOUND
;
56 *value
= strtoull(buffer
, (char **)NULL
, 10);
57 rc
= MEMCACHED_SUCCESS
;
63 static memcached_return
binary_incr_decr(memcached_st
*ptr
, uint8_t cmd
,
64 const char *key
, size_t key_length
,
65 uint32_t offset
, uint64_t *value
)
67 unsigned int server_key
;
68 bool no_reply
= (ptr
->flags
& MEM_NOREPLY
);
70 unlikely (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
71 return MEMCACHED_NO_SERVERS
;
73 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
77 if(cmd
== PROTOCOL_BINARY_CMD_DECREMENT
)
78 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
79 if(cmd
== PROTOCOL_BINARY_CMD_INCREMENT
)
80 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
82 protocol_binary_request_incr request
= {.bytes
= {0}};
84 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
85 request
.message
.header
.request
.opcode
= cmd
;
86 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
87 request
.message
.header
.request
.extlen
= 20;
88 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
89 request
.message
.header
.request
.bodylen
= htonl(key_length
+ request
.message
.header
.request
.extlen
);
90 request
.message
.body
.delta
= htonll(offset
);
92 /* TODO: The binary protocol allows you to specify initial and expiry time */
93 if ((memcached_do(&ptr
->hosts
[server_key
], request
.bytes
,
94 sizeof(request
.bytes
), 0)!=MEMCACHED_SUCCESS
) ||
95 (memcached_io_write(&ptr
->hosts
[server_key
], key
, key_length
, 1) == -1))
97 memcached_io_reset(&ptr
->hosts
[server_key
]);
98 return MEMCACHED_WRITE_FAILURE
;
102 return MEMCACHED_SUCCESS
;
103 return memcached_response(&ptr
->hosts
[server_key
], (char*)value
, sizeof(*value
), NULL
);
106 memcached_return
memcached_increment(memcached_st
*ptr
,
107 const char *key
, size_t key_length
,
111 memcached_return rc
= memcached_validate_key_length(key_length
, ptr
->flags
& MEM_BINARY_PROTOCOL
);
112 unlikely (rc
!= MEMCACHED_SUCCESS
)
115 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
116 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
117 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
, key
,
118 key_length
, offset
, value
);
120 rc
= memcached_auto(ptr
, "incr", key
, key_length
, offset
, value
);
122 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
127 memcached_return
memcached_decrement(memcached_st
*ptr
,
128 const char *key
, size_t key_length
,
132 memcached_return rc
= memcached_validate_key_length(key_length
, ptr
->flags
& MEM_BINARY_PROTOCOL
);
133 unlikely (rc
!= MEMCACHED_SUCCESS
)
136 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
137 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
138 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
, key
,
139 key_length
, offset
, value
);
141 rc
= memcached_auto(ptr
, "decr", key
, key_length
, offset
, value
);
143 LIBMEMCACHED_MEMCACHED_DECREMENT_END();