homemade caching function
This commit is contained in:
parent
39e547c303
commit
c6cf9ac795
3 changed files with 28 additions and 7 deletions
|
@ -1,3 +1,19 @@
|
||||||
|
import time
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
|
|
||||||
app = 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 logging
|
||||||
import config
|
import config
|
||||||
from sanic import response
|
from sanic import response
|
||||||
from aiocache import cached, SimpleMemoryCache
|
|
||||||
from aiocache.serializers import JsonSerializer
|
|
||||||
from app import app
|
from app import app
|
||||||
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
|
||||||
from app.services import processor
|
from app.services import processor
|
||||||
|
from app import get_cached
|
||||||
|
from app import set_cached
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -45,17 +45,21 @@ def query_comments(request):
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
|
||||||
@cached(ttl=300, serializer=JsonSerializer())
|
|
||||||
async def get_cached_comments_count(request):
|
async def get_cached_comments_count(request):
|
||||||
try:
|
try:
|
||||||
token = request.args.get('token', '')
|
token = request.args.get('token', '')
|
||||||
url = request.args.get('url', '')
|
url = request.args.get('url', '')
|
||||||
|
key = '%s:%s' % (token, url)
|
||||||
|
count = get_cached(key)
|
||||||
|
if count is None:
|
||||||
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()
|
||||||
|
set_cached(key, count)
|
||||||
r = {'count': count}
|
r = {'count': count}
|
||||||
except:
|
except:
|
||||||
|
logger.exception("cache exception")
|
||||||
r = {'count': 0}
|
r = {'count': 0}
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
|
|
@ -70,4 +70,5 @@ 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,
|
||||||
|
log_config=None,
|
||||||
workers=config.HTTP_WORKERS)
|
workers=config.HTTP_WORKERS)
|
||||||
|
|
Loading…
Add table
Reference in a new issue