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 if (*error
!= MEMCACHED_SUCCESS
)
17 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, "get %.*s\r\n",
19 if (*error
!= MEMCACHED_SUCCESS
)
22 if ((send(ptr
->fd
, buffer
, send_length
, 0) == -1))
24 fprintf(stderr
, "failed fetch on %.*s TCP\n", key_length
+1, key
);
25 *error
= MEMCACHED_WRITE_FAILURE
;
29 memset(buffer
, 0, MEMCACHED_DEFAULT_COMMAND_SIZE
);
30 *error
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
);
32 if (*error
== MEMCACHED_SUCCESS
)
37 string_ptr
+= 6; /* "VALUE " */
39 /* We do nothing with the key, since we only asked for one key */
40 for (end_ptr
= string_ptr
; *end_ptr
!= ' '; end_ptr
++);
43 string_ptr
= end_ptr
+ 1;
44 for (end_ptr
= string_ptr
; *end_ptr
!= ' '; end_ptr
++);
45 *flags
= (uint16_t)strtol(string_ptr
, &end_ptr
, 10);
48 string_ptr
= end_ptr
+ 1;
49 for (end_ptr
= string_ptr
; *end_ptr
!= ' '; end_ptr
++);
50 *value_length
= strtoll(string_ptr
, &end_ptr
, 10);
52 /* Skip past the \r\n */
53 string_ptr
= end_ptr
+2;
61 value
= (char *)malloc(*value_length
* sizeof(char));
65 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
69 need_to_copy
= (*value_length
< (size_t)(buffer
-string_ptr
))
71 : (size_t)(buffer
-string_ptr
) ;
73 pos_ptr
= memcpy(value
, string_ptr
, need_to_copy
);
75 if ( need_to_copy
> *value_length
)
80 need_to_read
= *value_length
- need_to_copy
;
82 read_length
= read(ptr
->fd
, pos_ptr
, need_to_read
);
83 if (read_length
!= need_to_read
)
86 *error
= MEMCACHED_PARTIAL_READ
;