Nostr.Cache (nostr_access v0.3.3)

View Source

Cache management for Nostr events with three-tier caching:

  • Zero results cache (TTL: 1 hour)
  • Immutable results cache (TTL: 1 day) - for queries with specific event IDs
  • Mutable results cache (TTL: 30 minutes) - for all other queries

Summary

Functions

Determines which cache table to use based on filter and results.

Gets a value from the appropriate cache based on filter.

Gets a value from cache or stores the result of the given function.

Gets a value from the zero results cache.

Creates a cache key from relays and filter.

Stores events in the appropriate cache based on filter and results.

Stores zero results in the zero results cache.

Tries to get a value from all cache tables in order of preference.

Functions

determine_cache_table(filter, results)

@spec determine_cache_table(map(), [map()]) :: atom()

Determines which cache table to use based on filter and results.

get_events(key, filter)

@spec get_events(
  {[String.t()], String.t()},
  map()
) :: {:ok, [map()] | nil} | {:error, term()}

Gets a value from the appropriate cache based on filter.

get_or_store(key, filter, fun)

@spec get_or_store({[String.t()], String.t()}, map(), (-> [map()])) :: [map()]

Gets a value from cache or stores the result of the given function.

get_zero_results(key)

@spec get_zero_results({[String.t()], String.t()}) ::
  {:ok, [map()] | nil} | {:error, term()}

Gets a value from the zero results cache.

make_key(relays, filter)

@spec make_key([String.t()], map()) :: {[String.t()], String.t()}

Creates a cache key from relays and filter.

put_events(key, events, filter)

@spec put_events({[String.t()], String.t()}, [map()], map()) ::
  {:ok, boolean()} | {:error, term()}

Stores events in the appropriate cache based on filter and results.

put_zero_results(key, results)

@spec put_zero_results(
  {[String.t()], String.t()},
  [map()]
) :: {:ok, boolean()} | {:error, term()}

Stores zero results in the zero results cache.

try_get_from_all_caches(key, filter)

@spec try_get_from_all_caches(
  {[String.t()], String.t()},
  map()
) :: {:ok, [map()]} | {:error, :not_found}

Tries to get a value from all cache tables in order of preference.