Class: QgsVectorLayerCache

Caches features for a given QgsVectorLayer.

The cached features can be indexed by QgsAbstractCacheIndex.

Proper indexing for a given use-case may speed up performance substantially.

Class Hierarchy

Inheritance diagram of qgis.core.QgsVectorLayerCache

Base classes

QObject

Methods

addCacheIndex

Adds a QgsAbstractCacheIndex to this cache.

cacheGeometry

Returns True if the cache will fetch and cache feature geometries.

cacheSize

Returns the maximum number of features this cache will hold.

cacheSubsetOfAttributes

Returns the list (possibly a subset) of cached attributes.

cachedFeatureIds

Returns the set of feature IDs for features which are cached.

checkInformationCovered

Checks if the information required to complete the request is cached.

completeFeatureAtId

Gets the feature at the given feature id with all attributes and geometry, if the cached feature already contains all attributes and geometry, calling this function has the same effect as calling featureAtId().

featureAtId

Gets the feature at the given feature id.

featureAtIdWithAllAttributes

Gets the feature at the given feature id with all attributes, if the cached feature already contains all attributes, calling this function has the same effect as calling featureAtId().

featureCount

Returns the number of features contained in the source, or -1 if the feature count is unknown.

featureRemoved

Gets called, whenever a feature has been removed.

fields

Returns the fields associated with features in the cache.

getFeature

Query the layer for the feature with the given id.

getFeatures

Query this VectorLayerCache for features.

hasFullCache

Returns True if the cache is complete, ie it contains all features.

isFidCached

Check if a certain feature id is cached.

layer

Returns the layer to which this cache belongs

removeCachedFeature

Removes the feature identified by fid from the cache if present.

requestCompleted

Gets called, whenever the full list of feature ids for a certain request is known.

setCacheAddedAttributes

If this is enabled, the subset of cached attributes will automatically be extended to also include newly added attributes.

setCacheGeometry

Enable or disable the caching of geometries

setCacheSize

Sets the maximum number of features to keep in the cache.

setCacheSubsetOfAttributes

Set the list (possibly a subset) of attributes to be cached.

setFullCache

This enables or disables full caching.

sourceCrs

Returns the coordinate reference system for features in the cache.

wkbType

Returns the geometry type for features in the cache.

Signals

attributeValueChanged

Emitted when an attribute is changed.

cachedLayerDeleted

Is emitted when the cached layer is deleted.

featureAdded

Emitted when a new feature has been added to the layer and this cache.

finished

When filling the cache, this signal gets emitted once the cache is fully initialized.

invalidated

The cache has been invalidated and cleared.

class qgis.core.QgsVectorLayerCache[source]

Bases: QObject

addCacheIndex(self, cacheIndex: QgsAbstractCacheIndex | None)[source]

Adds a QgsAbstractCacheIndex to this cache. Cache indices know about features present in this cache and decide, if enough information is present in the cache to respond to a QgsFeatureRequest. The layer cache will take ownership of the index.

Parameters:

cacheIndex (Optional[QgsAbstractCacheIndex]) – The cache index to add.

signal attributeValueChanged(fid: QgsFeatureId, field: int, value: object)[source]

Emitted when an attribute is changed. Is re-emitted after the layer itself emits this signal. You should connect to this signal, to be sure, to not get a cached value if querying the cache.

Parameters:
  • fid (QgsFeatureId)

  • field (int)

  • value (object)

cacheGeometry(self) bool[source]

Returns True if the cache will fetch and cache feature geometries.

Return type:

bool

cacheSize(self) int[source]

Returns the maximum number of features this cache will hold. In case full caching is enabled, this number can change, as new features get added.

Return type:

int

Returns:

int

cacheSubsetOfAttributes(self) List[int][source]

Returns the list (possibly a subset) of cached attributes.

Note

By default the cache will store all layer’s attributes.

Added in version 3.32.

Return type:

List[int]

cachedFeatureIds(self) Any[source]

Returns the set of feature IDs for features which are cached.

See also

isFidCached()

Return type:

Any

signal cachedLayerDeleted[source]

Is emitted when the cached layer is deleted. Is emitted when the cached layers layerDelete() signal is being emitted, but before the local reference to it has been set to None. So call to layer() will still return a valid pointer for cleanup purpose.

checkInformationCovered(self, featureRequest: QgsFeatureRequest) bool[source]

Checks if the information required to complete the request is cached. i.e. If all attributes required and the geometry is held in the cache. Please note, that this does not check, if the requested features are cached.

Parameters:

featureRequest (QgsFeatureRequest) – The QgsFeatureRequest to be answered

Return type:

bool

Returns:

True if the information is being cached, False if not

completeFeatureAtId(self, featureId: int, skipCache: bool = False)[source]

Gets the feature at the given feature id with all attributes and geometry, if the cached feature already contains all attributes and geometry, calling this function has the same effect as calling featureAtId().

Considers the changed, added, deleted and permanent features

Parameters:
  • featureId (int) – The id of the feature to query

  • skipCache (bool = False) -> (bool) – Will query the layer regardless if the feature is in the cache already

Returns:

  • True in case of success

  • feature: The result of the operation will be written to this feature

See also

featureAtId()

Added in version 3.44.

signal featureAdded(fid: QgsFeatureId)[source]

Emitted when a new feature has been added to the layer and this cache. You should connect to this signal instead of the layers’, if you want to be sure that this cache has updated information for the new feature

Parameters:

fid (QgsFeatureId) – The featureid of the changed feature

featureAtId(self, featureId: int, skipCache: bool = False)[source]

Gets the feature at the given feature id. Considers the changed, added, deleted and permanent features

Parameters:
  • featureId (int) – The id of the feature to query

  • skipCache (bool = False) -> (bool) – Will query the layer regardless if the feature is in the cache already

Returns:

  • True in case of success

  • feature: The result of the operation will be written to this feature

featureAtIdWithAllAttributes(self, featureId: int, skipCache: bool = False)[source]

Gets the feature at the given feature id with all attributes, if the cached feature already contains all attributes, calling this function has the same effect as calling featureAtId().

Considers the changed, added, deleted and permanent features

Parameters:
  • featureId (int) – The id of the feature to query

  • skipCache (bool = False) -> (bool) – Will query the layer regardless if the feature is in the cache already

Returns:

  • True in case of success

  • feature: The result of the operation will be written to this feature

See also

featureAtId()

Added in version 3.32.

featureCount(self) int

Returns the number of features contained in the source, or -1 if the feature count is unknown.

Return type:

int

featureRemoved(self, fid: int)[source]

Gets called, whenever a feature has been removed. Broadcasts this information to indices, so they can invalidate their cache if required.

Parameters:

fid (int) – The feature id of the removed feature.

fields(self) QgsFields[source]

Returns the fields associated with features in the cache.

Return type:

QgsFields

signal finished[source]

When filling the cache, this signal gets emitted once the cache is fully initialized.

getFeature(self, fid: int) QgsFeature[source]

Query the layer for the feature with the given id. If there is no such feature, the returned feature will be invalid.

Parameters:

fid (int)

Return type:

QgsFeature

getFeatures(self, featureRequest: QgsFeatureRequest = QgsFeatureRequest()) QgsFeatureIterator[source]

Query this VectorLayerCache for features. If the VectorLayerCache (and moreover any of its indices) is able to satisfy the request, the returned QgsFeatureIterator will iterate over cached features. If it’s not possible to fully satisfy the request from the cache, part or all of the features will be requested from the data provider.

Parameters:

featureRequest (QgsFeatureRequest = QgsFeatureRequest()) – The request specifying filter and required data.

Return type:

QgsFeatureIterator

Returns:

An iterator over the requested data.

getFeatures(self, expression: str | None) QgsFeatureIterator[source]

Query the layer for features matching a given expression.

Parameters:

expression (Optional[str])

Return type:

QgsFeatureIterator

getFeatures(self, fids: Any) QgsFeatureIterator[source]

Query the layer for the features with the given ids.

Parameters:

fids (Any)

Return type:

QgsFeatureIterator

getFeatures(self, rectangle: QgsRectangle) QgsFeatureIterator[source]

Query the layer for the features which intersect the specified rectangle.

Parameters:

rectangle (QgsRectangle)

Return type:

QgsFeatureIterator

hasFullCache(self) bool[source]

Returns True if the cache is complete, ie it contains all features. This may happen as a result of a call to setFullCache() or by through a feature request which resulted in all available features being cached.

See also

setFullCache()

Return type:

bool

signal invalidated[source]

The cache has been invalidated and cleared. Note that when a cache is invalidated the fullCache() setting will be cleared, and a full cache rebuild via setFullCache( True ) will need to be performed.

isFidCached(self, fid: int) bool[source]

Check if a certain feature id is cached.

Parameters:

fid (int) – The feature id to look for

Return type:

bool

Returns:

True if this id is in the cache

layer(self) QgsVectorLayer | None[source]

Returns the layer to which this cache belongs

Return type:

Optional[QgsVectorLayer]

removeCachedFeature(self, fid: int) bool[source]

Removes the feature identified by fid from the cache if present.

Parameters:

fid (int) – The id of the feature to delete

Return type:

bool

Returns:

True if the feature was removed, False if the feature id was not found in the cache

requestCompleted(self, featureRequest: QgsFeatureRequest, fids: Any)[source]

Gets called, whenever the full list of feature ids for a certain request is known. Broadcasts this information to indices, so they can update their tables.

Parameters:
  • featureRequest (QgsFeatureRequest) – The feature request that was answered

  • fids (Any) – The feature ids that have been returned

setCacheAddedAttributes(self, cacheAddedAttributes: bool)[source]

If this is enabled, the subset of cached attributes will automatically be extended to also include newly added attributes.

Parameters:

cacheAddedAttributes (bool) – Automatically cache new attributes

setCacheGeometry(self, cacheGeometry: bool)[source]

Enable or disable the caching of geometries

Parameters:

cacheGeometry (bool) – Enable or disable the caching of geometries

See also

cacheGeometry()

setCacheSize(self, cacheSize: int)[source]

Sets the maximum number of features to keep in the cache. Some features will be removed from the cache if the number is smaller than the previous size of the cache.

Parameters:

cacheSize (int) – indicates the maximum number of features to keep in the cache

setCacheSubsetOfAttributes(self, attributes: Iterable[int])[source]

Set the list (possibly a subset) of attributes to be cached.

Note

By default the cache will store all layer’s attributes.

Parameters:

attributes (Iterable[int]) – The attributes to be cached

setFullCache(self, fullCache: bool)[source]

This enables or disables full caching. If enabled, all features will be held in the cache. The cache size will incrementally be increased to offer space for all features. When enabled, all features will be read into cache. As this feature will most likely be used for slow data sources, be aware, that the call to this method might take a long time.

Parameters:

fullCache (bool) – True: enable full caching, False: disable full caching

Note

when a cache is invalidated() (e.g. by adding an attribute to a layer) this setting is reset. A full cache rebuild must be performed by calling setFullCache( True ) again.

See also

hasFullCache()

sourceCrs(self) QgsCoordinateReferenceSystem[source]

Returns the coordinate reference system for features in the cache.

Return type:

QgsCoordinateReferenceSystem

wkbType(self) Qgis.WkbType[source]

Returns the geometry type for features in the cache.

Return type:

Qgis.WkbType