From f35fd42c0711b00d4a8fff0dcc1634afb4f5019b Mon Sep 17 00:00:00 2001 From: Yax <1949284+kianby@users.noreply.github.com> Date: Sun, 9 Jul 2017 15:55:40 +0200 Subject: [PATCH] Fix count caching --- app/__init__.py | 2 -- app/controllers/api.py | 19 ++++++++++++------- app/run.py | 1 - 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index a9c5ad1..20164f2 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,5 +1,3 @@ from sanic import Sanic -from aiocache import SimpleMemoryCache app = Sanic() -cache = SimpleMemoryCache() diff --git a/app/controllers/api.py b/app/controllers/api.py index 8faa53f..8494327 100644 --- a/app/controllers/api.py +++ b/app/controllers/api.py @@ -4,9 +4,9 @@ import logging import config from sanic import response -from aiocache import cached +from aiocache import cached, SimpleMemoryCache +from aiocache.serializers import JsonSerializer from app import app -from app import cache from app.models.site import Site from app.models.comment import Comment from app.helpers.hashing import md5 @@ -45,22 +45,27 @@ def query_comments(request): return r -@cached(ttl=300) -@app.route("/comments/count", methods=['GET']) -def get_comments_count(request): +@cached(ttl=300, serializer=JsonSerializer()) +async def get_cached_comments_count(request): try: + print('GET COUNT FROM DB') token = request.args.get('token', '') url = request.args.get('url', '') count = Comment.select(Comment).join(Site).where( (Comment.url == url) & (Comment.published.is_null(False)) & (Site.token == token)).count() - r = response.json({'count': count}) + r = {'count': count} except: - r = response.json({'count': 0}) + r = {'count': 0} return r +@app.route("/comments/count", methods=['GET']) +async def get_comments_count(request): + return response.json(await get_cached_comments_count(request)) + + @app.route("/comments", methods=['OPTIONS']) def option_comments(request): return response.text('OK') diff --git a/app/run.py b/app/run.py index 236a62b..9402796 100644 --- a/app/run.py +++ b/app/run.py @@ -67,7 +67,6 @@ if not config.DEBUG: logging.getLogger('werkzeug').level = logging.WARNING if __name__ == '__main__': - app.run(host=config.HTTP_ADDRESS, port=config.HTTP_PORT, debug=config.DEBUG,