Is there any way to get a random item from memcache?
            Asked
            
        
        
            Active
            
        
            Viewed 1,025 times
        
    1
            
            
        - 
                    1What problem are you trying to solve? What statistical properties of randomness do you require? There is no built in way of asking memcache for a random value. However, there is probably a way to achieve your goal without this feature. – Peter Recore Jan 11 '12 at 17:22
 - 
                    I'm doing something similar to this solution http://stackoverflow.com/a/3451052/281021. I was just wondering if it can benefit from any optimization using memcache. – Albert Jan 16 '12 at 11:46
 
2 Answers
3
            
            
        Not directly via API.
But you could create a list of image keys for all images in memcache and then randomly select from that list. The list itself would also need to me stored in memcache.
        Peter Knego
        
- 79,991
 - 11
 - 123
 - 154
 
- 
                    How would you know what images were in memcache, in order to create your list? Items in memcache can disappear at any time with no warning. – Peter Recore Jan 11 '12 at 17:13
 - 
                    As usual you try to get it from cache, if not available load it from datastore. The additional work is to add/remove entry from list when image is added/removed from memcache. – Peter Knego Jan 11 '12 at 18:25
 - 
                    I assumed that going to the datastore is not acceptable. To me the question is "can i get a random item that is in memcache right now, without going to the datastore" – Peter Recore Jan 11 '12 at 21:21
 - 
                    If going to the datastore is acceptable, then you why do you need to keep track of what is in memcache? Just select a random record from the datastore. – Peter Recore Jan 11 '12 at 21:22
 - 
                    
 
2
            No, there's no way to select a random element from memcache. It's not designed for that, either.
        Nick Johnson
        
- 100,655
 - 16
 - 128
 - 198
 
- 
                    You helped provide this solution http://stackoverflow.com/a/3451052/281021. Can memcache help there in any way? – Albert Jan 16 '12 at 11:49
 - 
                    1@Albert No, because there's no way to query on memcache, only get by key. – Nick Johnson Jan 16 '12 at 12:09