class TestRedisCache(object): def setup(self): self.conn = Mock() self.cache = RedisCache(self.conn) def test_set_expiration(self): self.cache.set("foo", "bar", expires=datetime(2014, 2, 2)) assert self.conn.setex.called
def new_session(state=None, token=None): sess = getattr(g, 'session', None) if not sess: if 'github_token' in session and not token: token = session['github_token'] sess = g.session = CacheControl(OAuth2Session( consumer_key, state=state, token=token), RedisCache(redis)) return sess
def cached_session_init(self, *args, **kwargs): real_session_init(self, *args, **kwargs) if config.HTTP_CACHE_URL: adapter = CustomCacheControlAdapter( max_retries=RETRY, cache=RedisCache(utils.get_redis_for_http_cache()), serializer=CustomSerializer(), ) else: adapter = requests.adapters.HTTPAdapter(max_retries=RETRY) self.mount(f"https://api.{config.GITHUB_DOMAIN}", adapter) self.mount(config.SUBSCRIPTION_URL, adapter) if config.WEBHOOK_APP_FORWARD_URL: self.mount(config.WEBHOOK_APP_FORWARD_URL, adapter) if config.WEBHOOK_MARKETPLACE_FORWARD_URL: self.mount(config.WEBHOOK_MARKETPLACE_FORWARD_URL, adapter)
def setup(self): self.conn = Mock() self.cache = RedisCache(self.conn)
import redis import requests import cachecontrol from cachecontrol.caches import RedisCache from cachecontrol.heuristics import ExpiresAfter from django.conf import settings from django.shortcuts import render from django.views.decorators.http import require_http_methods # TODO: mechanism to bubble up details from individual services? # set up logger and requests session cache logger = logging.getLogger(__name__) pool = redis.ConnectionPool(host=settings.REDIS_HOST, port=settings.REDIS_PORT) cache = RedisCache(redis.Redis(connection_pool=pool)) session = cachecontrol.CacheControl(requests.Session(), cache, heuristic=ExpiresAfter(seconds=30)) session.verify = False @require_http_methods(['GET']) def index(request): config = settings.WATCHMEN watchmen = [] with futures.ThreadPoolExecutor(max_workers=5) as executor: future_to_bucket = {} for env in config: for project, url in config[env].iteritems(): future = executor.submit(get_watchman_data, url)