@@ -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