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

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

◆ deserialize()

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

Definition at line 195 of file cache.py.

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

Referenced by searx.cache.ExpireCacheSQLite.get(), 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 150 of file cache.py.

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

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

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

Referenced by searx.cache.ExpireCacheSQLite.get(), searx.cache.ExpireCacheSQLite.init(), 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 184 of file cache.py.

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

Referenced by searx.cache.ExpireCacheSQLite.get(), 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 199 of file cache.py.

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

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

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

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

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

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

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

Member Data Documentation

◆ hash_token

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

Definition at line 127 of file cache.py.

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


The documentation for this class was generated from the following file: