From cd3bd950aec65fbacfa70ddf56ae04e3ba68e06e Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Sun, 30 May 2021 14:40:20 +0200 Subject: [PATCH] add cache for content type --- backend/stream.py | 7 ++++++- backend/stream_providers.py | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/backend/stream.py b/backend/stream.py index f08a4b8..af405b3 100755 --- a/backend/stream.py +++ b/backend/stream.py @@ -12,6 +12,7 @@ import tornado.routing import aiohttp import aiohttp_socks import stream_providers +import expiringdict logging.basicConfig(format='[%(filename)s:%(lineno)d] %(message)s', stream=sys.stdout, level=logging.INFO) logger = logging.getLogger(__name__) @@ -31,6 +32,7 @@ icecast_server = os.environ.get("ICECAST_SERVER") stream_server = os.environ.get("STREAM_SERVER") proxy_server = os.environ.get("PROXY_SERVER") +ctype_cache = expiringdict.ExpiringDict(max_len=128, max_age_seconds=1800) class ProxyElem(): def __init__(self, proxy): self.proxy = proxy @@ -46,7 +48,9 @@ class ProxyElem(): def __repr__(self): return str(self.proxy) async def content_type(self, url): - ctype = None + cached = ctype_cache.get(url) + if isinstance(cached, str) and "binary" not in cached.lower(): + return cached async with self.session() as session: for i in range(5): try: @@ -56,6 +60,7 @@ class ProxyElem(): logger.info(e) else: if isinstance(ctype, str): + ctype_cache[url] = ctype return ctype return "binary/octet-type" async def proxy_url(self, current, path): diff --git a/backend/stream_providers.py b/backend/stream_providers.py index 9a698a2..b9c9bcb 100755 --- a/backend/stream_providers.py +++ b/backend/stream_providers.py @@ -167,7 +167,7 @@ class MetaRunner(StreamProvider): self.logger.info(e) return StreamData(None, data.get("og:image"), data.get("og:title"), data.get("og:description"), False) -upstream_cache = expiringdict.ExpiringDict(max_len=4096, max_age_seconds=1800) +upstream_cache = expiringdict.ExpiringDict(max_len=512, max_age_seconds=1800) async def get_from_runner(cache_key, runner, logger): result = None