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

Adding a prefix to cache keys #176

Closed
Andrew-Chen-Wang opened this issue Apr 6, 2021 · 4 comments
Closed

Adding a prefix to cache keys #176

Andrew-Chen-Wang opened this issue Apr 6, 2021 · 4 comments

Comments

@Andrew-Chen-Wang
Copy link
Collaborator

@Andrew-Chen-Wang

Could you point me where code I need to patch to achieve "adding a prefix to cache keys"?
https://github.com/Suor/django-cacheops#sharing-redis-instance

The main goal to add a prefix by hostname Thanks

Originally posted by @abodacs in #164 (comment)

All our cache keys are hashed with sha1 so there's really no chance of collision. If you really want to add a prefix, in your Django settings, you have a "CACHES" dictionary. Then create a new key called "cachalot" (or whatever you want the cache to be called) and it should have the same values (i.e. host, parser, etc.) as your "default" cache. Then set CACHALOT_CACHE to "cachalot" or whatever you previously called your cache.

@Andrew-Chen-Wang
Copy link
Collaborator Author

This issue was for @abodacs

@abodacs
Copy link

abodacs commented Jul 14, 2021

@Andrew-Chen-Wang

I think what was proposed at https://github.com/Suor/django-cacheops#sharing-redis-instance is to allow multi-tenancy, Is it possible at django-cachalot?

@Andrew-Chen-Wang
Copy link
Collaborator Author

Andrew-Chen-Wang commented Jul 14, 2021

@abodacs before I give the solution, I'm not entirely sure if adding a prefix to the generated cache key is really necessary (I guess it's helpful for invalidating caches manually) since we use sha1. But in any case, it is entirely possible. You just need to make your own functions and use our settings to point to those functions:

from cachalot.utils import get_query_cache_key, get_table_cache_key

def custom_get_query_cache_key(compiler):
    return "prefix" + get_query_cache_key(compiler)

def custom_get_table_cache_key(db_alias, table):
    return "prefix" + get_table_cache_key(db_alias, table)

You can then use these settings: https://django-cachalot.readthedocs.io/en/latest/quickstart.html#cachalot-query-keygen, just add:

CACHALOT_QUERY_KEYGEN = "path.to.module.custom_get_query_cache_key"
CACHALOT_TABLE_KEYGEN = "path.to.module.custom_get_table_cache_key"

Hope that helps!

Edit: if you're just going to hardcode a prefix, for compatibility sake if you don't use tests in Django, then you can write:

def custom_get_query_cache_key(*args, **kwargs):
    return "prefix" + get_query_cache_key(*args, **kwargs)

def custom_get_table_cache_key(*args, **kwargs):
    return "prefix" + get_table_cache_key(*args, **kwargs)

@abodacs
Copy link

abodacs commented Jul 14, 2021

Thanks, 💕
I'll try this

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

No branches or pull requests

2 participants