.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.cache.ExpireCache Class Reference
Inheritance diagram for searx.cache.ExpireCache:
Collaboration diagram for searx.cache.ExpireCache:

Public Member Functions

bool set (self, str key, typing.Any value, int|None expire, str|None ctx=None)
typing.Any get (self, str key, typing.Any default=None, str|None ctx=None)
bool maintenance (self, bool force=False, bool truncate=False)
ExpireCacheStats state (self)
bytes serialize (self, typing.Any value)
typing.Any deserialize (self, bytes value)
str secret_hash (self, str|bytes name)

Static Public Member Functions

"ExpireCacheSQLite" build_cache (ExpireCacheCfg cfg)
str normalize_name (str name)

Static Public Attributes

str hash_token = "hash_token"

Detailed Description

Abstract base class for the implementation of a key/value cache
with expire date.

Definition at line 120 of file cache.py.

Member Function Documentation

◆ build_cache()

"ExpireCacheSQLite" searx.cache.ExpireCache.build_cache ( ExpireCacheCfg cfg)
static
Factory to build a caching instance.

.. note::

   Currently, only the SQLite adapter is available, but other database
   types could be implemented in the future, e.g. a Valkey (Redis)
   adapter.

Definition at line 171 of file cache.py.

171 def build_cache(cfg: ExpireCacheCfg) -> "ExpireCacheSQLite":
172 """Factory to build a caching instance.
173
174 .. note::
175
176 Currently, only the SQLite adapter is available, but other database
177 types could be implemented in the future, e.g. a Valkey (Redis)
178 adapter.
179 """
180 return ExpireCacheSQLite(cfg)
181

◆ deserialize()

typing.Any searx.cache.ExpireCache.deserialize ( self,
bytes value )

Definition at line 194 of file cache.py.

194 def deserialize(self, value: bytes) -> typing.Any:
195 obj = pickle.loads(value)
196 return obj
197

Referenced by searx.cache.ExpireCacheSQLite.get(), searx.cache.ExpireCacheSQLite.pairs(), and searx.cache.ExpireCacheSQLite.state().

Here is the caller graph for this function:

◆ get()

typing.Any searx.cache.ExpireCache.get ( self,
str key,
typing.Any default = None,
str | None ctx = None )
Return *value* of *key*.  If key is unset, ``None`` is returned.

Reimplemented in searx.cache.ExpireCacheSQLite.

Definition at line 149 of file cache.py.

149 def get(self, key: str, default: typing.Any = None, ctx: str | None = None) -> typing.Any:
150 """Return *value* of *key*. If key is unset, ``None`` is returned."""
151

Referenced by searx.result_types._base.LegacyResult.__init__(), searx.result_types._base.LegacyResult.defaults_from(), and searx.answerers._core.AnswerStorage.register().

Here is the caller graph for this function:

◆ maintenance()

bool searx.cache.ExpireCache.maintenance ( self,
bool force = False,
bool truncate = False )
Performs maintenance on the cache.

``force``:
  Maintenance should be carried out even if the maintenance interval has
  not yet been reached.

``truncate``:
  Truncate the entire cache, which is necessary, for example, if the
  password has changed.

Reimplemented in searx.cache.ExpireCacheSQLite.

Definition at line 153 of file cache.py.

153 def maintenance(self, force: bool = False, truncate: bool = False) -> bool:
154 """Performs maintenance on the cache.
155
156 ``force``:
157 Maintenance should be carried out even if the maintenance interval has
158 not yet been reached.
159
160 ``truncate``:
161 Truncate the entire cache, which is necessary, for example, if the
162 password has changed.
163 """
164

Referenced by searx.cache.ExpireCacheSQLite.get(), searx.cache.ExpireCacheSQLite.init(), searx.cache.ExpireCacheSQLite.pairs(), searx.cache.ExpireCacheSQLite.set(), and searx.favicons.cache.FaviconCacheSQLite.set().

Here is the caller graph for this function:

◆ normalize_name()

str searx.cache.ExpireCache.normalize_name ( str name)
static
Returns a normalized name that can be used as a file name or as a SQL
table name (is used, for example, to normalize the context name).

Definition at line 183 of file cache.py.

183 def normalize_name(name: str) -> str:
184 """Returns a normalized name that can be used as a file name or as a SQL
185 table name (is used, for example, to normalize the context name)."""
186
187 _valid = "-_." + string.ascii_letters + string.digits
188 return "".join([c for c in name if c in _valid])
189

Referenced by searx.cache.ExpireCacheSQLite.get(), searx.cache.ExpireCacheSQLite.pairs(), and searx.cache.ExpireCacheSQLite.set().

Here is the caller graph for this function:

◆ secret_hash()

str searx.cache.ExpireCache.secret_hash ( self,
str | bytes name )
Creates a hash of the argument ``name``.  The hash value is formed
from the ``name`` combined with the :py:obj:`password
<ExpireCacheCfg.password>`.  Can be used, for example, to make the
``key`` stored in the DB unreadable for third parties.

Definition at line 198 of file cache.py.

198 def secret_hash(self, name: str | bytes) -> str:
199 """Creates a hash of the argument ``name``. The hash value is formed
200 from the ``name`` combined with the :py:obj:`password
201 <ExpireCacheCfg.password>`. Can be used, for example, to make the
202 ``key`` stored in the DB unreadable for third parties."""
203
204 if isinstance(name, str):
205 name = bytes(name, encoding='utf-8')
206 m = hmac.new(name + self.cfg.password, digestmod='sha256')
207 return m.hexdigest()
208
209

References searx.botdetection.config.Config.cfg, searx.cache.ExpireCacheSQLite.cfg, searx.favicons.cache.FaviconCacheMEM.cfg, and searx.favicons.cache.FaviconCacheSQLite.cfg.

◆ serialize()

bytes searx.cache.ExpireCache.serialize ( self,
typing.Any value )

Definition at line 190 of file cache.py.

190 def serialize(self, value: typing.Any) -> bytes:
191 dump: bytes = pickle.dumps(value)
192 return dump
193

Referenced by searx.cache.ExpireCacheSQLite.set().

Here is the caller graph for this function:

◆ set()

bool searx.cache.ExpireCache.set ( self,
str key,
typing.Any value,
int | None expire,
str | None ctx = None )
Set *key* to *value*.  To set a timeout on key use argument
``expire`` (in sec.).  If expire is unset the default is taken from
:py:obj:`ExpireCacheCfg.MAXHOLD_TIME`.  After the timeout has expired,
the key will automatically be deleted.

The ``ctx`` argument specifies the context of the ``key``.  A key is
only unique in its context.

The concrete implementations of this abstraction determine how the
context is mapped in the connected database.  In SQL databases, for
example, the context is a DB table or in a Key/Value DB it could be
a prefix for the key.

If the context is not specified (the default is ``None``) then a
default context should be used, e.g. a default table for SQL databases
or a default prefix in a Key/Value DB.

Reimplemented in searx.cache.ExpireCacheSQLite.

Definition at line 129 of file cache.py.

129 def set(self, key: str, value: typing.Any, expire: int | None, ctx: str | None = None) -> bool:
130 """Set *key* to *value*. To set a timeout on key use argument
131 ``expire`` (in sec.). If expire is unset the default is taken from
132 :py:obj:`ExpireCacheCfg.MAXHOLD_TIME`. After the timeout has expired,
133 the key will automatically be deleted.
134
135 The ``ctx`` argument specifies the context of the ``key``. A key is
136 only unique in its context.
137
138 The concrete implementations of this abstraction determine how the
139 context is mapped in the connected database. In SQL databases, for
140 example, the context is a DB table or in a Key/Value DB it could be
141 a prefix for the key.
142
143 If the context is not specified (the default is ``None``) then a
144 default context should be used, e.g. a default table for SQL databases
145 or a default prefix in a Key/Value DB.
146 """
147

◆ state()

ExpireCacheStats searx.cache.ExpireCache.state ( self)
Returns a :py:obj:`ExpireCacheStats`, which provides information
about the status of the cache.

Reimplemented in searx.cache.ExpireCacheSQLite.

Definition at line 166 of file cache.py.

166 def state(self) -> ExpireCacheStats:
167 """Returns a :py:obj:`ExpireCacheStats`, which provides information
168 about the status of the cache."""
169

Member Data Documentation

◆ hash_token

str searx.cache.ExpireCache.hash_token = "hash_token"
static

Definition at line 126 of file cache.py.

Referenced by searx.cache.ExpireCacheSQLite.init().


The documentation for this class was generated from the following file:
  • /home/andrew/Documents/code/public/searxng/searx/cache.py