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.DidInMemoryCache(*args: Any, **kwargs: Any)

Bases: DidBaseCache

set(did: str, document: DidDocument) None

Set cached DID.

Parameters:
  • did – DID.

  • document – DID document.

refresh(did: str, get_doc_callback: GetDocCallback) None

Refresh cached DID.

Parameters:
  • did – DID.

  • get_doc_callback – Get DID document callback.

delete(did: str) None

Delete cached DID.

Parameters:

did – DID.

clear() None

Clear cached DIDs.

Note

This method is used to clear all cached DIDs.

get(did: str) CachedDidResult | None

Get cached DID.

Parameters:

did – DID.

Returns:

Cached DID result or None if not found.

Return type:

CachedDidResult

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

Bases: AsyncDidBaseCache

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

Set cached DID.

Parameters:
  • did – DID.

  • document – DID document.

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

Refresh cached DID.

Parameters:
  • did – DID.

  • get_doc_callback – Get DID document callback.

async delete(did: str) None

Delete cached DID.

Parameters:

did – DID.

async clear() None

Clear cached DIDs.

Note

This method is used to clear all cached DIDs.

async get(did: str) CachedDidResult | None

Get cached DID.

Parameters:

did – DID.

Returns:

Cached DID result or None if not found.

Return type:

CachedDidResult

atproto_identity.cache.base_cache.GetDocCallback = GetDocCallback

Called to fetch a DID document when the cache misses or has gone stale.

atproto_identity.cache.base_cache.AsyncGetDocCallback = AsyncGetDocCallback

The awaitable twin of GetDocCallback.

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.

abstractmethod get(did: str) CachedDidResult | None

Get cached DID.

Parameters:

did – DID.

Returns:

Cached DID result or None if not found.

Return type:

CachedDidResult

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

Set cached DID.

Parameters:
  • did – DID.

  • document – DID document.

abstractmethod refresh(did: str, get_doc_callback: GetDocCallback) None

Refresh cached DID.

Parameters:
  • did – DID.

  • get_doc_callback – Get DID document callback.

abstractmethod delete(did: str) None

Delete cached DID.

Parameters:

did – DID.

abstractmethod clear() None

Clear cached DIDs.

Note

This method is used to clear all cached DIDs.

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.

abstractmethod async get(did: str) CachedDidResult | None

Get cached DID.

Parameters:

did – DID.

Returns:

Cached DID result or None if not found.

Return type:

CachedDidResult

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

Set cached DID.

Parameters:
  • did – DID.

  • document – DID document.

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

Refresh cached DID.

Parameters:
  • did – DID.

  • get_doc_callback – Get DID document callback.

abstractmethod async delete(did: str) None

Delete cached DID.

Parameters:

did – DID.

abstractmethod async clear() None

Clear cached DIDs.

Note

This method is used to clear all cached DIDs.

class atproto_identity.cache.models.CachedDid(document: DidDocument, updated_at: datetime)

Bases: object

Cached DID.

document: DidDocument
updated_at: datetime
class atproto_identity.cache.models.CachedDidResult(did: str, document: DidDocument, updated_at: datetime, stale: bool, expired: bool)

Bases: object

Cached DID result.

did: str
document: DidDocument
updated_at: datetime
stale: bool
expired: bool