MemcachedKeyLengthError: Key length is > 250


Warning: sizeof(): Parameter must be an array or an object that implements Countable in /var/www/clients/client1/web1/web/wp-content/plugins/ad-injection/ad-injection.php on line 824

Warning: count(): Parameter must be an array or an object that implements Countable in /var/www/clients/client1/web1/web/wp-content/plugins/ad-injection/ad-injection.php on line 831

Fix for python-memcached MemcachedKeyLengthError

from django.core.cache.backends import memcached
from django.utils.encoding import smart_str

"""
Simple Django cache backend overrides for dealing with python-memcached
differences between cmemcache.

For Django 1.2. All methods except `set_many` and `delete_many` will work for Django 1.1

Use by pointing to it in settings, for example:

CACHE_BACKEND = myproject.contrib.thisfile://127.0.0.1:11211
"""
class CacheClass(memcached.CacheClass):
    "Filter key through `_smart_key` before sending to backend for all cases"
    def __init__(self, server, params):
        super(CacheClass,self).__init__(server, params)
    
    def add(self, key, value, timeout=0):
        return super(CacheClass, self).add(self._smart_key(key), value, timeout)
    
    def get(self, key):
        return super(CacheClass, self).get(self._smart_key(key))
    
    def set(self, key, value, timeout=0):
        super(CacheClass, self).set(self._smart_key(key), value, timeout)
    
    def delete(self, key):
        super(CacheClass, self).delete(self._smart_key(key))
    
    def _smart_key(self, key):
        "Truncate all keys to 250 or less and remove control characters"
        if key:
            return smart_str(''.join([c for c in key
                                        if ord(c) > 32 and ord(c) != 127]))[:250]
        return key



coded by nessus