Fix count caching

pull/6/head
Yax 7 years ago
parent 48d45bfb2f
commit f35fd42c07

@ -1,5 +1,3 @@
from sanic import Sanic from sanic import Sanic
from aiocache import SimpleMemoryCache
app = Sanic() app = Sanic()
cache = SimpleMemoryCache()

@ -4,9 +4,9 @@
import logging import logging
import config import config
from sanic import response 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 app
from app import cache
from app.models.site import Site from app.models.site import Site
from app.models.comment import Comment from app.models.comment import Comment
from app.helpers.hashing import md5 from app.helpers.hashing import md5
@ -45,22 +45,27 @@ def query_comments(request):
return r return r
@cached(ttl=300) @cached(ttl=300, serializer=JsonSerializer())
@app.route("/comments/count", methods=['GET']) async def get_cached_comments_count(request):
def get_comments_count(request):
try: try:
print('GET COUNT FROM DB')
token = request.args.get('token', '') token = request.args.get('token', '')
url = request.args.get('url', '') url = request.args.get('url', '')
count = Comment.select(Comment).join(Site).where( count = Comment.select(Comment).join(Site).where(
(Comment.url == url) & (Comment.url == url) &
(Comment.published.is_null(False)) & (Comment.published.is_null(False)) &
(Site.token == token)).count() (Site.token == token)).count()
r = response.json({'count': count}) r = {'count': count}
except: except:
r = response.json({'count': 0}) r = {'count': 0}
return r 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']) @app.route("/comments", methods=['OPTIONS'])
def option_comments(request): def option_comments(request):
return response.text('OK') return response.text('OK')

@ -67,7 +67,6 @@ if not config.DEBUG:
logging.getLogger('werkzeug').level = logging.WARNING logging.getLogger('werkzeug').level = logging.WARNING
if __name__ == '__main__': if __name__ == '__main__':
app.run(host=config.HTTP_ADDRESS, app.run(host=config.HTTP_ADDRESS,
port=config.HTTP_PORT, port=config.HTTP_PORT,
debug=config.DEBUG, debug=config.DEBUG,

Loading…
Cancel
Save