Merge pull request #686 from return42/lib_redis

Add redis DB and connector
This commit is contained in:
Markus Heiser 2022-01-11 19:55:14 +01:00 committed by GitHub
commit 977e9a4330
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 491 additions and 3 deletions

View file

@ -62,6 +62,10 @@ server:
X-Robots-Tag: noindex, nofollow
Referrer-Policy: no-referrer
redis:
# https://redis-py.readthedocs.io/en/stable/connections.html#redis.client.Redis.from_url
url: unix:///usr/local/searxng-redis/run/redis.sock?db=0
ui:
# Custom static path - leave it blank if you didn't change
static_path: ""

View file

@ -170,6 +170,9 @@ SCHEMA = {
'method': SettingsValue(('POST', 'GET'), 'POST'),
'default_http_headers': SettingsValue(dict, {}),
},
'redis': {
'url': SettingsValue(str, 'unix:///usr/local/searxng-redis/run/redis.sock?db=0'),
},
'ui': {
'static_path': SettingsDirectoryValue(str, os.path.join(searx_dir, 'static')),
'templates_path': SettingsDirectoryValue(str, os.path.join(searx_dir, 'templates')),

47
searx/shared/redisdb.py Normal file
View file

@ -0,0 +1,47 @@
# 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