Fix count caching
This commit is contained in:
		
							parent
							
								
									48d45bfb2f
								
							
						
					
					
						commit
						f35fd42c07
					
				
					 3 changed files with 12 additions and 10 deletions
				
			
		|  | @ -1,5 +1,3 @@ | |||
| from sanic import Sanic | ||||
| from aiocache import SimpleMemoryCache | ||||
| 
 | ||||
| app = Sanic() | ||||
| cache = SimpleMemoryCache() | ||||
|  |  | |||
|  | @ -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') | ||||
|  |  | |||
|  | @ -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, | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Yax
						Yax