def test_request_cache_control_disobedience(self): c = SimpleCache() cfg = Config(request_controls_cache=False) s = Store(c, cfg) r = Retrieval(c, cfg) with a.test_request_context('/foo', headers=(('cache-control', 'no-store'), )): self.assertTrue(r.should_fetch_response()) with a.test_request_context('/foo', headers=(('cache-control', 'no-store'), )): self.assertTrue(s.should_cache_response(Response())) with a.test_request_context('/foo', headers=(('cache-control', 'no-store'), )): self.assertTrue(s.should_cache_response(Response())) resp = Response() resp.date = datetime.utcnow() - timedelta(seconds=100) resp.cache_control.max_age = 200 with a.test_request_context('/foo', headers=(('cache-control', 'min-fresh=150'), )): f = self.r.response_freshness_seconds(resp) try: r.verify_response_freshness_or_miss(resp, f) except CacheMiss: self.fail( 'unexpected CacheMiss when ignoring request cache control')
def easy_setup(app, cache=None): cache = cache or SimpleCache() config = storage.Config( resource_exemptions=('/static/', ), master_salt=utils.make_salt() if app.debug else '', ) handlers.RequestHandler(cache, app, config) handlers.ResponseHandler(cache, app, config)
def setUp(self): self.recached = False def dispatcher(salt): self.recached = True self.c = SimpleCache() cfg = Config(preemptive_recache_seconds=10, preemptive_recache_callback=dispatcher) self.s = Store(self.c, cfg) self.r = Retrieval(self.c, cfg)
def __init__(self, introspection_url, client_id, client_secret, verify_ssl_server=True): self.ctx = ssl.create_default_context() if not verify_ssl_server: self.ctx.check_hostname = False self.ctx.verify_mode = ssl.CERT_NONE self._introspection_url = introspection_url self._client_id = client_id self._client_secret = client_secret self._token_cache = SimpleCache() self.logger = logging.getLogger(__name__)
import hashlib import json import os import time from typing import cast, Dict from cachelib.simple import SimpleCache cache = SimpleCache() class Authentication: USERS_FILENAME = "users.json" def __init__(self, data_folder: str, password_salt: str, failed_login_delay_base: int) -> None: self.contents = {} # type: Dict with open(os.path.join(".", data_folder, self.USERS_FILENAME)) as file: self.contents = json.load(file) self.password_salt = password_salt self.failed_login_delay_base = failed_login_delay_base self.data_folder = data_folder def is_valid(self, username: str, password: str) -> bool: if username not in self.contents: self._failed_attempt(username) return False if self._hash_password(password) != self.contents[username]["password"]: self._failed_attempt(username) return False return True
from flask import session from cachelib.simple import SimpleCache import pickle from datetime import datetime from surv.game.game import Game c = SimpleCache() def save_game(game): string = pickle.dumps(game) c.set(session['id'], string) def load_game(): try: string = c.get(session['id']) game = pickle.loads(string) return game except: return new_game() def new_game(): id = str(datetime.now()) if 'id' in session: session.pop('id') session['id'] = id g = Game() save_game(g)
import binascii import datetime import functools import os from typing import Any, Callable, Dict, List from cachelib.simple import SimpleCache from flask import make_response, request from decksite import get_season_id from magic import seasons from shared_web import localization CACHE = SimpleCache() # type: ignore def cached() -> Callable: return cached_impl(cacheable=True, must_revalidate=True, client_only=False, client_timeout=1 * 60 * 60, server_timeout=5 * 60) # pylint: disable=too-many-arguments def cached_impl(cacheable: bool = False, must_revalidate: bool = True, client_only: bool = True, client_timeout: int = 0, server_timeout: int = 5 * 60, key: str = 'view{id}{locale}') -> Callable: """ @see https://jakearchibald.com/2016/caching-best-practices/ https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching """ def decorator(f: Callable) -> Callable: @functools.wraps(f)
def setUp(self): self.c = SimpleCache() self.s = Store(self.c) self.r = Retrieval(self.c)
def setUp(self): self.c = SimpleCache()
from resources.dining import Dining, DiningInformation, DiningSearch, DiningToday #from resources.weather import Weather from resources.wifi import Wifi, WifiNearMe from resources.laundry import Laundry from resources.main import Main from resources.free_food import FreeFood from resources.ews_status import EWSStatus from resources.athletic_schedule import AthleticSchedule from resources.buildings import Buildings from resources.directory import FacultyDirectory from resources.daily_illini import News, SubCategoryNews, SportsNews, RecentNews from resources.calendar import Calendar app = Flask(__name__) api = Api(app) cache = SimpleCache(app) # Define routes api.add_resource(Main, '/') '''Dining''' api.add_resource(DiningToday, '/dining/<string:hall>') api.add_resource(Dining, '/dining/<string:hall>/<string:dateFrom>/<string:dateTo>') api.add_resource(DiningSearch, '/dining/search/<string:query>') api.add_resource(DiningInformation, '/dining/information') '''Wifi''' api.add_resource(Wifi, '/wifi') #api.add_resource(WifiNearMe, '/wifi/<string:latitude>/<string:longitude>') #api.add_resource(Weather, '/weather')
def __init__(self, app=None): """Initialize the cache.""" super(ImageSimpleCache, self).__init__(app=app) self.cache = SimpleCache()