Skip to content

Instantly share code, notes, and snippets.

@gbarboza
Last active February 12, 2016 19:36
Show Gist options
  • Select an option

  • Save gbarboza/f51d565f893aba5d1070 to your computer and use it in GitHub Desktop.

Select an option

Save gbarboza/f51d565f893aba5d1070 to your computer and use it in GitHub Desktop.
import time
import random
from boto.s3.bucket import Bucket
from boto.s3.connection import S3Connection
from boto.s3.key import Key
from dogpile.cache import make_region
import logging
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
class CachedKey(Key):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@property
def unix_timestamp(self):
return time.mktime(time.strptime(self.last_modified, '%a, %d %b %Y %H:%M:%S %Z'))
def get_contents_as_string(self, *args, **kwargs):
try:
v, last_mod = self.bucket._region.get(self.name)
except TypeError:
v, last_mod = None, 0.0
if self.unix_timestamp > last_mod:
logger.debug('Updating {} in {}'.format(self.name, self.bucket.name))
v, last_mod = super().get_contents_as_string(*args, **kwargs), self.unix_timestamp
self.bucket._region.set(self.name, (v, last_mod))
return v
def send_file(self, *args, **kwargs):
logging.debug('Dropping {} from cache'.format(self.name))
self.bucket._region.delete(self.name)
super().send_file(*args, **kwargs)
class CachedBucket(Bucket):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.key_class = CachedKey
self._region = make_region().configure(
'dogpile.cache.redis',
expiration_time=5 * 60,
arguments={
'url': 'redis://127.0.0.1:6379/0',
'distributed_lock': True,
}
)
self._region.invalidate()
s3 = S3Connection('id', 'key')
bucket = CachedBucket(s3, 'bucket')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment