.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
valkeydb.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Implementation of the valkey client (valkey-py_).
3
4.. _valkey-py: https://github.com/valkey-io/valkey-py
5
6This implementation uses the :ref:`settings valkey` setup from ``settings.yml``.
7A valkey DB connect can be tested by::
8
9 >>> from searx import valkeydb
10 >>> valkeydb.initialize()
11 True
12 >>> db = valkeydb.client()
13 >>> db.set("foo", "bar")
14 True
15 >>> db.get("foo")
16 b'bar'
17 >>>
18
19"""
20
21import os
22import pwd
23import logging
24import warnings
25
26import valkey
27from searx import get_setting
28
29
30_CLIENT = None
31logger = logging.getLogger(__name__)
32
33
34def client() -> valkey.Valkey:
35 return _CLIENT
36
37
39 global _CLIENT # pylint: disable=global-statement
40 if get_setting('redis.url'):
41 warnings.warn("setting redis.url is deprecated, use valkey.url", DeprecationWarning)
42 valkey_url = get_setting('valkey.url') or get_setting('redis.url')
43 if not valkey_url:
44 return False
45 try:
46 # create a client, but no connection is done
47 _CLIENT = valkey.Valkey.from_url(valkey_url)
48
49 # log the parameters as seen by the valkey lib, without the password
50 kwargs = _CLIENT.get_connection_kwargs().copy()
51 kwargs.pop('password', None)
52 kwargs = ' '.join([f'{k}={v!r}' for k, v in kwargs.items()])
53 logger.info("connecting to Valkey %s", kwargs)
54
55 # check the connection
56 _CLIENT.ping()
57
58 # no error: the valkey connection is working
59 logger.info("connected to Valkey")
60 return True
61 except valkey.exceptions.ValkeyError:
62 _CLIENT = None
63 _pw = pwd.getpwuid(os.getuid())
64 logger.exception("[%s (%s)] can't connect valkey DB ...", _pw.pw_name, _pw.pw_uid)
65 return False
valkey.Valkey client()
Definition valkeydb.py:34
get_setting(name, default=_unset)
Definition __init__.py:69