.oO SearXNG Developer Documentation Oo.
|
Functions | |
lua_script_storage (client, script) | |
purge_by_prefix (client, str prefix="SearXNG_") | |
secret_hash (str name) | |
incr_counter (client, str name, int limit=0, int expire=0) | |
drop_counter (client, name) | |
incr_sliding_window (client, str name, int duration) | |
Variables | |
dict | LUA_SCRIPT_STORAGE = {} |
str | PURGE_BY_PREFIX |
str | INCR_COUNTER |
str | INCR_SLIDING_WINDOW |
A collection of convenient functions and redis/lua scripts. This code was partial inspired by the `Bullet-Proofing Lua Scripts in RedisPy`_ article. .. _Bullet-Proofing Lua Scripts in RedisPy: https://redis.com/blog/bullet-proofing-lua-scripts-in-redispy/
searx.redislib.drop_counter | ( | client, | |
name ) |
Drop counter with redis key ``SearXNG_counter_<name>`` The replacement ``<name>`` is a *secret hash* of the value from argument ``name`` (see :py:func:`incr_counter` and :py:func:`incr_sliding_window`).
Definition at line 159 of file redislib.py.
searx.redislib.incr_counter | ( | client, | |
str | name, | ||
int | limit = 0, | ||
int | expire = 0 ) |
Increment a counter and return the new value. If counter with redis key ``SearXNG_counter_<name>`` does not exists it is created with initial value 1 returned. The replacement ``<name>`` is a *secret hash* of the value from argument ``name`` (see :py:func:`secret_hash`). The implementation of the redis counter is the lua script from string :py:obj:`INCR_COUNTER`. :param name: name of the counter :type name: str :param expire: live-time of the counter in seconds (default ``None`` means infinite). :type expire: int / see EXPIRE_ :param limit: limit where the counter stops to increment (default ``None``) :type limit: int / limit is 2^64 see INCR_ :return: value of the incremented counter :type return: int .. _EXPIRE: https://redis.io/commands/expire/ .. _INCR: https://redis.io/commands/incr/ A simple demo of a counter with expire time and limit:: >>> for i in range(6): ... i, incr_counter(client, "foo", 3, 5) # max 3, duration 5 sec ... time.sleep(1) # from the third call on max has been reached ... (0, 1) (1, 2) (2, 3) (3, 3) (4, 3) (5, 1)
Definition at line 112 of file redislib.py.
searx.redislib.incr_sliding_window | ( | client, | |
str | name, | ||
int | duration ) |
Increment a sliding-window counter and return the new value. If counter with redis key ``SearXNG_counter_<name>`` does not exists it is created with initial value 1 returned. The replacement ``<name>`` is a *secret hash* of the value from argument ``name`` (see :py:func:`secret_hash`). :param name: name of the counter :type name: str :param duration: live-time of the sliding window in seconds :typeduration: int :return: value of the incremented counter :type return: int The implementation of the redis counter is the lua script from string :py:obj:`INCR_SLIDING_WINDOW`. The lua script uses `sorted sets in Redis`_ to implement a sliding window for the redis key ``SearXNG_counter_<name>`` (ZADD_). The current TIME_ is used to score the items in the sorted set and the time window is moved by removing items with a score lower current time minus *duration* time (ZREMRANGEBYSCORE_). The EXPIRE_ time (the duration of the sliding window) is refreshed on each call (increment) and if there is no call in this duration, the sorted set expires from the redis DB. The return value is the amount of items in the sorted set (ZCOUNT_), what means the number of calls in the sliding window. .. _Sorted sets in Redis: https://redis.com/ebook/part-1-getting-started/chapter-1-getting-to-know-redis/1-2-what-redis-data-structures-look-like/1-2-5-sorted-sets-in-redis/ .. _TIME: https://redis.io/commands/time/ .. _ZADD: https://redis.io/commands/zadd/ .. _EXPIRE: https://redis.io/commands/expire/ .. _ZREMRANGEBYSCORE: https://redis.io/commands/zremrangebyscore/ .. _ZCOUNT: https://redis.io/commands/zcount/ A simple demo of the sliding window:: >>> for i in range(5): ... incr_sliding_window(client, "foo", 3) # duration 3 sec ... time.sleep(1) # from the third call (second) on the window is moved ... 1 2 3 3 3 >>> time.sleep(3) # wait until expire >>> incr_sliding_window(client, "foo", 3) 1
Definition at line 182 of file redislib.py.
searx.redislib.lua_script_storage | ( | client, | |
script ) |
Returns a redis :py:obj:`Script <redis.commands.core.CoreCommands.register_script>` instance. Due to performance reason the ``Script`` object is instantiated only once for a client (``client.register_script(..)``) and is cached in :py:obj:`LUA_SCRIPT_STORAGE`.
Definition at line 21 of file redislib.py.
searx.redislib.purge_by_prefix | ( | client, | |
str | prefix = "SearXNG_" ) |
Purge all keys with ``prefix`` from database. Queries all keys in the database by the given prefix and set expire time to zero. The default prefix will drop all keys which has been set by SearXNG (drops SearXNG schema entirely from database). The implementation is the lua script from string :py:obj:`PURGE_BY_PREFIX`. The lua script uses EXPIRE_ instead of DEL_: if there are a lot keys to delete and/or their values are big, `DEL` could take more time and blocks the command loop while `EXPIRE` turns back immediate. :param prefix: prefix of the key to delete (default: ``SearXNG_``) :type name: str .. _EXPIRE: https://redis.io/commands/expire/ .. _DEL: https://redis.io/commands/del/
Definition at line 52 of file redislib.py.
searx.redislib.secret_hash | ( | str | name | ) |
Creates a hash of the ``name``. Combines argument ``name`` with the ``secret_key`` from :ref:`settings server`. This function can be used to get a more anonymized name of a Redis KEY. :param name: the name to create a secret hash for :type name: str
Definition at line 75 of file redislib.py.
str searx.redislib.INCR_COUNTER |
Definition at line 90 of file redislib.py.
str searx.redislib.INCR_SLIDING_WINDOW |
Definition at line 169 of file redislib.py.
dict searx.redislib.LUA_SCRIPT_STORAGE = {} |
Definition at line 16 of file redislib.py.
str searx.redislib.PURGE_BY_PREFIX |
Definition at line 44 of file redislib.py.