Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MemcachedKeyCharacterError #21

Open
DragosChirila opened this issue Apr 5, 2017 · 5 comments
Open

MemcachedKeyCharacterError #21

DragosChirila opened this issue Apr 5, 2017 · 5 comments

Comments

@DragosChirila
Copy link

DragosChirila commented Apr 5, 2017

Hi,

I am trying to use the @memoize decorator to cache a method in a view, with python-memcached present, like below:

class BlogView(generic.ListView, CoreBreadcrumbs):
    template_name = 'blog/blog.html'
    context_object_name = 'items'

    @memoize(timeout=3600)
    def get_paginate_by(self, queryset):
        return BlogSettings.objects.get().howmany_perpage
...

and I get the following error:

MemcachedKeyCharacterError: Control/space characters not allowed (key=':1:blog.views.BlogView.get_paginate_by.<blog.views.BlogView object at 0x03518AD0>_memver')

Full traceback - https://gist.github.com/DragosChirila/ce814ffd2a4d24e82c78c8cd013288e6

Am I doing something wrong here? Could you please advice?

@tvavrys
Copy link

tvavrys commented Apr 5, 2017

Hi there,

please try to overwrite __repr__ with something simpler or use Redis. It seems like Memcached has character limitations for keys.

@DragosChirila
Copy link
Author

DragosChirila commented Apr 5, 2017

Thanks! Indeed, using something like

    def __repr__(self):
        """ Overwrite this for memcache key generation.
        """
        return '{}.{}'.format(self.__module__, self.__class__.__name__)

seems to work.

On the other hand, I tried to use the make_name parameter but without success. Do you have a working example with a custom make_name method?

@tvavrys
Copy link

tvavrys commented Apr 5, 2017

What do you mean by "use the make_name parameter"? What is it?

EDIT: Nevermind. I get it now :) I'll look into it later. Thanks.

@tvavrys
Copy link

tvavrys commented Apr 5, 2017

Looking at the code - make_name has to be callable. What did you use? String?

@DragosChirila
Copy link
Author

DragosChirila commented Apr 5, 2017

I did it like this:

    @memoize(timeout=3600, make_name=make_name_for_view)
    def get_paginate_by(self, queryset):
        return BlogSettings.objects.get().howmany_perpage

where

def make_name_for_view(name):
    return 'my custom name'

But, make_name_for_view never gets called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants