Skip to content

Instantly share code, notes, and snippets.

@dolph
Last active January 22, 2016 15:49
Show Gist options
  • Save dolph/04bd4984c7d1f34ef821 to your computer and use it in GitHub Desktop.
Save dolph/04bd4984c7d1f34ef821 to your computer and use it in GitHub Desktop.
Show that cache invalidation occurs per dogpile.cache region
"""Illustrate dogpile.cache invalidation behavior.
dogpile.cache's invalidation behavior is surprising because dogpile does not
write to memcached when you tell it to invalidate an entry.
Invalidation works by setting a current timestamp (using time.time())
representing the "minimum creation time" for a value. Any retrieved value
whose creation time is prior to this timestamp is considered to be stale.
It does not affect the data in the cache in any way, and is also local to
this instance of CacheRegion.
Source: http://dogpilecache.readthedocs.org/en/latest/api.html
"""
import time
import dogpile.cache
REGION_COUNT = 3
# Build a few identically caches, all configured to point to the same memcached
# process. This is simulating multiple keystone processes all sharing a single
# memcached process.
regions = []
for x in range(REGION_COUNT):
regions.append(dogpile.cache.region.make_region().configure(
'dogpile.cache.memcached',
expiration_time=1,
arguments={
'memcached_expire_time': 2,
'url': '127.0.0.1'}))
# Set a key in one cache instance.
regions[0].set('key', True)
# Ensure that key is available to all cache regions.
for x in range(REGION_COUNT):
assert regions[x].get('key') is True
# Invalidate the *entire* cache region (all keys). This operation does not
# write to memcached!
regions[0].invalidate()
# Ensure the key is "missing" from the region we invalidated it from.
assert isinstance(regions[0].get('key'), dogpile.cache.api.NoValue)
# Show that the cached value is still available in *all other regions*.
for x in range(1, REGION_COUNT):
assert regions[x].get('key') is True
# Now wait until the *dogpile.cache* expiration passes for everyone...
time.sleep(1)
# Show that the cache is now invalid for everyone.
for x in range(REGION_COUNT):
assert isinstance(regions[x].get('key'), dogpile.cache.api.NoValue)
# Show that even after the cache is supposed to be expired, we can ignore the
# expiration time and fetch the value anyway.
for x in range(REGION_COUNT):
assert regions[x].get('key', ignore_expiration=True) is True
# Wait more time until the *memcached* expiration passes for everyone...
time.sleep(1)
# Show that the cache is now invalid for everyone, even if we ignore
# expiration.
for x in range(REGION_COUNT):
assert isinstance(
regions[x].get('key', ignore_expiration=True),
dogpile.cache.api.NoValue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment