def get(self): get_cache().clean() n = len(list(get_cache().keys())) keys = dict(status="OK", message=f"Cache cleaned, {n} keys left") mimetype = "application/json" header = "Content-Type" body = mimetype self.set_header(header, body) self.write(json.dumps(keys))
def get(self, query): r = get_cache().remove(query) mimetype = "application/json" header = "Content-Type" body = mimetype self.set_header(header, body) self.write(json.dumps(dict(query=query, removed=r)))
def get(self, query): contains = get_cache().contains(query) mimetype = "application/json" header = "Content-Type" body = mimetype self.set_header(header, body) self.write(json.dumps(dict(query=query, cached=contains)))
def get(self, query): keys = dict(keys=list(get_cache().keys())) mimetype = "application/json" header = "Content-Type" body = mimetype self.set_header(header, body) self.write(json.dumps(keys))
def test_cache_control(self): from liquer.cache import MemoryCache, set_cache, get_cache @command def cached(state): return state.with_caching(True).with_data(123) @command def cache_off(state): return state.with_caching(False).with_data(234) set_cache(MemoryCache()) assert evaluate("cached").get() == 123 assert evaluate("cache_off").get() == 234 assert get_cache().contains("cached") assert not get_cache().contains("cache_off") set_cache(None) reset_command_registry()
def get(self, query): metadata = get_cache().get_metadata(query) if metadata == False: metadata = dict(query=query, status="not available", cached=False) self.write(metadata) mimetype = "application/json" header = "Content-Type" body = mimetype self.set_header(header, body) self.write(json.dumps(metadata))
def test_cache_control(self): from liquer.cache import MemoryCache, set_cache, get_cache @first_command def cached(context): context.enable_cache() return 123 @command def cache_off(x, context): context.disable_cache() return 234 set_cache(MemoryCache()) assert evaluate("cached").get() == 123 assert evaluate("cache_off").get() == 234 assert get_cache().contains("cached") assert not get_cache().contains("cache_off") set_cache(None) reset_command_registry()
def get(self, query): """Main service for evaluating queries""" state = get_cache().get(query) if state is None: self.set_status(404) self.finish(f"404 - {query} not found in cache") b, mimetype, filename = response(state) header = "Content-Type" body = mimetype self.set_header(header, body) self.write(b)
def get_stored_metadata(query): """Get metadata for a query - if it is stored in cache or a store. """ if not isinstance(query, Query): query = parse(query) if query.is_resource_query(): rq = query.resource_query() header = rq.header key = rq.path() try: return get_store().get_metadata(key) except KeyNotFoundStoreException: return None else: return get_cache().get_metadata(str(query))
def queries_status(include_ready=False, reduce=True): import liquer.parser as lp import traceback try: cache = get_cache() data = [] for key in sorted(cache.keys()): metadata = cache.get_metadata(key) if metadata is None: continue progress = metadata.get("progress_indicators", []) + metadata.get( "child_progress_indicators", []) d = dict( query=key, short=lp.parse(key).short(), status=metadata.get("status", "none"), updated=metadata.get("updated", "?"), message=metadata.get("message", ""), progress=progress[:3], ) if include_ready or d["status"] != Status.READY.value: data.append((key, d)) data = [x[1] for x in sorted(data)] if reduce and len(data): reduced_data = [data[0]] for d, next_d in zip(data[1:], data[2:]): previous_d = reduced_data[-1] if not (previous_d["status"] == Status.EVALUATING_PARENT.value and d["status"] == Status.EVALUATING_PARENT.value and d["query"].startswith(previous_d["query"]) and next_d["query"].startswith(d["query"])): reduced_data.append(d) reduced_data.append(data[-1]) data = reduced_data return data except: return [ dict( query="", status="not available", updated="", message=traceback.format_exc(), progress=[], ) ]
def evaluate_ql_on(ql, state=None, cache=None): """This is equivalent to evaluate_query_on, but accepts decoded query (list of lists of strings).""" if cache is None: cache = get_cache() if state is None: state = State() elif not isinstance(state, State): state = State().with_data(state) cr = command_registry() for i, qcommand in enumerate(ql): if i == len(ql) - 1: if len(qcommand) == 1 and '.' in qcommand[0]: state.with_filename(qcommand[0]) break state = cr.evaluate_command(state, qcommand) if state.caching and not state.is_error and not state.is_volatile(): cache.store(state) return state
def cache_store_metadata(query): """Store metadata in cache. Allows to use liquer server as a remote cache. """ metadata = request.get_json(force=True) try: result_code = get_cache().store_metadata(metadata) result = dict(query=query, result=result_code, status="OK", message="OK", traceback="") except Exception as e: result = dict( query=query, result=False, status="ERROR", message=str(e), traceback=traceback.format_exc(), ) return jsonify(result)
def post(self, param): try: metadata = json.loads(self.request.body) query = metadata.get('query') result_code = get_cache().store_metadata(metadata) result = dict(query=query, result=result_code, status="OK", message="OK", traceback="") except Exception as e: result = dict( query=query, result=False, status="ERROR", message=str(e), traceback=traceback.format_exc(), ) mimetype = "application/json" header = "Content-Type" body = mimetype self.set_header(header, body) self.write(json.dumps(result))
def cache_keys(): """interface to cache keys""" keys = dict(keys=list(get_cache().keys())) return jsonify(keys)
def clean_cache(): print(f"clean cache {get_cache()}") get_cache().clean() return "Cache cleaned"
def cache_remove(query): """interface to cache remove""" r = get_cache().remove(query) return jsonify(dict(query=query, removed=r))
def cache_get_metadata(query): """Get cached metadata""" metadata = get_cache().get_metadata(query) if metadata == False: metadata = dict(query=query, status="not available", cached=False) return jsonify(metadata)
def cache_get(query): """Get cached metadata""" state = get_cache().get(query) if state is None: abort(404) return response(state)
def cache_clean(): """interface to cache clean""" get_cache().clean() n = len(list(get_cache().keys())) keys = dict(status="OK", message=f"Cache cleaned, {n} keys left") return jsonify(keys)
def cache(self): return get_cache()
def cache_contains(query): """interface to cache contains""" contains = get_cache().contains(query) return jsonify(dict(query=query, cached=contains))