MemcachedKeyCharacterError: Control characters not allowed. It seems that it may happen when you specify an object etc. as a key with the cache function of django.
models.py
from django.db import models
class TestModel(models.Model):
test = models.CharField(verbose_name=u'test')
views.py
from django.core.cache import cache
def test_view(request):
obj = TestModel.objects.get(pk=1)
objs = cache.get(obj, None)
If you call the view above, you may get a'MemcachedKeyCharacterError: Control characters not allowed'error at'cache.get (obj, None)'.
models.py
from django.db import models
class TestModel(models.Model):
test = models.CharField(verbose_name=u'test')
def __unicode__(self):
return u"%s:%s" % (self.__class__.__name__, self.id)
You can avoid the error by defining a character string with the method called when the object is output as described above.
Cache key contains characters that will cause errors if used with memcached
views.py
from django.core.cache import cache
def test_view(request):
obj = TestModel.objects.get(pk=1)
objs = cache.get("aaaa bbbb", None)
As mentioned above, keys including spaces are also NG.
Recommended Posts