Commit 856399f1 authored by HERBERT Ryan's avatar HERBERT Ryan
Browse files

controllers/notification.py reworked caching system for a more fluid and responsive service

parent 5a739761
...@@ -8,6 +8,8 @@ if request.env.http_origin: ...@@ -8,6 +8,8 @@ if request.env.http_origin:
ACCESS_DENIED = "access denied" ACCESS_DENIED = "access denied"
NOTIFICATION_CACHE_PREFIX = 'notification_'
CACHE_EXPIRY = 3600
def index(): def index():
if not auth.is_admin() : if not auth.is_admin() :
...@@ -21,7 +23,7 @@ def index(): ...@@ -21,7 +23,7 @@ def index():
query=query) query=query)
def info(): def info():
user_id = auth.user.id if auth.user else None
query = db.notification[request.vars['id']] query = db.notification[request.vars['id']]
if auth.user: if auth.user:
rows = db((db.user_preference.user_id==auth.user.id) rows = db((db.user_preference.user_id==auth.user.id)
...@@ -33,8 +35,10 @@ def info(): ...@@ -33,8 +35,10 @@ def info():
preference='mail', preference='mail',
val=request.vars['id']) val=request.vars['id'])
notifications = db(db.notification).select(orderby=~db.notification.id) # Clear cache of this user
cache.ram.clear(regex=NOTIFICATION_CACHE_PREFIX + str(user_id))
notifications = db(db.notification).select(orderby=~db.notification.id)
return dict(query=query, return dict(query=query,
notifications=notifications) notifications=notifications)
...@@ -81,6 +85,8 @@ def add_form(): ...@@ -81,6 +85,8 @@ def add_form():
"args" : { "id" : id }, "args" : { "id" : id },
"message": "notification added"} "message": "notification added"}
log.info(res) log.info(res)
# Clear cache of all notifications
cache.ram.clear(regex=NOTIFICATION_CACHE_PREFIX + '*')
return gluon.contrib.simplejson.dumps(res, separators=(',',':')) return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
else : else :
...@@ -133,6 +139,8 @@ def edit_form(): ...@@ -133,6 +139,8 @@ def edit_form():
"args" : { "id" : request.vars['id'] }, "args" : { "id" : request.vars['id'] },
"message": "notification updated"} "message": "notification updated"}
log.info(res) log.info(res)
# Clear cache of all notifications
cache.ram.clear(regex=NOTIFICATION_CACHE_PREFIX + '*')
return gluon.contrib.simplejson.dumps(res, separators=(',',':')) return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
else : else :
...@@ -156,37 +164,47 @@ def delete(): ...@@ -156,37 +164,47 @@ def delete():
"success": "true", "success": "true",
"message": "notification " + request.vars['id'] + " deleted"} "message": "notification " + request.vars['id'] + " deleted"}
log.info(res) log.info(res)
# Clear cache of all notifications
cache.ram.clear(regex=NOTIFICATION_CACHE_PREFIX + '*')
return gluon.contrib.simplejson.dumps(res, separators=(',',':')) return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
# #
def get_active_notifications(): def get_active_notifications():
today = date.today() today = date.today()
#TODO globalise these values ?
cache_time = 3600
cache_type = cache.ram
user_id = auth.user.id if auth.user else None user_id = auth.user.id if auth.user else None
if (request.vars['type'] is not None): if user_id:
query = db( key = NOTIFICATION_CACHE_PREFIX + str(user_id)
((db.notification.expiration >= today) else:
| (db.notification.expiration == None)) key = NOTIFICATION_CACHE_PREFIX
& (db.notification.message_type == request.vars['type'])
).select( # Try try to overwrite current cache with None.
db.notification.ALL, db.user_preference.val, # If it works the cache has expired or this is the first call
left=db.user_preference.on( cached = cache.ram(key, lambda: None, time_expire=CACHE_EXPIRY)
(db.user_preference.val==db.notification.id) if not cached:
&(db.user_preference.user_id==user_id)), # No cache found: query database
cache=(cache_type, cache_time)) if (request.vars['type'] is not None):
else : query = db(
query = db( ((db.notification.expiration >= today)
(db.notification.expiration >= today) | (db.notification.expiration == None) | (db.notification.expiration == None))
).select( & (db.notification.message_type == request.vars['type'])
db.notification.ALL, db.user_preference.val, ).select(
left=db.user_preference.on( db.notification.ALL, db.user_preference.val,
(db.user_preference.val==db.notification.id) left=db.user_preference.on(
&(db.user_preference.user_id==user_id)), (db.user_preference.val==db.notification.id)
cache=(cache_type, cache_time)) &(db.user_preference.user_id==user_id)))
else :
query = db(
(db.notification.expiration >= today) | (db.notification.expiration == None)
).select(
db.notification.ALL, db.user_preference.val,
left=db.user_preference.on(
(db.user_preference.val==db.notification.id)
&(db.user_preference.user_id==user_id)))
query = query.find(lambda row: row.user_preference.val is None) query = query.find(lambda row: row.user_preference.val is None)
cache.ram.clear(key)
cached = cache.ram(key, lambda: query, time_expire=CACHE_EXPIRY)
#TODO sanitize this response #TODO sanitize this response
return query.as_json() return cached.as_json()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment