From f798ddd4922d793d5e6ccb7c4111810d549ff4f4 Mon Sep 17 00:00:00 2001 From: Gaspard d'Hautefeuille Date: Wed, 9 Jul 2025 07:55:37 +0200 Subject: [mod] migrate from Redis to Valkey (#4795) This patch migrates from `redis==5.2.1` [1] to `valkey==6.1.0` [2]. The migration to valkey is necessary because the company behind Redis has decided to abandon the open source license. After experiencing a drop in user numbers, they now want to run it under a dual license again. But this move demonstrates once again how unreliable the company is and how it treats open source developers. To review first, read the docs:: $ make docs.live Follow the instructions to remove redis: - http://0.0.0.0:8000/admin/settings/settings_redis.html Config and install a local valkey DB: - http://0.0.0.0:8000/admin/settings/settings_valkey.html [1] https://pypi.org/project/redis/ [2] https://pypi.org/project/valkey/ Co-authored-by: HLFH Co-authored-by: Markus Heiser --- searx/valkeydb.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 searx/valkeydb.py (limited to 'searx/valkeydb.py') diff --git a/searx/valkeydb.py b/searx/valkeydb.py new file mode 100644 index 000000000..2817c6d0a --- /dev/null +++ b/searx/valkeydb.py @@ -0,0 +1,65 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +"""Implementation of the valkey client (valkey-py_). + +.. _valkey-py: https://github.com/valkey-io/valkey-py + +This implementation uses the :ref:`settings valkey` setup from ``settings.yml``. +A valkey DB connect can be tested by:: + + >>> from searx import valkeydb + >>> valkeydb.initialize() + True + >>> db = valkeydb.client() + >>> db.set("foo", "bar") + True + >>> db.get("foo") + b'bar' + >>> + +""" + +import os +import pwd +import logging +import warnings + +import valkey +from searx import get_setting + + +_CLIENT = None +logger = logging.getLogger(__name__) + + +def client() -> valkey.Valkey: + return _CLIENT + + +def initialize(): + global _CLIENT # pylint: disable=global-statement + if get_setting('redis.url'): + warnings.warn("setting redis.url is deprecated, use valkey.url", DeprecationWarning) + valkey_url = get_setting('valkey.url') or get_setting('redis.url') + if not valkey_url: + return False + try: + # create a client, but no connection is done + _CLIENT = valkey.Valkey.from_url(valkey_url) + + # log the parameters as seen by the valkey lib, without the password + kwargs = _CLIENT.get_connection_kwargs().copy() + kwargs.pop('password', None) + kwargs = ' '.join([f'{k}={v!r}' for k, v in kwargs.items()]) + logger.info("connecting to Valkey %s", kwargs) + + # check the connection + _CLIENT.ping() + + # no error: the valkey connection is working + logger.info("connected to Valkey") + return True + except valkey.exceptions.ValkeyError: + _CLIENT = None + _pw = pwd.getpwuid(os.getuid()) + logger.exception("[%s (%s)] can't connect valkey DB ...", _pw.pw_name, _pw.pw_uid) + return False -- cgit v1.2.3