From 18a58943ccaa324b39232fa503462eb39f407399 Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Wed, 24 Sep 2025 19:34:03 +0200 Subject: [mod] ExpireCacheSQLite - implement .setmany() for bulk loading [1] https://github.com/searxng/searxng/issues/5223#issuecomment-3328597147 Suggested-by: Ivan G [1] Signed-off-by: Markus Heiser --- searx/data/currencies.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'searx/data/currencies.py') diff --git a/searx/data/currencies.py b/searx/data/currencies.py index 33aa9530c..538900762 100644 --- a/searx/data/currencies.py +++ b/searx/data/currencies.py @@ -6,10 +6,12 @@ __all__ = ["CurrenciesDB"] import typing as t import json import pathlib -import time from .core import get_cache, log +if t.TYPE_CHECKING: + from searx.cache import CacheRowType + @t.final class CurrenciesDB: @@ -33,19 +35,14 @@ class CurrenciesDB: # in /tmp and will be rebuild during the reboot anyway def load(self): - _start = time.time() log.debug("init searx.data.CURRENCIES") with open(self.json_file, encoding="utf-8") as f: data_dict: dict[str, dict[str, str]] = json.load(f) - for key, value in data_dict["names"].items(): - self.cache.set(key=key, value=value, ctx=self.ctx_names, expire=None) - for key, value in data_dict["iso4217"].items(): - self.cache.set(key=key, value=value, ctx=self.ctx_iso4217, expire=None) - log.debug( - "init searx.data.CURRENCIES added %s items in %s sec.", - len(data_dict["names"]) + len(data_dict["iso4217"]), - time.time() - _start, - ) + + rows: "list[CacheRowType]" = [(k, v, None) for k, v in data_dict["names"].items()] + self.cache.setmany(rows, ctx=self.ctx_names) + rows = [(k, v, None) for k, v in data_dict["iso4217"].items()] + self.cache.setmany(rows, ctx=self.ctx_iso4217) def name_to_iso4217(self, name: str) -> str | None: self.init() -- cgit v1.2.3