#7398 closed (fixed)
Making it easier to add custom cache backends
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Core (Cache system) | Version: | dev |
Severity: | Keywords: | ||
Cc: | lau@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I ran into an inconvenience with the Django cache. I wanted to add my own custom cache backend without having to muck about with the Django core. This proved to be a bit challenging since the backend importer uses a complete path to the module (e.g. "django.core.cache.backends.memcached").
I did a small patch which lets you define extra backend modules without touching the Django folder. Essentially it uses the BACKENDS dictionary in core/cache/init.py to keep information about the full import paths, like this:
BACKENDS = { # name for use in settings file --> path to the backend module 'memcached': 'django.core.cache.backends.memcached', 'locmem': 'django.core.cache.backends.locmem', 'file': 'django.core.cache.backends.filebased', 'db': 'django.core.cache.backends.db', 'dummy': 'django.core.cache.backends.dummy', } if hasattr(settings, "CACHE_BACKEND_MODULES"): BACKENDS.update(settings.CACHE_BACKEND_MODULES)
So if I wanted to add a new backend in my project, I'd do something like:
settings.py:
CACHE_BACKEND_MODULES = { 'penandpaper': 'backends.cache.penandpaper' }
and place the backend in my_app/backends/cache/.
There has been a bit of talk about it lately on the developer mailing list:
http://groups.google.com/group/django-developers/browse_thread/thread/e0fbaff909c44336
Attachments (3)
Change History (19)
by , 16 years ago
Attachment: | ease-custom-cache-backends.diff added |
---|
comment:1 by , 16 years ago
Needs documentation: | set |
---|
by , 16 years ago
Attachment: | ease-custom-cache-backends-with-documentation.diff added |
---|
Added documentation in docs/settings.txt
comment:2 by , 16 years ago
Needs documentation: | unset |
---|
comment:5 by , 16 years ago
Triage Stage: | Unreviewed → Ready for checkin |
---|
comment:6 by , 16 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Ready for checkin → Accepted |
I think a whole extra setting and a dictionary like this is too much for this feature. Let's handle it like database backends: if the cache name (the bit before the colon, ":") is not one of the standard names, it's treated as an import path. Everything after the colon is passed through as an argument, just like now.
So extra caches can be added anywhere on the Python path and we don't need an extra setting. Being able to redefine the existing names to mean something else isn't needed here. If you want to have "some other memcached", just import it under a different name using the extension mechanism in the above paragraph. The point is that every time we add an extra setting, it's one more bit of documentation, one more thing for people to wonder if they need, etc. Let's go for simplicity first.
follow-up: 8 comment:7 by , 16 years ago
So, you're proposing something like this in settings.py:
CACHE_BACKEND = "backends.cache.penandpaper://"
with something like this in django.core.conf:
backend_import = scheme if hasattr(BACKENDS, scheme): backend_import = BACKENDS[scheme] cache_class = getattr(__import__(backend_import, {}, {}, ['']), 'CacheClass')
Correct?
by , 16 years ago
Attachment: | importable-backends.diff added |
---|
Allows CACHE_BACKEND to be a path - doc formatting corrected
comment:10 by , 16 years ago
Patch needs improvement: | unset |
---|
I put together a patch to implement what we discussed earlier. Please take a peek and let me know what you think. =)
comment:12 by , 16 years ago
milestone: | → post-1.0 |
---|
comment:13 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Ease custom cache backends