|
3 | 3 | from dynamicannotationdb.models import AnalysisTable, AnalysisVersion
|
4 | 4 |
|
5 | 5 | from cachetools import TTLCache, cached, LRUCache
|
| 6 | +from cachetools.keys import hashkey |
| 7 | +from functools import wraps |
6 | 8 | from flask import Response, abort, request, current_app, g
|
7 | 9 | from flask_accepts import accepts
|
8 | 10 | from flask_restx import Namespace, Resource, inputs, reqparse
|
@@ -1752,12 +1754,44 @@ def assemble_view_dataframe(datastack_name, version, view_name, data, args):
|
1752 | 1754 | return df, column_names, warnings
|
1753 | 1755 |
|
1754 | 1756 |
|
| 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 | + |
1755 | 1789 | @client_bp.route(
|
1756 | 1790 | "/datastack/<string:datastack_name>/version/<int:version>/view/<string:view_name>/info"
|
1757 | 1791 | )
|
1758 | 1792 | class MatViewSegmentInfo(Resource):
|
1759 | 1793 | method_decorators = [
|
1760 |
| - cached(TTLCache(maxsize=256, ttl=60 * 60 * 24)), |
| 1794 | + conditional_view_cache, |
1761 | 1795 | validate_datastack,
|
1762 | 1796 | limit_by_category("query"),
|
1763 | 1797 | auth_requires_permission("view", table_arg="datastack_name"),
|
|
0 commit comments