Skip to main content
Module

x/earthstar/mod.ts>ReplicaCache

A specification and Javascript library for building online tools you can truly call your own.
Go to Latest
class ReplicaCache
import { ReplicaCache } from "https://deno.land/x/earthstar@v9.3.3/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: IReplica,
timeToLive?: number,
onCacheUpdatedWrapper?: (cb: () => void) => void,
)

Create a new ReplicaCache.

Properties

_docCache: Map<string, CacheEntry>
_isClosed: boolean
_onCacheUpdatedCallbacks: Set<(entry: string) => void>
_onFireCacheUpdatedsWrapper: (cb: () => void) => unknown
_replica: IReplica
_timeToLive: number
version: number

Methods

_fireOnCacheUpdateds(entry: string)
_updateCache(key: string, doc: Doc): 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(path: Path): Doc | 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: (entryKey: string) => 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.

queryDocs(query?: Query): Doc[]

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

set(keypair: AuthorKeypair, docToSet: DocToSet)

Add a new document directly to the backing replica.