From d29ace8912b2e32f90e0c41e73bb39b215f0182d Mon Sep 17 00:00:00 2001 From: Fuhu Xia Date: Fri, 31 Mar 2023 13:45:00 -0400 Subject: [PATCH] add max_age_in_days default 2 for cache --- README.md | 4 ++++ ckanext/report/model.py | 11 +++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a750b1c..4c19d8e 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,10 @@ Enable the plugin. In your config (e.g. development.ini or production.ini) add ` ckan.plugins = report +(Optional) Set max age to avoid stale cache. In the CKAN config add ``ckanext-report.max_age_in_days`` and assign an integer value. Default is 2 days. e.g.: + + ckanext-report.max_age_in_days = 2 + ## Command-line interface diff --git a/ckanext/report/model.py b/ckanext/report/model.py index 6151770..059641c 100644 --- a/ckanext/report/model.py +++ b/ckanext/report/model.py @@ -8,6 +8,8 @@ from sqlalchemy.orm import mapper from ckan import model +import ckan.plugins as p +from ckan.plugins.toolkit import config try: from collections import OrderedDict # from python 2.7 except ImportError: @@ -81,7 +83,7 @@ def get(cls, object_id, key, convert_json=False, max_age=None): if not item: return (None, None) - if max_age: + if max_age is not None: age = datetime.datetime.utcnow() - item.created if age > max_age: log.debug('Cache not returned - it is older than requested %s/%s %r > %r', @@ -105,7 +107,12 @@ def get(cls, object_id, key, convert_json=False, max_age=None): @classmethod def get_if_fresh(cls, *args, **kwargs): - return cls.get(*args, max_age=datetime.timedelta(days=2), **kwargs) + # Get cache if it is less than 2 (or as configured) days old. + max_age_in_days = p.toolkit.asint(config.get( + 'ckanext-report.max_age_in_days', 2 + )) + max_age = datetime.timedelta(days=max_age_in_days) + return cls.get(*args, max_age=max_age, **kwargs) @classmethod def set(cls, object_id, key, value, convert_json=False):