From c0e5255bee4e3232b87ba060d1c195e9752f3b05 Mon Sep 17 00:00:00 2001 From: Yax <1949284+kianby@users.noreply.github.com> Date: Sun, 9 Jul 2017 17:13:41 +0200 Subject: [PATCH] one cache per worker --- app/__init__.py | 16 ---------------- app/controllers/api.py | 18 ++++++++++++++++-- requirements.txt | 3 --- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 86d275c..20164f2 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,19 +1,3 @@ -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 diff --git a/app/controllers/api.py b/app/controllers/api.py index 58bdea5..5040865 100644 --- a/app/controllers/api.py +++ b/app/controllers/api.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +import time import logging import config from sanic import response @@ -9,10 +10,23 @@ 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__) +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']) diff --git a/requirements.txt b/requirements.txt index cf37f42..a06b7da 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,4 @@ -aiocache==0.7.0 aiofiles==0.3.1 -aiomcache==0.5.2 -aioredis==0.3.3 clize==2.4 hiredis==0.2.0 httptools==0.0.9