From f2432e33d6e3778cb0e976d602bb259d60081d64 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 15 Jul 2026 18:05:21 +0200 Subject: [PATCH] [fix] engine cache: return default value when cache entry expired It's possible that it's expired but has not yet been automatically deleted by the periodic maintenance because that, by default, runs only every 2 hours. Related: - see https://github.com/searxng/searxng/pull/6409#discussion_r3584712601 --- searx/cache.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/searx/cache.py b/searx/cache.py index e6b6541d9..1b2d50e2b 100644 --- a/searx/cache.py +++ b/searx/cache.py @@ -458,12 +458,22 @@ class ExpireCacheSQLite(sqlitedb.SQLiteAppl, ExpireCache): # Before values are taken from the table, a maintenance interval may # need to be carried out. self.maintenance() - sql = f"SELECT value FROM {table} WHERE key = ?" + sql = f"SELECT value, expire FROM {table} WHERE key = ?" row = self.DB.execute(sql, (key,)).fetchone() if row is None: return default - return self.deserialize(row[0]) + # Check if value is expired. It's possible that it's expired but has not + # yet been automatically deleted by the periodic maintenance + (value, expire) = row + now = time.time() + if expire < now: + # The record is deleted during the maintenance interval. Deleting + # the record at this point offers no advantage, as a SELECT + # statement must be executed for every cache.get request anyways. + return default + + return self.deserialize(value) def pairs(self, ctx: str) -> Iterator[tuple[str, typing.Any]]: """Iterate over key/value pairs from table given by argument ``ctx``.