Skip to content

Commit d44b5aa

Browse files
fix: invalidate cache on delete/store (#50)
1 parent d5730f5 commit d44b5aa

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

app/api/endpoints/manifest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def get_config_id(catalog) -> str | None:
8080

8181

8282
async def _manifest_handler(response: Response, token: str):
83-
response.headers["Cache-Control"] = "public, max-age=7200"
83+
response.headers["Cache-Control"] = "no-cache"
8484

8585
if not token:
8686
raise HTTPException(status_code=401, detail="Missing token. Please reconfigure the addon.")

app/core/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.1.3"
1+
__version__ = "1.1.4"

app/services/token_store.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def _ensure_secure_salt(self) -> None:
3737
if not settings.TOKEN_SALT or settings.TOKEN_SALT == "change-me":
3838
logger.error("Refusing to store credentials because TOKEN_SALT is unset or using the insecure default.")
3939
raise RuntimeError(
40-
"Server misconfiguration: TOKEN_SALT must be set to a non-default value before storing credentials."
40+
"Server misconfiguration: TOKEN_SALT must be set to a non-default value before storing" " credentials."
4141
)
4242

4343
def _get_cipher(self) -> Fernet:
@@ -145,6 +145,20 @@ async def store_user_data(self, user_id: str, payload: dict[str, Any]) -> str:
145145
else:
146146
await client.set(key, json_str)
147147

148+
# Invalidate async LRU cache for fresh reads on subsequent requests
149+
try:
150+
# bound method supports targeted invalidation by argument(s)
151+
self.get_user_data.cache_invalidate(token)
152+
except KeyError:
153+
# The token was not in the cache, no action needed.
154+
pass
155+
except Exception as e:
156+
logger.warning(f"Targeted cache invalidation failed: {e}. Falling back to clearing cache.")
157+
try:
158+
self.get_user_data.cache_clear()
159+
except Exception as e_clear:
160+
logger.error(f"Error while clearing cache: {e_clear}")
161+
148162
# Ensure we remove from negative cache so new value is read next time
149163
try:
150164
if token in self._missing_tokens:
@@ -194,6 +208,19 @@ async def delete_token(self, token: str = None, key: str = None) -> None:
194208
client = await self._get_client()
195209
await client.delete(key)
196210

211+
# Invalidate async LRU cache so future reads reflect deletion
212+
try:
213+
if token:
214+
self.get_user_data.cache_invalidate(token)
215+
else:
216+
# If only key is provided, clear cache entirely to be safe
217+
self.get_user_data.cache_clear()
218+
except KeyError:
219+
# The token was not in the cache, no action needed.
220+
pass
221+
except Exception as e:
222+
logger.warning(f"Failed to invalidate user data cache during token deletion: {e}")
223+
197224
# Remove from negative cache as token is deleted
198225
try:
199226
if token and token in self._missing_tokens:

0 commit comments

Comments
 (0)