mirror of
https://github.com/searxng/searxng
synced 2024-11-03 09:40:20 +00:00
e85744c254
This reverts interim fix from commit 50c4b58db
and adds requirement
`redis==4.1.1`.
The interim fix was needed by Alpine images (Docker) [1] and has been fixed in
commit [2] merged with the patch series from [3]. In redis-py version 4.1.1
this pach has been released on PyPi [4].
[1] https://github.com/redis/redis-py/issues/1869
[2] https://github.com/redis/redis-py/commit/1fc1233f
[3] https://github.com/redis/redis-py/pull/1854
[4] https://github.com/redis/redis-py/issues/1880
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
# 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'
|
|
>>>
|
|
|
|
"""
|
|
|
|
import logging
|
|
import redis
|
|
from searx import get_setting
|
|
|
|
logger = logging.getLogger('searx.shared.redis')
|
|
_client = None
|
|
|
|
|
|
def client():
|
|
global _client # pylint: disable=global-statement
|
|
if _client is None:
|
|
# not thread safe: in the worst case scenario, two or more clients are
|
|
# initialized only one is kept, the others are garbage collected.
|
|
_client = redis.Redis.from_url(get_setting('redis.url'))
|
|
return _client
|
|
|
|
|
|
def init():
|
|
try:
|
|
c = client()
|
|
logger.info("connected redis DB --> %s", c.acl_whoami())
|
|
return True
|
|
except redis.exceptions.ConnectionError as exc:
|
|
logger.error("can't connet redis DB ...")
|
|
logger.error(" %s", exc)
|
|
return False
|