Files
spothole/core/url_data_cache.py

25 lines
1.1 KiB
Python

import threading
from datetime import timedelta
from requests_cache import CachedSession
from core.data_store import CACHE_DIR
class URLDataCache(CachedSession):
"""Cache for "semi-static" data retrieved from a URL. This is an extra layer of caching in addition to the normal
caching of spots, alerts and other data in the DataStore class. Its purpose is to avoid hitting remote endpoints
frequently when e.g. restarting Spothole many times during testing. It should only be used for data that isn't
expected to change on a sub-daily basis, so never for spots & alerts. It also implements a lock to allow it to be
used across multiple threads, though note that URL lookups will block each other this way, so it is still better to
create one of these objects per thread if possible."""
_lock = threading.Lock()
def __init__(self, name):
super().__init__(CACHE_DIR + "urls/" + name, expire_after=timedelta(days=1),
allowable_codes=(200, 400, 401, 403, 404))
def get(self, *args, **kwargs):
with self._lock:
return super().get(*args, **kwargs)