Skip to main content
Module

x/earthstar/mod.ts>ReplicaCache

Storage for private, distributed, offline-first applications.
Latest
class ReplicaCache
import { ReplicaCache } from "https://deno.land/x/earthstar@v10.2.2/mod.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

private
onReplicaEvent(event: ExpireEvent<DocBase<string>> | IngestEventSuccess<DocBase<string>> | AttachmentIngestEvent<DocBase<string>>)
_updateCache(key: string, doc: DocBase<string>): void
addAttachments<F = DefaultFormats>(docs: FormatDocType<F>[], formats?: FormatsArg<F>): DocWithAttachment<FormatDocType<F>>[]
getAllDocs<F = DefaultFormats>(formats?: FormatsArg<F>): FormatDocType<F>[]

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

getAllDocsAtPath<F = DefaultFormats>(path: Path, formats?: FormatsArg<F>): FormatDocType<F>[]

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.

getAttachment<F = DefaultFormat>(doc: FormatDocType<F>, format?: FormatArg<F>): DocAttachment | undefined | ValidationError
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.

getLatestDocs<F = DefaultFormats>(formats?: FormatsArg<F>): FormatDocType<F>[]

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.

overwriteAllDocsByAuthor<F = DefaultFormat>(keypair: AuthorKeypair, format?: FormatArg<F>)

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>