.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.redisdb Namespace Reference

Functions

redis.Redis client ()
 
 initialize ()
 

Variables

str OLD_REDIS_URL_DEFAULT_URL = 'unix:///usr/local/searxng-redis/run/redis.sock?db=0'
 
 _CLIENT = None
 
 logger = logging.getLogger(__name__)
 

Detailed Description

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 import redisdb
  >>> redisdb.initialize()
  True
  >>> db = redisdb.client()
  >>> db.set("foo", "bar")
  True
  >>> db.get("foo")
  b'bar'
  >>>

Function Documentation

◆ client()

redis.Redis searx.redisdb.client ( )

Definition at line 35 of file redisdb.py.

35def client() -> redis.Redis:
36 return _CLIENT
37
38

◆ initialize()

searx.redisdb.initialize ( )

Definition at line 39 of file redisdb.py.

39def initialize():
40 global _CLIENT # pylint: disable=global-statement
41 redis_url = get_setting('redis.url')
42 if not redis_url:
43 return False
44 try:
45 # create a client, but no connection is done
46 _CLIENT = redis.Redis.from_url(redis_url)
47
48 # log the parameters as seen by the redis lib, without the password
49 kwargs = _CLIENT.get_connection_kwargs().copy()
50 kwargs.pop('password', None)
51 kwargs = ' '.join([f'{k}={v!r}' for k, v in kwargs.items()])
52 logger.info("connecting to Redis %s", kwargs)
53
54 # check the connection
55 _CLIENT.ping()
56
57 # no error: the redis connection is working
58 logger.info("connected to Redis")
59 return True
60 except redis.exceptions.RedisError as e:
61 _CLIENT = None
62 _pw = pwd.getpwuid(os.getuid())
63 logger.exception("[%s (%s)] can't connect redis DB ...", _pw.pw_name, _pw.pw_uid)
64 if redis_url == OLD_REDIS_URL_DEFAULT_URL and isinstance(e, redis.exceptions.ConnectionError):
65 logger.info(
66 "You can safely ignore the above Redis error if you don't use Redis. "
67 "You can remove this error by setting redis.url to false in your settings.yml."
68 )
69 return False

Variable Documentation

◆ _CLIENT

searx.redisdb._CLIENT = None
protected

Definition at line 31 of file redisdb.py.

◆ logger

searx.redisdb.logger = logging.getLogger(__name__)

Definition at line 32 of file redisdb.py.

◆ OLD_REDIS_URL_DEFAULT_URL

str searx.redisdb.OLD_REDIS_URL_DEFAULT_URL = 'unix:///usr/local/searxng-redis/run/redis.sock?db=0'

Definition at line 28 of file redisdb.py.