This site runs best with JavaScript enabled.

MemcachedKeyLengthError: Key length is > 250


Fixing a recurring problem with Django's built-in cache page decorator

Lately I was getting this error frequently as I was using Django's built in cache_page decorator to cache some views.

memcache in check_key
MemcachedKeyLengthError: Key length is > 250

Basically the problem is that Memcached only allows a 250 char key and some of my view names were pretty long and so it was creating keys greater than 250 chars.

I found a quick fix to hash the key with an md5 hash if the key is going to be over 250 characters. You can modify the function that creates the key.

In my settings file I added the following:

1import hashlib
2
3...
4
5def hash_key(key, key_prefix, version):
6 new_key = ':'.join([key_prefix, str(version), key])
7 if len(new_key) > 250:
8 m = hashlib.md5()
9 m.update(new_key)
10 new_key = m.hexdigest()
11 return new_key
12
13CACHES = {
14 'default': {
15 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
16 'LOCATION': '127.0.0.1:11211',
17 'KEY_FUNCTION': hash_key,
18 }
19}

The reason why I only hash if the key is going to be over 250 characters is because 1) hashing is CPU intensive and I only want to do it when I have to; 2) I prefer to have my memcached keys human readable when possible; 3) less likely to have collision problems with duplicate hashes.

I thank Russell Keith-Magee for these tips.

Discuss on TwitterEdit post on GitHub

Share article
Dustin Davis

Dustin Davis is a software engineer, people manager, hacker, and entreprenuer. He loves to develop systems and automation. He lives with his wife and five kids in Utah.

Join the Newsletter



Dustin Davis