[enh] add redis connector searx/shared/redisdb.py

Add a redis connector, the default DB connector is a socket at::

    unix:///usr/local/searxng-redis/run/redis.sock?db=0

To set up a redis instance simply use::

    $ ./manage redis.build
    $ sudo -H ./manage redis.install

A hint for developers:

To get access rights to this instance, your developer account needs to be added
to the *searxng-redis* group::

    $ sudo -H ./manage redis.addgrp "${USER}"
    # don't forget to logout & login to get member of group

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
Markus Heiser 2022-01-03 17:43:20 +01:00
parent 4f1130d663
commit a6cfab93fa
6 changed files with 87 additions and 0 deletions

View File

@ -139,6 +139,36 @@ Global Settings
``default_http_headers``: ``default_http_headers``:
Set additional HTTP headers, see `#755 <https://github.com/searx/searx/issues/715>`__ Set additional HTTP headers, see `#755 <https://github.com/searx/searx/issues/715>`__
.. _settings redis:
``redis:``
----------
.. _Redis.from_url(url): https://redis-py.readthedocs.io/en/stable/connections.html#redis.client.Redis.from_url
``url``
URL to connect redis database, see `Redis.from_url(url)`_ & :ref:`redis db`::
redis://[[username]:[password]]@localhost:6379/0
rediss://[[username]:[password]]@localhost:6379/0
unix://[[username]:[password]]@/path/to/socket.sock?db=0
.. admonition:: Tip for developers
To set up a redis instance simply use::
$ ./manage redis.build
$ sudo -H ./manage redis.install
To get access rights to this instance, your developer account needs to be
added to the *searxng-redis* group::
$ sudo -H ./manage redis.addgrp "${USER}"
# don't forget to logout & login to get member of group
.. _settings outgoing:
``outgoing:`` ``outgoing:``
------------- -------------

View File

@ -0,0 +1,8 @@
.. _redis db:
========
Redis DB
========
.. automodule:: searx.shared.redisdb
:members:

View File

@ -13,3 +13,4 @@ uvloop==0.16.0
httpx-socks[asyncio]==0.4.1 httpx-socks[asyncio]==0.4.1
langdetect==1.0.9 langdetect==1.0.9
setproctitle==1.2.2 setproctitle==1.2.2
redis==4.1.0

View File

@ -61,6 +61,10 @@ server:
X-Robots-Tag: noindex, nofollow X-Robots-Tag: noindex, nofollow
Referrer-Policy: no-referrer 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: ui:
# Custom static path - leave it blank if you didn't change # Custom static path - leave it blank if you didn't change
static_path: "" static_path: ""

View File

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

41
searx/shared/redisdb.py Normal file
View File

@ -0,0 +1,41 @@
# 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')
def client():
return redis.Redis.from_url(get_setting('redis.url'))
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