Skip to main content
`Ctrl`+`K`

# pystac.cache

class pystac.cache.CollectionCache(cached_ids: dict[str, Collection | dict[str, Any]] | None = None, cached_hrefs: dict[str, Collection | dict[str, Any]] | None = None)

  Cache of collections that can be used to avoid re-reading Collection JSON in `pystac.serialization.merge_common_properties`. The CollectionCache will contain collections as either as dicts or PySTAC Collections, and will set Collection JSON that it reads in order to merge in common properties.

  cache(collection: Collection | dict[str, Any], href: str | None = None) → None

    Caches a collection JSON.

  cached_hrefs: dict[str, Collection | dict[str, Any]]
  cached_ids: dict[str, Collection | dict[str, Any]]
  contains_id(collection_id: str) → bool
  get_by_href(href: str) → Collection | dict[str, Any] | None
  get_by_id(collection_id: str) → Collection | dict[str, Any] | None

class pystac.cache.ResolvedObjectCache(id_keys_to_objects: dict[str, STACObject] | None = None, hrefs_to_objects: dict[str, STACObject] | None = None, ids_to_collections: dict[str, Collection] | None = None)

  This class tracks resolved objects tied to root catalogs. A STAC object is ‘resolved’ when it is a Python Object; a link to a STAC object such as a Catalog or Item is considered “unresolved” if it’s target is pointed at an HREF of the object.

  Tracking resolved objects allows us to tie together the same instances when there are loops in the Graph of the STAC catalog (e.g. a LabelItem can link to a rel:source, and if that STAC Item exists in the same root catalog they should refer to the same Python object).

  Resolution tracking is important when copying STACs in-memory: In order for object links to refer to the copy of STAC Objects rather than their originals, we have to keep track of the resolved STAC Objects and replace them with their copies.

  Parameters:

    - id_keys_to_objects – Existing cache of a key made up of the STACObject and it’s parents IDs mapped to the cached STACObject.

    - hrefs_to_objects – STAC Object HREFs matched to their cached object.

    - ids_to_collections – Map of collection IDs to collections.

  as_collection_cache() → CollectionCache
  cache(obj: STACObject) → None

    Set the given object into the cache.

    Parameters:

      obj – The object to cache

  contains_collection_id(collection_id: str) → bool

    Returns True if there is a collection with given collection ID is cached.

  get(obj: STACObject) → STACObject | None

    Get the cached object that has the same cache key as the given object.

    Parameters:

      obj – The given object who’s cache key will be checked against the cache.

    Returns:

      Either the cached object that has the same cache key as the given object, or None

    Return type:

      STACObject or None

  get_by_href(href: str) → STACObject | None

    Gets the cached object at href.

    Parameters:

      href – The href to use as the key for the cached object.

    Returns:

      Returns the STACObject if cached, otherwise None.

    Return type:

      STACObject or None

  get_collection_by_id(id: str) → Collection | None

    Retrieved a cached Collection by its ID.

    Parameters:

      id – The ID of the collection.

    Returns:

      Returns the collection if there is one cached

        with the given ID, otherwise None.

    Return type:

      Collection or None

  get_or_cache(obj: STACObject) → STACObject

    Gets the STACObject that is the cached version of the given STACObject; or, if none exists, sets the cached object to the given object.

    Parameters:

      obj – The given object who’s cache key will be checked against the cache.

    Returns:

      Either the cached object that has the same cache key as the given object, or the given object.

    Return type:

      STACObject

  hrefs_to_objects: dict[str, STACObject]

    STAC Object HREFs matched to their cached object.

  id_keys_to_objects: dict[str, STACObject]

    Existing cache of a key made up of the STACObject and it’s parents IDs mapped to the cached STACObject.

  ids_to_collections: dict[str, Collection]

    Map of collection IDs to collections.

  static merge(first: ResolvedObjectCache, second: ResolvedObjectCache) → ResolvedObjectCache

    Merges two ResolvedObjectCache.

    The merged cache will give preference to the first argument; that is, if there are cached keys that exist in both the first and second cache, the object cached in the first will be cached in the resulting merged ResolvedObjectCache.

    Parameters:

      - first – The first cache to merge. This cache will be the preferred cache for objects in the case of ID conflicts.

      - second – The second cache to merge.

    Returns:

      The resulting merged cache.

    Return type:

      ResolvedObjectCache

  remove(obj: STACObject) → None

    Removes any cached object that matches the given object’s cache key.

    Parameters:

      obj – The object to remove

class pystac.cache.ResolvedObjectCollectionCache(resolved_object_cache: ResolvedObjectCache, cached_ids: dict[str, Collection | dict[str, Any]] | None = None, cached_hrefs: dict[str, Collection | dict[str, Any]] | None = None)
  cache(collection: Collection | dict[str, Any], href: str | None = None) → None

    Caches a collection JSON.

  contains_id(collection_id: str) → bool
  get_by_href(href: str) → Collection | dict[str, Any] | None
  get_by_id(collection_id: str) → Collection | dict[str, Any] | None
  static merge(resolved_object_cache: ResolvedObjectCache, first: ResolvedObjectCollectionCache | None, second: ResolvedObjectCollectionCache | None) → ResolvedObjectCollectionCache
  resolved_object_cache: ResolvedObjectCache

pystac.cache.get_cache_key(stac_object: STACObject) → tuple[str, bool]

  Produce a cache key for the given STAC object.

  If a self href is set, use that as the cache key. If not, use a key that combines this object’s ID with it’s parents’ IDs.

  Returns:

    A tuple with the cache key as the first

      element and a boolean that is true if the cache key is the object’s HREF as the second element.

  Return type:

    Tuple[str, bool]