3 static char *memcached_value_fetch(memcached_st
*ptr
, char *key
, size_t *key_length
,
6 memcached_return
*error
,
8 unsigned int server_key
)
10 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
15 memset(buffer
, 0, MEMCACHED_DEFAULT_COMMAND_SIZE
);
16 *error
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, server_key
);
18 if (*error
== MEMCACHED_SUCCESS
)
23 string_ptr
+= 6; /* "VALUE " */
28 memset(key
, 0, MEMCACHED_MAX_KEY
);
29 for (end_ptr
= string_ptr
; *end_ptr
!= ' '; end_ptr
++)
35 else /* Skip characters */
36 for (end_ptr
= string_ptr
; *end_ptr
!= ' '; end_ptr
++);
39 string_ptr
= end_ptr
+ 1;
40 for (end_ptr
= string_ptr
; *end_ptr
!= ' '; end_ptr
++);
41 *flags
= (uint16_t)strtol(string_ptr
, &end_ptr
, 10);
44 string_ptr
= end_ptr
+ 1;
45 for (end_ptr
= string_ptr
; *end_ptr
!= ' '; end_ptr
++);
46 *value_length
= strtoll(string_ptr
, &end_ptr
, 10);
48 /* Skip past the \r\n */
49 string_ptr
= end_ptr
+2;
56 /* We add two bytes so that we can walk the \r\n */
57 value
= (char *)malloc(((*value_length
) +2) * sizeof(char));
62 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
66 read_length
= read(ptr
->hosts
[server_key
].fd
, value
, (*value_length
)+2);
68 if ((read_length
-2) != *value_length
)
71 *error
= MEMCACHED_PARTIAL_READ
;
83 char *memcached_get(memcached_st
*ptr
, char *key
, size_t key_length
,
86 memcached_return
*error
)
89 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
90 unsigned int server_key
;
93 *error
= memcached_connect(ptr
);
95 if (*error
!= MEMCACHED_SUCCESS
)
98 server_key
= memcached_generate_hash(key
, key_length
) % ptr
->number_of_hosts
;
100 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, "get %.*s\r\n",
101 (int)key_length
, key
);
102 if (*error
!= MEMCACHED_SUCCESS
)
105 if ((write(ptr
->hosts
[server_key
].fd
, buffer
, send_length
) == -1))
107 *error
= MEMCACHED_WRITE_FAILURE
;
111 return memcached_value_fetch(ptr
, key
, &key_length
, value_length
, flags
,
112 error
, 0, server_key
);
115 memcached_return
memcached_mget(memcached_st
*ptr
,
116 char **keys
, size_t *key_length
,
117 unsigned int number_of_keys
)
119 char buffer
[HUGE_STRING_LEN
];
124 ptr
->cursor_server
= 0;
125 memset(buffer
, 0, HUGE_STRING_LEN
);
127 rc
= memcached_connect(ptr
);
129 if (rc
!= MEMCACHED_SUCCESS
)
132 memcpy(buffer
, "get", strlen("get"));
134 buffer_ptr
+=strlen("get");
136 for (x
= 0; x
< number_of_keys
; x
++)
140 memcpy(buffer_ptr
, keys
[x
], key_length
[x
]);
141 buffer_ptr
+= key_length
[x
];
144 memcpy(buffer_ptr
, "\r\n", 2);
148 This must be fixed. Right now we hit every server, and send keys
149 to all servers. We should fix this quickly.
151 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
153 if ((write(ptr
->hosts
[x
].fd
, buffer
, (size_t)(buffer_ptr
- buffer
)) == -1))
156 rc
= MEMCACHED_SOME_ERRORS
;
163 char *memcached_fetch(memcached_st
*ptr
, char *key
, size_t *key_length
,
164 size_t *value_length
,
166 memcached_return
*error
)
170 while (ptr
->cursor_server
< ptr
->number_of_hosts
)
172 value_check
= memcached_value_fetch(ptr
, key
, key_length
, value_length
, flags
,
173 error
, 1, ptr
->cursor_server
);
175 if (*error
== MEMCACHED_NOTFOUND
)
176 ptr
->cursor_server
++;