Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions ckanext/report/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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',
Expand All @@ -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):
Expand Down