summaryrefslogtreecommitdiff
path: root/searx/shared/redisdb.py
diff options
context:
space:
mode:
authorMarkus Heiser <markus.heiser@darmarIT.de>2022-01-11 19:55:14 +0100
committerGitHub <noreply@github.com>2022-01-11 19:55:14 +0100
commit977e9a433085124e17f2f7f26afabbeb71db9470 (patch)
tree286c046cdd9eaa069e49c81b6f7f7bb545746b65 /searx/shared/redisdb.py
parentf4004133b605cea8225c720a7f4c593d4c20d13e (diff)
parentdca83944b588be3ec9e49486daea6cf15ef58f78 (diff)
Merge pull request #686 from return42/lib_redis
Add redis DB and connector
Diffstat (limited to 'searx/shared/redisdb.py')
-rw-r--r--searx/shared/redisdb.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/searx/shared/redisdb.py b/searx/shared/redisdb.py
new file mode 100644
index 000000000..da71d169c
--- /dev/null
+++ b/searx/shared/redisdb.py
@@ -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