How to limit the value size for a PyMemcacheCache value?

Hi,

I’m using django.core.cache.backends.memcached.PyMemcacheCache as my cache backend. I’m not sure of the best way to limit the value of the cache to 3mb.

Before I was using MemcachedCache and I could set this with the option 'server_max_value_length': 1024 * 1024 * 3. I couldn’t find any options with PyMemcacheCache so guessing I need to handle this myself somehow.

I’m currently overriding PyMemcacheCache’s set method and checking the value isn’t over 3mb with sys.getsizeof but I’m thinking this may not be the best approach.

Any ideas?

Do you want to limit the individual size of (key->)values, or the maximum size of the whole cache?

if the latter, vi /etc/sysconfig/memcached, or elsewhere depending on your OS? -_-

Woo. I forgot I could set it on the cache directly. The limit has already been set too! Silly me :smiling_face_with_tear:. Cheers for help.

sure thing… though this isn’t stricly django related :wink:

FYI sys.getsizeof is not the cached value’s size, it is the size of the object in Python, which can be significantly different to the serialized version.

For example, the int value 1 is 28 bytes in Python but 5 bytes pickled:

In [1]: import pickle

In [2]: x = 1

In [3]: import sys

In [4]: sys.getsizeof(1)
Out[4]: 28

In [5]: len(pickle.dumps(x))
Out[5]: 5
1 Like

28 bytes Oo blimey … I should brush up on my C xD