-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #686 from return42/lib_redis
Add redis DB and connector
- Loading branch information
Showing
10 changed files
with
491 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.. _redis db: | ||
|
||
======== | ||
Redis DB | ||
======== | ||
|
||
.. automodule:: searx.shared.redisdb | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ uvloop==0.16.0 | |
httpx-socks[asyncio]==0.7.2 | ||
langdetect==1.0.9 | ||
setproctitle==1.2.2 | ||
redis==4.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.