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 (http://www.sample.com/mybigfile.exe); 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 = file.read() 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)
To memcached, any value you store is just a stream of data. Remember, though, that the maximum size of an object you can store in memcached is 1MB, but can be configured to be larger by using the -I option in memcached 1.4. 2 and later, or by modifying the source in versions before 1.4.
When memcached needs to store new data in memory, and the memory is already full, what happen is this: memcached searches for a a suitable* expired entry, and if one is found, it replaces the data in that entry.
How does Memcached work? Unlike databases that store data on disk or SSDs, Memcached keeps its data in memory. By eliminating the need to access disks, in-memory key-value stores such as Memcached avoid seek time delays and can access data in microseconds.
memcached can process over 50 million keys per second on a 48 core machine using only RAM and heavy batching.
As of memcache 1.4.2, this is a user-configurable parameter: ReleaseNotes142 * memcached @ Github
Configurable maximum item size
Many people have asked for memcached to be able to store items larger than 1MB, while it's generally recommended that one not do this, it is now supported on the commandline.
A few enlightened folk have also asked for memcached to reduce the maximum item size. That is also an option.
The new -I parameter allows you to specify the maximum item size at runtime. It supports a unit postfix to allow for natural expression of item size.
Examples:
memcached -I 128k # Refuse items larger than 128k. memcached -I 10m # Allow objects up to 10MB
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With