add cache for content type

This commit is contained in:
Roy Olav Purser 2021-05-30 14:40:20 +02:00
parent 69e9a75d15
commit cd3bd950ae
Signed by: roypur
GPG Key ID: E14D26A036F21656
2 changed files with 7 additions and 2 deletions

View File

@ -12,6 +12,7 @@ import tornado.routing
import aiohttp import aiohttp
import aiohttp_socks import aiohttp_socks
import stream_providers import stream_providers
import expiringdict
logging.basicConfig(format='[%(filename)s:%(lineno)d] %(message)s', stream=sys.stdout, level=logging.INFO) logging.basicConfig(format='[%(filename)s:%(lineno)d] %(message)s', stream=sys.stdout, level=logging.INFO)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -31,6 +32,7 @@ icecast_server = os.environ.get("ICECAST_SERVER")
stream_server = os.environ.get("STREAM_SERVER") stream_server = os.environ.get("STREAM_SERVER")
proxy_server = os.environ.get("PROXY_SERVER") proxy_server = os.environ.get("PROXY_SERVER")
ctype_cache = expiringdict.ExpiringDict(max_len=128, max_age_seconds=1800)
class ProxyElem(): class ProxyElem():
def __init__(self, proxy): def __init__(self, proxy):
self.proxy = proxy self.proxy = proxy
@ -46,7 +48,9 @@ class ProxyElem():
def __repr__(self): def __repr__(self):
return str(self.proxy) return str(self.proxy)
async def content_type(self, url): 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: async with self.session() as session:
for i in range(5): for i in range(5):
try: try:
@ -56,6 +60,7 @@ class ProxyElem():
logger.info(e) logger.info(e)
else: else:
if isinstance(ctype, str): if isinstance(ctype, str):
ctype_cache[url] = ctype
return ctype return ctype
return "binary/octet-type" return "binary/octet-type"
async def proxy_url(self, current, path): async def proxy_url(self, current, path):

View File

@ -167,7 +167,7 @@ class MetaRunner(StreamProvider):
self.logger.info(e) self.logger.info(e)
return StreamData(None, data.get("og:image"), data.get("og:title"), data.get("og:description"), False) 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): async def get_from_runner(cache_key, runner, logger):
result = None result = None