diff options
Diffstat (limited to 'searx')
-rw-r--r-- | searx/settings.yml | 4 | ||||
-rw-r--r-- | searx/settings_defaults.py | 3 | ||||
-rw-r--r-- | searx/shared/redisdb.py | 41 |
3 files changed, 48 insertions, 0 deletions
diff --git a/searx/settings.yml b/searx/settings.yml index 3227a5a55..0105c7939 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -61,6 +61,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: "" diff --git a/searx/settings_defaults.py b/searx/settings_defaults.py index 9c4711bfc..669f2fa87 100644 --- a/searx/settings_defaults.py +++ b/searx/settings_defaults.py @@ -169,6 +169,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')), diff --git a/searx/shared/redisdb.py b/searx/shared/redisdb.py new file mode 100644 index 000000000..613b82a38 --- /dev/null +++ b/searx/shared/redisdb.py @@ -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 |