コード例 #1
0
 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))
コード例 #2
0
 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)))
コード例 #3
0
 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)))
コード例 #4
0
 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))
コード例 #5
0
ファイル: test_query.py プロジェクト: andreamlpython/liquer
    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()
コード例 #6
0
 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))
コード例 #7
0
    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()
コード例 #8
0
    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)
コード例 #9
0
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))
コード例 #10
0
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=[],
            )
        ]
コード例 #11
0
ファイル: query.py プロジェクト: andreamlpython/liquer
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
コード例 #12
0
ファイル: blueprint.py プロジェクト: orest-d/liquer
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)
コード例 #13
0
 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))
コード例 #14
0
ファイル: blueprint.py プロジェクト: orest-d/liquer
def cache_keys():
    """interface to cache keys"""
    keys = dict(keys=list(get_cache().keys()))
    return jsonify(keys)
コード例 #15
0
def clean_cache():
    print(f"clean cache {get_cache()}")
    get_cache().clean()
    return "Cache cleaned"
コード例 #16
0
ファイル: blueprint.py プロジェクト: orest-d/liquer
def cache_remove(query):
    """interface to cache remove"""
    r = get_cache().remove(query)
    return jsonify(dict(query=query, removed=r))
コード例 #17
0
ファイル: blueprint.py プロジェクト: orest-d/liquer
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)
コード例 #18
0
ファイル: blueprint.py プロジェクト: orest-d/liquer
def cache_get(query):
    """Get cached metadata"""
    state = get_cache().get(query)
    if state is None:
        abort(404)
    return response(state)
コード例 #19
0
ファイル: blueprint.py プロジェクト: orest-d/liquer
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)
コード例 #20
0
 def cache(self):
     return get_cache()
コード例 #21
0
ファイル: blueprint.py プロジェクト: orest-d/liquer
def cache_contains(query):
    """interface to cache contains"""
    contains = get_cache().contains(query)
    return jsonify(dict(query=query, cached=contains))