Python cache.Cache() Examples
The following are 18
code examples of cache.Cache().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
cache
, or try the search function
.
Example #1
Source File: thread.py From maniwani with MIT License | 6 votes |
def invalidate_board_cache(board_id: int): """Invalidates the cache for the given board in addition to the firehose.""" cache_connection = cache.Cache() slip_bitmasks = 0, 1, 3, 7 theme_list = app.config.get("THEME_LIST") or ("stock", "harajuku", "wildride") # invalidate full-page renders for bitmask in slip_bitmasks: for theme in theme_list: catalog_render_key = "board-%d-%d-%s-render" % (board_id, bitmask, theme) cache_connection.invalidate(catalog_render_key) firehose_render_key = "firehose-%d-%s-render" % (bitmask, theme) cache_connection.invalidate(firehose_render_key) # invalidate retrieved thread listings catalog_thread_key = "board-%d-threads" % board_id cache_connection.invalidate(catalog_thread_key) # invalidate firehose listing firehose_thread_key = "firehose-threads" cache_connection.invalidate(firehose_thread_key)
Example #2
Source File: BoardListCatalog.py From maniwani with MIT License | 6 votes |
def retrieve(self, board_id): session = db.session cache_connection = cache.Cache() board_cache_key = "board-%d-threads" % board_id cached_threads = cache_connection.get(board_cache_key) if cached_threads: deserialized_threads = json.loads(cached_threads) for thread in deserialized_threads: thread["last_updated"] = datetime.datetime.utcfromtimestamp(thread["last_updated"]) return deserialized_threads board = session.query(Board).filter(Board.id == board_id).one() thread_list = board.threads json_friendly = self._to_json(thread_list) cache_friendly = json.dumps(json_friendly, default=_datetime_handler) cache_connection.set(board_cache_key, cache_friendly) return json_friendly
Example #3
Source File: Firehose.py From maniwani with MIT License | 6 votes |
def _get_threads(self): firehose_cache_key = "firehose-threads" cache_connection = cache.Cache() cached_threads = cache_connection.get(firehose_cache_key) if cached_threads: deserialized_threads = json.loads(cached_threads) for thread in deserialized_threads: thread["last_updated"] = datetime.datetime.utcfromtimestamp(thread["last_updated"]) return deserialized_threads firehose_limit = app.config["FIREHOSE_LENGTH"] raw_threads = db.session.query(Thread).order_by(desc(Thread.last_updated)).limit(firehose_limit).all() threads = BoardCatalog()._to_json(raw_threads) for thread in threads: db_thread = db.session.query(Thread).get(thread["id"]) thread["board"] = db_thread.board cache_friendly = json.dumps(threads, default=_datetime_handler) cache_connection.set(firehose_cache_key, cache_friendly) return threads
Example #4
Source File: Post.py From maniwani with MIT License | 5 votes |
def render_post_collection(posts, context, extensions): cache_connection = cache.Cache() for post in posts: cache_key = post_render_cache_key(context, post["id"]) cached_render = cache_connection.get(cache_key) if cached_render: post["body"] = cached_render continue rendered_markdown = clean(markdown(post["body"], extensions=extensions), ALLOWED_TAGS, ALLOWED_ATTRIBUTES) cache_connection.set(cache_key, rendered_markdown) post["body"] = rendered_markdown
Example #5
Source File: resolver.py From aws-deployment-framework with Apache License 2.0 | 5 votes |
def __init__(self, parameter_store, stage_parameters, comparison_parameters): self.parameter_store = parameter_store self.stage_parameters = stage_parameters self.comparison_parameters = comparison_parameters self.sts = STS() self.cache = Cache()
Example #6
Source File: test_organizations.py From aws-deployment-framework with Apache License 2.0 | 5 votes |
def test_build_account_path(cls): cls.client = Mock() cache = Cache() cls.client.list_parents.return_value = stub_organizations.list_parents_root cls.client.describe_organizational_unit.return_value = stub_organizations.describe_organizational_unit assert cls.build_account_path('some_ou_id', [], cache) == 'some_ou_name'
Example #7
Source File: test_cache.py From aws-deployment-framework with Apache License 2.0 | 5 votes |
def cls(): return Cache()
Example #8
Source File: determine_event.py From aws-deployment-framework with Apache License 2.0 | 5 votes |
def lambda_handler(event, _): parameters = ParameterStore(region=REGION_DEFAULT, role=boto3) account_id = event.get( 'detail').get( 'requestParameters').get('accountId') organizations = Organizations(role=boto3, account_id=account_id) parsed_event = Event( event=event, parameter_store=parameters, organizations=organizations, account_id=account_id ) cache = Cache() account_path = "ROOT" if parsed_event.moved_to_root else parsed_event.organizations.build_account_path( parsed_event.destination_ou_id, [], # Initial empty array to hold OU Path, cache ) if parsed_event.moved_to_root or parsed_event.moved_to_protected: return parsed_event.create_output_object("adf-bootstrap/" + account_path) parsed_event.set_destination_ou_name() return parsed_event.create_output_object("adf-bootstrap/" + account_path)
Example #9
Source File: jinja_cache.py From maniwani with MIT License | 5 votes |
def _ensure_connection(self): if not self._cache_connection: self._cache_connection = cache.Cache()
Example #10
Source File: ThreadPosts.py From maniwani with MIT License | 5 votes |
def retrieve(self, thread_id): session = db.session thread = session.query(Thread).filter(Thread.id == thread_id).one() cache_connection = cache.Cache() cache_key = thread_posts_cache_key(thread_id) cached_posts = cache_connection.get(cache_key) if cached_posts: deserialized_posts = json.loads(cached_posts) for post in deserialized_posts: post["datetime"] = datetime.datetime.utcfromtimestamp(post["datetime"]) return deserialized_posts posts = self._json_friendly(thread.posts, thread) cache_connection.set(cache_key, json.dumps(posts, default=_datetime_handler)) return posts
Example #11
Source File: PostReplyPattern.py From maniwani with MIT License | 5 votes |
def url_for_post(post_id): cache_key = post_url_cache_key(post_id) cache_connection = cache.Cache() cached_url = cache_connection.get(cache_key) if cached_url: return cached_url from model.Thread import Thread thread = Thread.query.filter(Thread.posts.any(id=post_id)).one() post_url = url_for("threads.view", thread_id=thread.id) + "#" + str(post_id) cache_connection.set(cache_key, post_url) return post_url
Example #12
Source File: stack.py From quantipy with MIT License | 5 votes |
def __reduce__(self): arguments = (self.name, ) state = self.__dict__.copy() if 'cache' in state: state.pop('cache') state['cache'] = Cache() # Empty the cache for storage return self.__class__, arguments, state, None, self.iteritems()
Example #13
Source File: boards.py From maniwani with MIT License | 5 votes |
def catalog(board_id): current_theme = session.get("theme") or app.config.get("DEFAULT_THEME") or "stock" response_cache_key = "board-%d-%d-%s-render" % (board_id, get_slip_bitmask(), current_theme) cache_connection = cache.Cache() cached_response_body = cache_connection.get(response_cache_key) etag_value = "%s-%f" % (response_cache_key, time.time()) etag_cache_key = "%s-etag" % response_cache_key if cached_response_body: etag_header = request.headers.get("If-None-Match") current_etag = cache_connection.get(etag_cache_key) if etag_header: parsed_etag = parse_etags(etag_header) if parsed_etag.contains_weak(current_etag): return make_response("", 304) cached_response = make_response(cached_response_body) cached_response.set_etag(current_etag, weak=True) cached_response.headers["Cache-Control"] = "public,must-revalidate" return cached_response threads = BoardCatalog().retrieve(board_id) board = db.session.query(Board).get(board_id) board_name = board.name render_for_catalog(threads) tag_styles = get_tags(threads) template = render_template("catalog.html", threads=threads, board=board, board_name=board_name, tag_styles=tag_styles) uncached_response = make_response(template) uncached_response.set_etag(etag_value, weak=True) uncached_response.headers["Cache-Control"] = "public,must-revalidate" cache_connection.set(response_cache_key, template) cache_connection.set(etag_cache_key, etag_value) return uncached_response
Example #14
Source File: main.py From maniwani with MIT License | 5 votes |
def index(): cache_connection = cache.Cache() current_theme = session.get("theme") or app.config.get("DEFAULT_THEME") or "stock" response_cache_key = "firehose-%d-%s-render" % (get_slip_bitmask(), current_theme) cached_response_body = cache_connection.get(response_cache_key) etag_value = "%s-%f" % (response_cache_key, time.time()) etag_cache_key = "%s-etag" % response_cache_key if cached_response_body: etag_header = request.headers.get("If-None-Match") current_etag = cache_connection.get(etag_cache_key) if etag_header: parsed_etag = parse_etags(etag_header) if parsed_etag.contains_weak(current_etag): return make_response("", 304) cached_response = make_response(cached_response_body) cached_response.set_etag(current_etag, weak=True) cached_response.headers["Cache-Control"] = "public,must-revalidate" return cached_response greeting = open("deploy-configs/index-greeting.html").read() threads = Firehose().get_impl() tag_styles = get_tags(threads) template = render_template("index.html", greeting=greeting, threads=threads, tag_styles=tag_styles) uncached_response = make_response(template) uncached_response.set_etag(etag_value, weak=True) uncached_response.headers["Cache-Control"] = "public,must-revalidate" cache_connection.set(response_cache_key, template) cache_connection.set(etag_cache_key, etag_value) return uncached_response
Example #15
Source File: test_run_app.py From vmaas with GNU General Public License v2.0 | 5 votes |
def test_run_app(monkeypatch, caplog): """Test webapp init process.""" monkeypatch.setattr(Cache, 'reload', lambda _: None) app.create_app() assert f'Starting (version {VMAAS_VERSION}).' in caplog.messages
Example #16
Source File: threads.py From maniwani with MIT License | 4 votes |
def view(thread_id): thread = db.session.query(Thread).filter(Thread.id == thread_id).one() thread.views += 1 db.session.add(thread) db.session.commit() current_theme = session.get("theme") or app.config.get("DEFAULT_THEME") or "stock" response_cache_key = "thread-%d-%d-%s-render" % (thread_id, get_slip_bitmask(), current_theme) cache_connection = cache.Cache() view_key = "thread-%d-views" % thread_id cached_views = cache_connection.get(view_key) fetch_from_cache = True if cached_views is None: fetch_from_cache = False else: cached_views = int(cached_views) if fetch_from_cache and (thread.views / cached_views) >= app.config.get("CACHE_VIEW_RATIO", 0): fetch_from_cache = False etag_value = "%s-%f" % (response_cache_key, time.time()) etag_cache_key = "%s-etag" % response_cache_key if fetch_from_cache: etag_header = request.headers.get("If-None-Match") current_etag = cache_connection.get(etag_cache_key) if etag_header: parsed_etag = parse_etags(etag_header) if parsed_etag.contains_weak(current_etag): return make_response("", 304) cache_response_body = cache_connection.get(response_cache_key) if cache_response_body is not None: cached_response = make_response(cache_response_body) cached_response.set_etag(current_etag, weak=True) cached_response.headers["Cache-Control"] = "public,must-revalidate" return cached_response posts = ThreadPosts().retrieve(thread_id) render_for_threads(posts) board = db.session.query(Board).get(thread.board) num_posters = db.session.query(Poster).filter(Poster.thread == thread_id).count() num_media = thread.num_media() reply_urls = _get_reply_urls(posts) template = render_template("thread.html", thread_id=thread_id, board=board, posts=posts, num_views=thread.views, num_media=num_media, num_posters=num_posters, reply_urls=reply_urls) uncached_response = make_response(template) uncached_response.set_etag(etag_value, weak=True) uncached_response.headers["Cache-Control"] = "public,must-revalidate" cache_connection.set(view_key, str(thread.views)) cache_connection.set(response_cache_key, template) cache_connection.set(etag_cache_key, etag_value) return uncached_response
Example #17
Source File: generate_pipeline_inputs.py From aws-deployment-framework with Apache License 2.0 | 4 votes |
def main(): LOGGER.info('ADF Version %s', ADF_VERSION) LOGGER.info("ADF Log Level is %s", ADF_LOG_LEVEL) _create_inputs_folder() parameter_store = ParameterStore( DEPLOYMENT_ACCOUNT_REGION, boto3 ) s3 = S3(DEPLOYMENT_ACCOUNT_REGION, SHARED_MODULES_BUCKET) deployment_map = DeploymentMap( parameter_store, s3, ADF_PIPELINE_PREFIX ) sts = STS() role = sts.assume_cross_account_role( 'arn:aws:iam::{0}:role/{1}-readonly'.format( MASTER_ACCOUNT_ID, parameter_store.fetch_parameter('cross_account_access_role') ), 'pipeline' ) organizations = Organizations(role) clean(parameter_store, deployment_map) ensure_event_bus_status(ORGANIZATION_ID) try: auto_create_repositories = parameter_store.fetch_parameter('auto_create_repositories') except ParameterNotFoundError: auto_create_repositories = 'enabled' threads = [] _cache = Cache() for p in deployment_map.map_contents.get('pipelines', []): _source_account_id = p.get('default_providers', {}).get('source', {}).get('properties', {}).get('account_id', {}) if _source_account_id and int(_source_account_id) != int(DEPLOYMENT_ACCOUNT_ID) and not _cache.check(_source_account_id): rule = Rule(p['default_providers']['source']['properties']['account_id']) rule.create_update() _cache.add(p['default_providers']['source']['properties']['account_id'], True) thread = PropagatingThread(target=worker_thread, args=( p, organizations, auto_create_repositories, deployment_map, parameter_store )) thread.start() threads.append(thread) for thread in threads: thread.join()
Example #18
Source File: test_cache.py From vmaas with GNU General Public License v2.0 | 4 votes |
def test_cache(monkeypatch): """Test cache load and clear method""" monkeypatch.setattr(Cache, 'download', lambda _: True) cache_test = yaml_cache.load_test_cache() cache_test.dump_shelve(TEST_DUMP_FILE) cache = Cache(TEST_DUMP_FILE) assert len(cache.arch2id) == 3 assert len(cache.arch_compat) == 2 assert len(cache.cve_detail) == 1 assert len(cache.dbchange) == 5 assert len(cache.errata_detail) == 4 assert len(cache.errataid2name) == 4 assert len(cache.errataid2repoids) == 2 assert len(cache.evr2id) == 7 assert len(cache.id2arch) == 3 assert len(cache.id2evr) == 7 assert len(cache.id2packagename) == 3 assert len(cache.modulename2id) == 2 assert len(cache.nevra2pkgid) == 5 assert len(cache.package_details) == 8 assert len(cache.packagename2id) == 3 assert len(cache.pkgerrata2module) == 5 assert len(cache.pkgid2errataids) == 7 assert len(cache.pkgid2repoids) == 6 assert len(cache.productid2repoids) == 1 assert len(cache.repo_detail) == 5 assert len(cache.repolabel2ids) == 1 assert len(cache.src_pkg_id2pkg_ids) == 2 assert len(cache.updates) == 2 assert len(cache.updates_index) == 2 assert len(cache.content_set_id2label) == 2 assert len(cache.label2content_set_id) == 2 assert len(cache.content_set_id2pkg_name_ids) == 2 cache.clear() variables = vars(cache) assert len(variables) == 30 for name, var in variables.items(): if name == "filename": assert var == TEST_DUMP_FILE else: assert var == {}