Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/earthstar/src/entries/universal.ts>ReplicaCache

Storage for private, distributed, offline-first applications.
Go to Latest
class ReplicaCache
import { ReplicaCache } from "https://deno.land/x/earthstar@v10.2.0/src/entries/universal.ts";

A cached, synchronous interface to a replica, useful for reactive abstractions. Always returns results from its cache, and proxies the query to the backing replica in case of a cache miss.

const cache = new ReplicaCache(myReplica);
const pngQuery = { filter: { pathEndsWith: ".png" } };
let pngDocs = cache.queryDocs(pngQuery);
cache.onCacheUpdate(() => {
   pngDocs = cache.queryDocs(pngQuery);
});

Constructors

new
ReplicaCache(
replica: Replica,
timeToLive?: number,
onCacheUpdatedWrapper?: (cb: () => void) => void,
)

Create a new ReplicaCache.

Properties

private
attachmentCache: Map<string, AttachmentCacheEntry<any>>
private
closed: boolean
private
docCache: Map<string, DocsCacheEntry<DocBase<string>>>
private
onCacheUpdatedCallbacks: Set<() => void>
private
onFireCacheUpdatedsWrapper: (cb: () => void) => unknown
private
replica: Replica
private
timeToLive: number
version: number

Methods

_updateCache(key: string, doc: DocBase<string>): void

Fetch all versions of all docs from the cache. Returns an empty array in case of a cache miss, and queries the backing replica.

Fetch all versions of all docs from a certain path from the cache. Returns an empty array in case of a cache miss, and queries the backing replica.

getLatestDocAtPath<F = DefaultFormat>(path: Path, format?: FormatArg<F>): FormatDocType<F> | undefined

Fetch latest version of a doc at a path from the cache. Returns an empty array in case of a cache miss, and queries the backing replica.

Fetch latest versions of all docs from the cache. Returns an empty array in case of a cache miss, and queries the backing replica.

onCacheUpdated(callback: () => void): () => void

Subscribes to the cache, calling a callback when previously returned results can be considered stale. Returns a function for unsubscribing.

Call this method on the backing replica.

queryAuthors<F = DefaultFormats>(query?: Omit<Query<[string]>, "formats">, formats?: FormatsArg<F>): AuthorAddress[]

Returns an array of all unique authors of documents returned by a given query.

queryDocs<F = DefaultFormats>(query?: Omit<Query<[string]>, "formats">, formats?: FormatsArg<F>): FormatDocType<F>[]

Fetch docs matching a query from the cache. Returns an empty array in case of a cache miss, and queries the backing replica.

queryPaths<F = DefaultFormats>(query?: Omit<Query<[string]>, "formats">, formats?: FormatsArg<F>): Path[]

Returns an array of all unique paths of documents returned by a given query.

set<F = DefaultFormat>(
keypair: AuthorKeypair,
docToSet: Omit<FormatInputType<F>, "format">,
format?: FormatArg<F>,
): Promise<IngestEvent<FormatDocType<F>> | ValidationError>

Add a new document directly to the backing replica.

wipeDocAtPath<F = DefaultFormat>(
keypair: AuthorKeypair,
path: string,
format?: FormatArg<F>,
): Promise<IngestEvent<FormatDocType<F>> | ValidationError>