def caches(request, tmp_path, redis_server, redis_sentinel_server, memcache_server): """ Time-based cache parametrized to generate a cache for each cache_type (eg backend) """ if request.param == 'redissentinel' and os.getenv('TRAVIS', 'no') == 'yes': pytest.skip( "Unfortunately on Travis Redis Sentinel currently can't be installed" ) # uwsgi tests should only run if running under uwsgi if request.param == 'uwsgi': try: import uwsgi except ImportError: pytest.skip( "uWSGI could not be imported, are you running under uWSGI?") return None # build a dict of caches for each eviction strategy caches = { eviction_strategy: Cache( config={ 'CACHE_EVICTION_STRATEGY': eviction_strategy, 'CACHE_TYPE': request.param, 'CACHE_THRESHOLD': CACHE_THRESHOLD, 'CACHE_DIR': tmp_path if request.param == 'filesystem' else None, 'CACHE_REDIS_PORT': REDIS_PORT }) for eviction_strategy in EVICTION_STRATEGIES } return caches
def context( ebl_ai_client, word_repository, sign_repository, file_repository, photo_repository, folio_repository, fragment_repository, text_repository, changelog, bibliography_repository, annotations_repository, lemma_repository, user, parallel_line_injector, ): return ebl.context.Context( ebl_ai_client=ebl_ai_client, auth_backend=NoneAuthBackend(lambda: user), word_repository=word_repository, sign_repository=sign_repository, public_file_repository=file_repository, photo_repository=photo_repository, folio_repository=folio_repository, fragment_repository=fragment_repository, changelog=changelog, bibliography_repository=bibliography_repository, text_repository=text_repository, annotations_repository=annotations_repository, lemma_repository=lemma_repository, cache=Cache({"CACHE_TYPE": "null"}), parallel_line_injector=parallel_line_injector, )
def test_cache_delete_many_ignored(): c = Cache( config={ "CACHE_TYPE": "simple", "CACHE_IGNORE_ERRORS": True, "EVICTION_STRATEGY": 'time-based' }) cache = c.cache cache.set("hi", "hello") assert cache.get("hi") == "hello" cache.delete_many("ho", "hi") assert cache.get("hi") is None
import falcon from falcon_caching import Cache cache = Cache(config={'CACHE_TYPE': 'simple'}) import json import libs.utils import math from datetime import datetime from datetime import timezone from dateutil.relativedelta import relativedelta import mongodb from apispec import APISpec from apispec.ext.marshmallow import MarshmallowPlugin from falcon_apispec import FalconPlugin from marshmallow import Schema, fields import calendar class CirculationSupplyResource: def __init__(self): self.mydb=mongodb.Mongodb() self.tools=libs.utils.tools() self.currencies=self.mydb.find("currencies") #@cache.cached(timeout=60) def on_get(self, req, resp): #If an end timestamp is specified, use this one, or timestamp=now() nbDatapoints=50
from falcon_caching import Cache # Setup the cache instance cache = Cache( config={ # Sets management strategy of the cache 'CACHE_EVICTION_STRATEGY': 'time-based', # Sets type of cache ('null' sets no cache, used for development and unit test) # A production environment should use onother type of cache such as 'simple.' 'CACHE_TYPE': 'null' })
def test_caching_content_type_json_only(tmp_path, redis_server, redis_sentinel_server, memcache_server, cache_type, eviction_strategy): """ Testing that the Content-Type header does NOT get cached when the CACHE_CONTENT_TYPE_JSON_ONLY = True is set, which means that no msgpack serialization is used in this case, which should mean this gives a nice performance bump when the Content-Type header caching is not required because the application/json Content-Type (which is the default in Falcon) is sufficient for the app. """ if cache_type == 'redissentinel' and os.getenv('TRAVIS', 'no') == 'yes': pytest.skip( "Unfortunately on Travis Redis Sentinel currently can't be installed" ) # uwsgi tests should only run if running under uwsgi if cache_type == 'uwsgi': try: import uwsgi except ImportError: pytest.skip( "uWSGI could not be imported, are you running under uWSGI?") return None if True: cache = Cache( config={ 'CACHE_EVICTION_STRATEGY': eviction_strategy, 'CACHE_TYPE': cache_type, 'CACHE_THRESHOLD': CACHE_THRESHOLD, 'CACHE_DIR': tmp_path if cache_type == 'filesystem' else None, 'CACHE_REDIS_PORT': REDIS_PORT, 'CACHE_CONTENT_TYPE_JSON_ONLY': True # this is what we are testing here! }) # a resource where a custom response Cache-Type is set class CachedResource: @cache.cached(timeout=1) def on_get(self, req, resp): resp.content_type = 'mycustom/verycustom' if FALCONVERSION_MAIN < 3: resp.body = json.dumps( {'num': random.randrange(0, 100000)}) else: resp.text = json.dumps( {'num': random.randrange(0, 100000)}) app = API(middleware=cache.middleware) app.add_route('/randrange_cached', CachedResource()) client = testing.TestClient(app) # before making the first call let's ensure that the cache is empty if cache_type == 'memcached': cache.cache.delete("/randrange_cached:GET") else: cache.clear() # the first call will cache it result1 = client.simulate_get('/randrange_cached') assert result1.headers['Content-Type'] == 'mycustom/verycustom' # the second call returns it from cache - but as the content-type # is NOT cached, it will return the default 'application/json' type result2 = client.simulate_get('/randrange_cached') assert result1.json['num'] == result2.json['num'] assert result2.headers['Content-Type'] == 'application/json'
def cached_client(context): api = ebl.app.create_app( attr.evolve(context, cache=Cache(config={"CACHE_TYPE": "simple"}))) return testing.TestClient(api)
def create_cache() -> Cache: return Cache(config=load_config())