Return a Cache-Control max-age on the API's isp view
authorGu1 <gu1@cafai.fr>
Fri, 12 Dec 2014 17:43:10 +0000 (18:43 +0100)
committerGu1 <gu1@cafai.fr>
Fri, 12 Dec 2014 17:43:10 +0000 (18:43 +0100)
ffdnispdb/default_settings.py
ffdnispdb/views_api.py

index d2f3ff7..8e82916 100644 (file)
@@ -4,6 +4,7 @@ SQLALCHEMY_DATABASE_URI = 'sqlite:///../ffdn-db.sqlite'
 CRAWLER_MIN_CACHE_TIME = 60*60 # 1 hour
 CRAWLER_MAX_CACHE_TIME = 60*60*24*14 # 2 week
 CRAWLER_DEFAULT_CACHE_TIME = 60*60*12 # 12 hours
+CRAWLER_CRON_INTERVAL = 60*20 # used to return valid cache info in the API
 SYSTEM_TIME_ZONE = 'Europe/Paris'
 LANGUAGES = {
     'en': u'English',
index f890e41..d6a8287 100644 (file)
@@ -6,6 +6,7 @@ from collections import OrderedDict
 import sys
 import json
 import datetime
+import time
 
 from . import utils, db
 from .models import ISP, CoveredArea
@@ -193,7 +194,22 @@ class ISPResource(Resource):
             s = ISP.query.filter_by(id=isp_id, is_disabled=False).scalar()
             if not s:
                 raise ObjectNotFound
-            return self.isp_to_dict(s)
+            if s.json_url:
+                # compute next update time, based on crawler cron interval.
+                interval = current_app.config['CRAWLER_CRON_INTERVAL']
+                nupdt = time.mktime(s.next_update.timetuple())
+                nupdt = datetime.datetime.fromtimestamp(nupdt - (nupdt % interval) + interval + 60)
+
+                now = datetime.datetime.utcnow()
+                if nupdt > now:
+                    max_age = (nupdt - now).seconds
+                else:
+                    max_age = current_app.config['CRAWLER_MIN_CACHE_TIME']
+            else:
+                # default max-age for isp using the form
+                max_age = current_app.config['CRAWLER_MIN_CACHE_TIME']
+            headers = {'Cache-Control': 'max-age='+str(max_age)}
+            return self.isp_to_dict(s), 200, headers
         else:
             s = ISP.query.filter_by(is_disabled=False)
             return self.handle_list(s, self.isp_to_dict)