Created
October 28, 2011 09:22
-
-
Save ojii/1321944 to your computer and use it in GitHub Desktop.
Dummy code for my thoughts described in https://groups.google.com/forum/#!topic/django-cms-developers/bFbhE1a-rTs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Dummy code for my thoughts described in https://groups.google.com/forum/#!topic/django-cms-developers/bFbhE1a-rTs | |
Note that this pseudo-implementation has problems where between `cache.get(...)` | |
in `get_cached` and `cache.set(...)` the value *could* have changed in the cache, | |
so some kind of better way to prevent overwriting changes done by others should | |
be implemented. | |
""" | |
# the local python in-memory cache | |
LOCAL_CACHE = {} | |
def get_cached(key, calculator): | |
""" | |
Returns the value returned by `calculator`. | |
Does so ideally by getting a pre-calculated value using `key` in the | |
`LOCAL_CACHE` dictionary. | |
""" | |
# get the list of PIDs or an empty list from the django cache (eg: memcached) | |
cached = cache.get(key, []) | |
# if our PID is in that list | |
if os.getpid() in cached: | |
# check if we already did the computation of the value | |
if key in LOCAL_CACHE: | |
# if so, return it | |
return LOCAL_CACHE[key] | |
else: | |
# else, aquire a write-lock for this process to prevent threading issues | |
with Lock(): | |
# fill the local cache | |
LOCAL_CACHE[key] = calculator() | |
# add this PID to the list in the django cache | |
cached.append(os.getpid()) | |
cache.set(key, cached) | |
# return the value | |
return LOCAL_CACHE[key] | |
else: | |
# if we're not in that list yet, aquire a write-lock for this process to prevent threading issues | |
with Lock(): | |
# fill the local cache | |
LOCAL_CACHE[key] = calculator() | |
# return the value | |
return LOCAL_CACHE[key] | |
def invalidate_cache(key): | |
""" | |
Invalidate the local cache for `key` and signal other processes using the | |
django cache that they should invalidate too. | |
""" | |
# delete the django (memcached?) cache | |
cache.delete(key) | |
# get a write lock, again for threading issues | |
with Lock(): | |
# delete the key from the local cache | |
del LOCAL_CACHE[key] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment