How can I preserve the order of the values fetched with memcache's get_multi() function? By default, the order returned is random. Thanks.
Asked
Active
Viewed 1,861 times
1
-
Remember that memcache requests may be hitting many memcache servers, so there's no way you'll actually get the results in any particular order in general. Any order preservation will be client-side--not preserving order, but restoring it. – Glenn Maynard Dec 07 '10 at 03:12
2 Answers
3
Python's Memcache library returns a dictionary, and dictionaries in python are unordered, so you have to get the values from the dictionary in the right order manually:
result = cache.get_multi(keys)
values = [result.get(key) for key in keys]

Ivan Virabyan
- 1,666
- 2
- 19
- 25
-
There's a syntax error in the second line, the right side of the assignment should either begin with a bracket or end with a square bracket. – dnet Sep 29 '11 at 11:44
0
as I remember memcache has flag GET PRESERVE ORDER, try adding this to function flags

damir
- 1,898
- 3
- 16
- 23
-
I was able to find this function in php but not in any of the python libraries – ensnare Dec 07 '10 at 05:15