2022-01-03 16:43:20 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# lint: pylint
|
|
|
|
"""Implementation of the redis client (redis-py_).
|
|
|
|
|
|
|
|
.. _redis-py: https://github.com/redis/redis-py
|
|
|
|
|
|
|
|
This implementation uses the :ref:`settings redis` setup from ``settings.yml``.
|
|
|
|
A redis DB connect can be tested by::
|
|
|
|
|
|
|
|
>>> from searx.shared import redisdb
|
|
|
|
>>> redisdb.init()
|
|
|
|
True
|
|
|
|
>>> db = redisdb.client()
|
|
|
|
>>> db.set("foo", "bar")
|
|
|
|
True
|
|
|
|
>>> db.get("foo")
|
|
|
|
b'bar'
|
|
|
|
>>>
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2022-05-27 16:43:14 +00:00
|
|
|
import os
|
|
|
|
import pwd
|
2022-01-03 16:43:20 +00:00
|
|
|
import logging
|
2022-01-17 16:19:41 +00:00
|
|
|
import redis
|
2022-01-03 16:43:20 +00:00
|
|
|
from searx import get_setting
|
|
|
|
|
2022-05-27 16:43:14 +00:00
|
|
|
|
2022-07-15 16:38:32 +00:00
|
|
|
logger = logging.getLogger('searx.shared.redisdb')
|
2022-01-07 16:29:32 +00:00
|
|
|
_client = None
|
2022-01-03 16:43:20 +00:00
|
|
|
|
|
|
|
|
2022-07-15 16:38:32 +00:00
|
|
|
def client() -> redis.Redis:
|
2022-01-07 16:29:32 +00:00
|
|
|
return _client
|
2022-01-03 16:43:20 +00:00
|
|
|
|
|
|
|
|
2022-07-15 16:38:32 +00:00
|
|
|
def initialize():
|
|
|
|
global _client # pylint: disable=global-statement
|
2022-01-03 16:43:20 +00:00
|
|
|
try:
|
2022-07-15 16:38:32 +00:00
|
|
|
_client = redis.Redis.from_url(get_setting('redis.url'))
|
|
|
|
logger.info("connected redis: %s", get_setting('redis.url'))
|
2022-01-03 16:43:20 +00:00
|
|
|
except redis.exceptions.ConnectionError as exc:
|
2022-05-27 16:43:14 +00:00
|
|
|
_pw = pwd.getpwuid(os.getuid())
|
|
|
|
logger.error("[%s (%s)] can't connect redis DB ...", _pw.pw_name, _pw.pw_uid)
|
2022-01-03 16:43:20 +00:00
|
|
|
logger.error(" %s", exc)
|