Improve use of URL cache #118

This commit is contained in:
Ian Renton
2026-08-01 08:06:44 +01:00
parent fe10943ecc
commit c472872e10
5 changed files with 41 additions and 36 deletions
+17 -15
View File
@@ -3,21 +3,23 @@ from datetime import timedelta
from requests_cache import CachedSession
# Cache for "semi-static" data retrieved from a URL. This is a layet 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.
_session = CachedSession("cache/semi_static_urls", expire_after=timedelta(days=1),
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))
_lock = threading.Lock()
class _ThreadSafeSession:
"""Wraps CachedSession with a lock to prevent concurrent SQLite access across threads. This allows a single object
to be used freely across the application."""
def get(self, *args, **kwargs):
with _lock:
return _session.get(*args, **kwargs)
# Global object
URL_DATA_CACHE = _ThreadSafeSession()
with self._lock:
return super().get(*args, **kwargs)