homemade caching function
This commit is contained in:
parent
c9b1444bb6
commit
198978a35d
3 changed files with 28 additions and 7 deletions
|
@ -1,3 +1,19 @@
|
|||
import time
|
||||
from sanic import Sanic
|
||||
|
||||
app = Sanic()
|
||||
cache = {}
|
||||
cache_time = 0
|
||||
|
||||
def get_cached(key):
|
||||
global cache
|
||||
global cache_time
|
||||
value = cache.get(key,None)
|
||||
if (time.time() - cache_time) > 120:
|
||||
cache = {}
|
||||
cache_time = time.time()
|
||||
return value
|
||||
|
||||
def set_cached(key, value):
|
||||
global cache
|
||||
cache[key] = value
|
||||
|
|
|
@ -4,13 +4,13 @@
|
|||
import logging
|
||||
import config
|
||||
from sanic import response
|
||||
from aiocache import cached, SimpleMemoryCache
|
||||
from aiocache.serializers import JsonSerializer
|
||||
from app import app
|
||||
from app.models.site import Site
|
||||
from app.models.comment import Comment
|
||||
from app.helpers.hashing import md5
|
||||
from app.services import processor
|
||||
from app import get_cached
|
||||
from app import set_cached
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
@ -45,17 +45,21 @@ def query_comments(request):
|
|||
return r
|
||||
|
||||
|
||||
@cached(ttl=300, serializer=JsonSerializer())
|
||||
async def get_cached_comments_count(request):
|
||||
try:
|
||||
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()
|
||||
key = '%s:%s' % (token, url)
|
||||
count = get_cached(key)
|
||||
if count is None:
|
||||
count = Comment.select(Comment).join(Site).where(
|
||||
(Comment.url == url) &
|
||||
(Comment.published.is_null(False)) &
|
||||
(Site.token == token)).count()
|
||||
set_cached(key, count)
|
||||
r = {'count': count}
|
||||
except:
|
||||
logger.exception("cache exception")
|
||||
r = {'count': 0}
|
||||
return r
|
||||
|
||||
|
|
|
@ -70,4 +70,5 @@ if __name__ == '__main__':
|
|||
app.run(host=config.HTTP_ADDRESS,
|
||||
port=config.HTTP_PORT,
|
||||
debug=config.DEBUG,
|
||||
log_config=None,
|
||||
workers=config.HTTP_WORKERS)
|
||||
|
|
Loading…
Add table
Reference in a new issue