Reputation: 6175
Say you have to following code with Python and Django.
OBJECT_CACHE = {}
def get_my_objects(key, *args, **kwargs):
try:
return OBJECT_CACHE[key]
except KeyError:
OBJECT_CACHE[key] = # code to get object
return OBJECT_CACHE[key]
How will this behave in situations of multiple projects running the same codebase? Will OBJECT_CACHE
be unique for each project, or will it be mixed up with objects from all projects?
Upvotes: 1
Views: 1263
Reputation: 7644
It will be unique "cache" for each running process (how many that are depends on your environment and configuration)
But anyway, this is not the correct way to do this. Django has build in caching with a build in local memory cache which would be essentially the same as your approach but with the benefit that you can swap in "real" caching with no effort at all.
You add this to your settings.py:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake'
}
}
And you are done. Now you can access the cache using the standard Django interface:
>>> from django.core.cache import cache
>>> cache.set('my_key', 'hello, world!', 30)
>>> cache.get('my_key')
'hello, world!'
See the Django docs for for information.
Upvotes: 8