cfischer cfischer - 10 months ago 53
Python Question

Maximum size of object that can be saved in memcached with

I need to return a rather big file (11MB) to the user. For certain reasons, I can't just provide a direct url to the file (; instead it must be accessed through code.

Instead of having to read it from disk over and over, I thought of saving it in memcached (if this is not a good idea, let me know). Everything seems to work, fine (no errors), but when I try to retrieve the file from memcached I always get None, as if the file wasn't cached.

Is there a size limit to what can be saved?

Here's the code:

def download_demo():
Returns the demo file
KEY = "xyz"
TIME = 86400 #24 hours

buff = memc.get(KEY)
if not buff:
file = open(FILENAME, 'r')
buff =
memc.set(KEY, buff, TIME)

print "Content-Type:application/x-download\nContent-Disposition:attachment;filename=%s\nContent-Length:%s\n\n%s" % (os.path.split(FILENAME)[-1], len(buff), buff)

Answer Source

There are two entries about that in the memcached FAQ :

The answer to the first one is (quoting, emphasis mine) :

The maximum size of a value you can store in memcached is 1 megabyte. If your data is larger, consider clientside compression or splitting the value up into multiple keys.

So I'm guessing your 11MB file is quite too big to fit in one memcached entry.

Increasing the size of an object is possible, as per other answers.