3 char *memcached_get(memcached_st
*ptr
, char *key
, size_t key_length
,
6 memcached_return
*error
)
9 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
12 *error
= memcached_connect(ptr
);
14 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, "get %.*s\r\n",
16 if (*error
!= MEMCACHED_SUCCESS
)
19 if ((send(ptr
->fd
, buffer
, send_length
, 0) == -1))
21 fprintf(stderr
, "failed fetch on %.*s TCP\n", key_length
+1, key
);
22 *error
= MEMCACHED_WRITE_FAILURE
;
26 memset(buffer
, 0, MEMCACHED_DEFAULT_COMMAND_SIZE
);
27 *error
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
);
29 if (*error
== MEMCACHED_SUCCESS
)
34 string_ptr
+= 6; /* "VALUE " */
36 /* We do nothing with the key, since we only asked for one key */
37 for (end_ptr
= string_ptr
; *end_ptr
!= ' '; end_ptr
++);
40 string_ptr
= end_ptr
+ 1;
41 for (end_ptr
= string_ptr
; *end_ptr
!= ' '; end_ptr
++);
42 *flags
= (uint16_t)strtol(string_ptr
, &end_ptr
, 10);
45 string_ptr
= end_ptr
+ 1;
46 for (end_ptr
= string_ptr
; *end_ptr
!= ' '; end_ptr
++);
47 *value_length
= strtoll(string_ptr
, &end_ptr
, 10);
49 /* Skip past the \r\n */
50 string_ptr
= end_ptr
+2;
58 value
= (char *)malloc(*value_length
* sizeof(char));
62 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
66 need_to_copy
= (*value_length
< (size_t)(buffer
-string_ptr
))
68 : (size_t)(buffer
-string_ptr
) ;
70 pos_ptr
= memcpy(value
, string_ptr
, need_to_copy
);
72 if ( need_to_copy
> *value_length
)
77 need_to_read
= *value_length
- need_to_copy
;
79 read_length
= read(ptr
->fd
, pos_ptr
, need_to_read
);
80 if (read_length
!= need_to_read
)
83 *error
= MEMCACHED_PARTIAL_READ
;