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((const 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
= (size_t)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 uint64_t offset
, uint64_t initial
,
69 unsigned int server_key
;
70 bool no_reply
= (ptr
->flags
& MEM_NOREPLY
);
72 unlikely (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
73 return MEMCACHED_NO_SERVERS
;
75 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
79 if(cmd
== PROTOCOL_BINARY_CMD_DECREMENT
)
80 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
81 if(cmd
== PROTOCOL_BINARY_CMD_INCREMENT
)
82 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
84 protocol_binary_request_incr request
= {.bytes
= {0}};
86 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
87 request
.message
.header
.request
.opcode
= cmd
;
88 request
.message
.header
.request
.keylen
= htons((uint16_t) key_length
);
89 request
.message
.header
.request
.extlen
= 20;
90 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
91 request
.message
.header
.request
.bodylen
= htonl((uint32_t) (key_length
+ request
.message
.header
.request
.extlen
));
92 request
.message
.body
.delta
= htonll(offset
);
93 request
.message
.body
.initial
= htonll(initial
);
94 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
96 if ((memcached_do(&ptr
->hosts
[server_key
], request
.bytes
,
97 sizeof(request
.bytes
), 0)!=MEMCACHED_SUCCESS
) ||
98 (memcached_io_write(&ptr
->hosts
[server_key
], key
, key_length
, 1) == -1))
100 memcached_io_reset(&ptr
->hosts
[server_key
]);
101 return MEMCACHED_WRITE_FAILURE
;
105 return MEMCACHED_SUCCESS
;
106 return memcached_response(&ptr
->hosts
[server_key
], (char*)value
, sizeof(*value
), NULL
);
109 memcached_return
memcached_increment(memcached_st
*ptr
,
110 const char *key
, size_t key_length
,
114 memcached_return rc
= memcached_validate_key_length(key_length
, ptr
->flags
& MEM_BINARY_PROTOCOL
);
115 unlikely (rc
!= MEMCACHED_SUCCESS
)
118 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
119 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
120 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
, key
, key_length
,
121 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
124 rc
= memcached_auto(ptr
, "incr", key
, key_length
, offset
, value
);
126 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
131 memcached_return
memcached_decrement(memcached_st
*ptr
,
132 const char *key
, size_t key_length
,
136 memcached_return rc
= memcached_validate_key_length(key_length
, ptr
->flags
& MEM_BINARY_PROTOCOL
);
137 unlikely (rc
!= MEMCACHED_SUCCESS
)
140 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
141 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
142 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
, key
, key_length
,
143 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
146 rc
= memcached_auto(ptr
, "decr", key
, key_length
, offset
, value
);
148 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
153 memcached_return
memcached_increment_with_initial(memcached_st
*ptr
,
161 memcached_return rc
= memcached_validate_key_length(key_length
, ptr
->flags
& MEM_BINARY_PROTOCOL
);
162 unlikely (rc
!= MEMCACHED_SUCCESS
)
165 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
166 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
167 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
, key
,
168 key_length
, offset
, initial
, (uint32_t)expiration
,
171 rc
= MEMCACHED_PROTOCOL_ERROR
;
173 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
178 memcached_return
memcached_decrement_with_initial(memcached_st
*ptr
,
186 memcached_return rc
= memcached_validate_key_length(key_length
, ptr
->flags
& MEM_BINARY_PROTOCOL
);
187 unlikely (rc
!= MEMCACHED_SUCCESS
)
190 LIBMEMCACHED_MEMCACHED_DECREMENT_WITH_INITIAL_START();
191 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
192 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
, key
,
193 key_length
, offset
, initial
, (uint32_t)expiration
,
196 rc
= MEMCACHED_PROTOCOL_ERROR
;
198 LIBMEMCACHED_MEMCACHED_DECREMENT_WITH_INITIAL_END();