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
];
122 memcached_string_st
**cursor_key_exec
;
124 ptr
->cursor_server
= 0;
125 memset(buffer
, 0, HUGE_STRING_LEN
);
127 rc
= memcached_connect(ptr
);
129 if (rc
!= MEMCACHED_SUCCESS
)
132 cursor_key_exec
= (memcached_string_st
**)malloc(sizeof(memcached_string_st
*) * ptr
->number_of_hosts
);
133 memset(cursor_key_exec
, 0, sizeof(memcached_string_st
*) * ptr
->number_of_hosts
);
136 for (x
= 0; x
< number_of_keys
; x
++)
138 unsigned int server_key
;
140 server_key
= memcached_generate_hash(keys
[x
], key_length
[x
]) % ptr
->number_of_hosts
;
142 if (cursor_key_exec
[server_key
])
144 memcached_string_st
*string
= cursor_key_exec
[server_key
];
146 memcached_string_append_character(ptr
, string
, ' ');
147 memcached_string_append(ptr
, string
, keys
[x
], key_length
[x
]);
151 memcached_string_st
*string
= memcached_string_init(ptr
, SMALL_STRING_LEN
);
156 memcached_string_append(ptr
, string
, "get ", 4);
157 memcached_string_append(ptr
, string
, keys
[x
], key_length
[x
]);
159 cursor_key_exec
[server_key
]= string
;
165 Should we muddle on if some servers are dead?
167 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
169 if (cursor_key_exec
[x
])
171 memcached_string_st
*string
= cursor_key_exec
[x
];
172 memcached_string_append(ptr
, string
, "\r\n", 2);
174 if ((write(ptr
->hosts
[x
].fd
, string
->string
,
175 memcached_string_length(ptr
, string
)) == -1))
178 rc
= MEMCACHED_SOME_ERRORS
;
180 memcached_string_free(ptr
, string
);
184 free(cursor_key_exec
);
189 char *memcached_fetch(memcached_st
*ptr
, char *key
, size_t *key_length
,
190 size_t *value_length
,
192 memcached_return
*error
)
196 while (ptr
->cursor_server
< ptr
->number_of_hosts
)
198 value_check
= memcached_value_fetch(ptr
, key
, key_length
, value_length
, flags
,
199 error
, 1, ptr
->cursor_server
);
201 if (*error
== MEMCACHED_NOTFOUND
)
202 ptr
->cursor_server
++;