Skip to content

Commit 654b423

Browse files
committed
add version 0 caching
1 parent f50816b commit 654b423

File tree

1 file changed

+35
-1
lines changed
  • materializationengine/blueprints/client

1 file changed

+35
-1
lines changed

materializationengine/blueprints/client/api2.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from dynamicannotationdb.models import AnalysisTable, AnalysisVersion
44

55
from cachetools import TTLCache, cached, LRUCache
6+
from cachetools.keys import hashkey
7+
from functools import wraps
68
from flask import Response, abort, request, current_app, g
79
from flask_accepts import accepts
810
from flask_restx import Namespace, Resource, inputs, reqparse
@@ -1752,12 +1754,44 @@ def assemble_view_dataframe(datastack_name, version, view_name, data, args):
17521754
return df, column_names, warnings
17531755

17541756

1757+
# Define your cache (LRU Cache with a maximum size of 100 items)
1758+
view_mat_cache = LRUCache(maxsize=100)
1759+
view_live_cache = TTLCache(maxsize=100, ttl=60)
1760+
1761+
1762+
def conditional_view_cache(func):
1763+
@wraps(func)
1764+
def wrapper(*args, **kwargs):
1765+
# Generate a cache key
1766+
key = hashkey(*args, **kwargs)
1767+
1768+
# Check if the 'version' argument is in the kwargs and if it is set to 0
1769+
if kwargs.get("version") == 0:
1770+
# Check if the result is in the live cache
1771+
if key in view_live_cache:
1772+
return view_live_cache[key]
1773+
else:
1774+
result = func(*args, **kwargs)
1775+
view_live_cache[key] = result
1776+
return result
1777+
else:
1778+
# Check if the result is in the materialized cache
1779+
if key in view_mat_cache:
1780+
return view_mat_cache[key]
1781+
else:
1782+
result = func(*args, **kwargs)
1783+
view_mat_cache[key] = result
1784+
return result
1785+
1786+
return wrapper
1787+
1788+
17551789
@client_bp.route(
17561790
"/datastack/<string:datastack_name>/version/<int:version>/view/<string:view_name>/info"
17571791
)
17581792
class MatViewSegmentInfo(Resource):
17591793
method_decorators = [
1760-
cached(TTLCache(maxsize=256, ttl=60 * 60 * 24)),
1794+
conditional_view_cache,
17611795
validate_datastack,
17621796
limit_by_category("query"),
17631797
auth_requires_permission("view", table_arg="datastack_name"),

0 commit comments

Comments
 (0)