one cache per worker

This commit is contained in:
Yax 2017-07-09 17:13:41 +02:00
parent c6cf9ac795
commit c5ae33f738
3 changed files with 16 additions and 21 deletions

View file

@ -1,19 +1,3 @@
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

View file

@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import time
import logging import logging
import config import config
from sanic import response from sanic import response
@ -9,10 +10,23 @@ 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__)
cache = {}
cache_time = 0
def get_cached(key):
global cache
global cache_time
value = cache.get(key,None)
if (time.time() - cache_time) > 10:
cache = {}
cache_time = time.time()
return value
def set_cached(key, value):
global cache
cache[key] = value
@app.route("/comments", methods=['GET']) @app.route("/comments", methods=['GET'])

View file

@ -1,7 +1,4 @@
aiocache==0.7.0
aiofiles==0.3.1 aiofiles==0.3.1
aiomcache==0.5.2
aioredis==0.3.3
clize==2.4 clize==2.4
hiredis==0.2.0 hiredis==0.2.0
httptools==0.0.9 httptools==0.0.9