Hello django friends,

Problems:
--------
1) MemcachedKeyCharacterError: Control characters not allowed
2) MemcachedKeyLengthError: Key length is > 250

We where conflicted with problem number one, but the solution should
also work for the second error which did not happen on our system.

System-Information:
-------------------
Host: Ubuntu Server 10_04
Django: Django-1.2.4
CACHE-backend: memcached

Solution:
--------
as suggested by
http://evanculver.com/2010/03/30/simple-fix-for-python-memcached-memcachedkeylengtherror-and-memcachedkeycharactererror/

I took his wrapper and adjusted it for django-1.2.4: (adjustment just
added default=None as it is commented in the provided code)
----------------------------------------------------
MyMemchacedWrapper.py
---------------------
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, default=None):
        return super(CacheClass, self).get(self._smart_key(key),
default=None) #here default=None had to be added for django1.2.4
    
    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"
        return smart_str(''.join([c for c in key
                                    if ord(c) > 32 and ord(c) !=
127]))[:250]

in local_settings.py we point to:
--------------------------------
CACHE_BACKEND = "store.cache.MyMemcachedWrapper.py://127.0.0.1:11211/"

Now everything works just fine.

please comment my solution. I feel uncomfortable with it, and want to
know how you solved the problem? There is not really a lot to find about
concerning these issues. I think you guys do use a totally other
solution, because I could hardly find anything about that issues.
(searchtime two days.)

How do you cache? and why is there not a lot more talk about the errors
on the web? Am I missing something?

Thanks for your replies in advance!
ionic


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to