Skip to content

Instantly share code, notes, and snippets.

@adimania
Last active December 7, 2016 18:06
Show Gist options
  • Save adimania/379ddedc1fee6280b30ffccf17aede60 to your computer and use it in GitHub Desktop.
Save adimania/379ddedc1fee6280b30ffccf17aede60 to your computer and use it in GitHub Desktop.
Storing big files in memcached
import math
from pymemcache.client.base import Client
client = Client(('172.17.0.2', 11211))
chunk_size = 1000000.0
def get_loops(size):
return math.ceil(size/chunk_size)
def set_file(name, file_obj):
"""store a file in memcache by its name"""
size = len(file_obj.read())
file_obj.seek(0)
loops = int(get_loops(size))
client.set(name, loops)
for i in range(loops):
data = file_obj.read(int(chunk_size))
client.set("%s%s%s" % (name, "_", str(i)), data)
def get_file(name):
"""retrieve a file from memcache by its name"""
loops = int(client.get(name))
file = ''
for i in range(loops):
data = client.get("%s%s%s" % (name,"_", str(i)))
file = file + data
return file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment