.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, 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

ExpireCache build_cache (ExpireCacheCfg cfg)
str normalize_name (str name)

Static Public Attributes

ExpireCacheCfg hash_token = "hash_token"

Detailed Description

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

Definition at line 122 of file cache.py.

Member Function Documentation

◆ build_cache()

ExpireCache 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 173 of file cache.py.

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

◆ deserialize()

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

Definition at line 196 of file cache.py.

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

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,
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 151 of file cache.py.

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

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 155 of file cache.py.

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

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 185 of file cache.py.

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

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 200 of file cache.py.

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

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 192 of file cache.py.

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

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 131 of file cache.py.

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

◆ 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 168 of file cache.py.

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

Member Data Documentation

◆ hash_token

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

Definition at line 128 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