Cache

Cache could be used to store previously resolved DID Documents. SDK provides DidInMemoryCache and DidBaseCache classes.

DidInMemoryCache is a simple implementation of DidBaseCache that stores data in memory. Feel free to use it as real cache or as a reference implementation.

DidBaseCache is an abstract class that could be used to implement custom cache. Please note that there is 2 base classes. One for synchronous and another for asynchronous cache.

Here is an example of how to use DidInMemoryCache with IdResolver:

from atproto import DidInMemoryCache, IdResolver  # for async use AsyncDidInMemoryCache and AsyncIdResolver

cache = DidInMemoryCache()
resolver = IdResolver(cache=cache)
did_doc = resolver.did.resolve('did:web:feed.atproto.blue')

# Now did_document is cached and could be retrieved without network request
did_doc = resolver.did.resolve('did:web:feed.atproto.blue')

# Clear cache
cache.clear()

# Now did_document is not cached and will be retrieved with network request
did_doc = resolver.did.resolve('did:web:feed.atproto.blue')
class atproto_identity.cache.in_memory_cache.AsyncDidInMemoryCache(*args: Any, **kwargs: Any)

Bases: AsyncDidBaseCache

async clear() None

Clear cached DIDs.

Note

This method is used to clear all cached DIDs.

async delete(did: str) None

Delete cached DID.

Parameters:

did – DID.

async get(did: str) CachedDidResult | None

Get cached DID.

Parameters:

did – DID.

Returns:

Cached DID result or None if not found.

Return type:

CachedDidResult

async refresh(did: str, get_doc_callback: AsyncGetDocCallback) None

Refresh cached DID.

Parameters:
  • did – DID.

  • get_doc_callback – Get DID document callback.

async set(did: str, document: DidDocument) None

Set cached DID.

Parameters:
  • did – DID.

  • document – DID document.

class atproto_identity.cache.in_memory_cache.DidInMemoryCache(*args: Any, **kwargs: Any)

Bases: DidBaseCache

clear() None

Clear cached DIDs.

Note

This method is used to clear all cached DIDs.

delete(did: str) None

Delete cached DID.

Parameters:

did – DID.

get(did: str) CachedDidResult | None

Get cached DID.

Parameters:

did – DID.

Returns:

Cached DID result or None if not found.

Return type:

CachedDidResult

refresh(did: str, get_doc_callback: GetDocCallback) None

Refresh cached DID.

Parameters:
  • did – DID.

  • get_doc_callback – Get DID document callback.

set(did: str, document: DidDocument) None

Set cached DID.

Parameters:
  • did – DID.

  • document – DID document.

class atproto_identity.cache.base_cache.AsyncDidBaseCache(stale_ttl: int | None = None, max_ttl: int | None = None)

Bases: _DidBaseCache, ABC

Asynchronous Abstract DID Cache.

Parameters:
  • stale_ttl – Stale TTL in seconds. Default is 1 hour.

  • max_ttl – Max TTL in seconds. Default is 1 day.

abstract async clear() None

Clear cached DIDs.

Note

This method is used to clear all cached DIDs.

abstract async delete(did: str) None

Delete cached DID.

Parameters:

did – DID.

abstract async get(did: str) CachedDidResult | None

Get cached DID.

Parameters:

did – DID.

Returns:

Cached DID result or None if not found.

Return type:

CachedDidResult

abstract async refresh(did: str, get_doc_callback: Callable[[], Coroutine[Any, Any, DidDocument | None]]) None

Refresh cached DID.

Parameters:
  • did – DID.

  • get_doc_callback – Get DID document callback.

abstract async set(did: str, document: DidDocument) None

Set cached DID.

Parameters:
  • did – DID.

  • document – DID document.

class atproto_identity.cache.base_cache.DidBaseCache(stale_ttl: int | None = None, max_ttl: int | None = None)

Bases: _DidBaseCache, ABC

Abstract DID Cache.

Parameters:
  • stale_ttl – Stale TTL in seconds. Default is 1 hour.

  • max_ttl – Max TTL in seconds. Default is 1 day.

abstract clear() None

Clear cached DIDs.

Note

This method is used to clear all cached DIDs.

abstract delete(did: str) None

Delete cached DID.

Parameters:

did – DID.

abstract get(did: str) CachedDidResult | None

Get cached DID.

Parameters:

did – DID.

Returns:

Cached DID result or None if not found.

Return type:

CachedDidResult

abstract refresh(did: str, get_doc_callback: Callable[[], DidDocument | None]) None

Refresh cached DID.

Parameters:
  • did – DID.

  • get_doc_callback – Get DID document callback.

abstract set(did: str, document: DidDocument) None

Set cached DID.

Parameters:
  • did – DID.

  • document – DID document.