Bases: MutableMapping[str, QgsMapLayer]
Layers opened by the worker, shared by all jobs of the worker process.
Entries which were not used for longer than the TTL are evicted by
evict_expired. This bounds the growth caused by layers which are not
requested anymore (e.g. after a project was changed). It does not bound the
number of layers used within one TTL.
Not thread safe: only the thread processing the jobs may access it.
Source code in src/qgis_server_light/worker/layer_cache.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 | class LayerCache(MutableMapping[str, QgsMapLayer]):
"""Layers opened by the worker, shared by all jobs of the worker process.
Entries which were not used for longer than the TTL are evicted by
`evict_expired`. This bounds the growth caused by layers which are not
requested anymore (e.g. after a project was changed). It does not bound the
number of layers used within one TTL.
Not thread safe: only the thread processing the jobs may access it.
"""
def __init__(self, ttl_seconds: float = DEFAULT_TTL_SECONDS) -> None:
self.ttl_seconds = ttl_seconds
self._layers: dict[str, QgsMapLayer] = {}
self._last_used: dict[str, float] = {}
def __getitem__(self, cache_name: str) -> QgsMapLayer:
layer = self._layers[cache_name]
self._last_used[cache_name] = monotonic()
return layer
def __setitem__(self, cache_name: str, layer: QgsMapLayer) -> None:
self._layers[cache_name] = layer
self._last_used[cache_name] = monotonic()
def __delitem__(self, cache_name: str) -> None:
del self._layers[cache_name]
del self._last_used[cache_name]
def __contains__(self, cache_name: object) -> bool:
return cache_name in self._layers
def __iter__(self) -> Iterator[str]:
return iter(self._layers)
def __len__(self) -> int:
return len(self._layers)
def evict_expired(self) -> int:
"""Evicts all layers which were not used within the TTL.
Returns:
The number of evicted layers.
"""
expired_before = monotonic() - self.ttl_seconds
expired = [name for name, used in self._last_used.items() if used < expired_before]
for cache_name in expired:
del self[cache_name]
if expired:
# cache names contain the layer source (credentials), they are not logged
logging.info(
f"Evicted {len(expired)} layers unused for more than {self.ttl_seconds}s "
f"from cache, {len(self)} layers remain"
)
return len(expired)
|
ttl_seconds = ttl_seconds
instance-attribute
__contains__(cache_name: object) -> bool
Source code in src/qgis_server_light/worker/layer_cache.py
| def __contains__(self, cache_name: object) -> bool:
return cache_name in self._layers
|
__delitem__(cache_name: str) -> None
Source code in src/qgis_server_light/worker/layer_cache.py
| def __delitem__(self, cache_name: str) -> None:
del self._layers[cache_name]
del self._last_used[cache_name]
|
__getitem__(cache_name: str) -> QgsMapLayer
Source code in src/qgis_server_light/worker/layer_cache.py
| def __getitem__(self, cache_name: str) -> QgsMapLayer:
layer = self._layers[cache_name]
self._last_used[cache_name] = monotonic()
return layer
|
__init__(ttl_seconds: float = DEFAULT_TTL_SECONDS) -> None
Source code in src/qgis_server_light/worker/layer_cache.py
| def __init__(self, ttl_seconds: float = DEFAULT_TTL_SECONDS) -> None:
self.ttl_seconds = ttl_seconds
self._layers: dict[str, QgsMapLayer] = {}
self._last_used: dict[str, float] = {}
|
__iter__() -> Iterator[str]
Source code in src/qgis_server_light/worker/layer_cache.py
| def __iter__(self) -> Iterator[str]:
return iter(self._layers)
|
__len__() -> int
Source code in src/qgis_server_light/worker/layer_cache.py
| def __len__(self) -> int:
return len(self._layers)
|
__setitem__(cache_name: str, layer: QgsMapLayer) -> None
Source code in src/qgis_server_light/worker/layer_cache.py
| def __setitem__(self, cache_name: str, layer: QgsMapLayer) -> None:
self._layers[cache_name] = layer
self._last_used[cache_name] = monotonic()
|
evict_expired() -> int
Evicts all layers which were not used within the TTL.
Returns:
-
int
–
The number of evicted layers.
Source code in src/qgis_server_light/worker/layer_cache.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 | def evict_expired(self) -> int:
"""Evicts all layers which were not used within the TTL.
Returns:
The number of evicted layers.
"""
expired_before = monotonic() - self.ttl_seconds
expired = [name for name, used in self._last_used.items() if used < expired_before]
for cache_name in expired:
del self[cache_name]
if expired:
# cache names contain the layer source (credentials), they are not logged
logging.info(
f"Evicted {len(expired)} layers unused for more than {self.ttl_seconds}s "
f"from cache, {len(self)} layers remain"
)
return len(expired)
|