mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-21 22:30:41 +08:00
Cache repo summaries for the sidebar
This commit is contained in:
@@ -46,7 +46,12 @@
|
||||
|
||||
import { nodeRunning } from "@app/lib/events";
|
||||
import { dynamicInterval, resetDynamicInterval } from "@app/lib/interval";
|
||||
import { invoke, writeToClipboard } from "@app/lib/invoke";
|
||||
import {
|
||||
cachedListReposSummary,
|
||||
invalidateReposSummary,
|
||||
invoke,
|
||||
writeToClipboard,
|
||||
} from "@app/lib/invoke";
|
||||
import * as router from "@app/lib/router";
|
||||
import { explorerUrl, formatRepositoryId } from "@app/lib/utils";
|
||||
|
||||
@@ -180,8 +185,9 @@
|
||||
});
|
||||
|
||||
async function reloadRepos() {
|
||||
invalidateReposSummary();
|
||||
[repos, seededNotReplicated] = await Promise.all([
|
||||
invoke<RepoSummary[]>("list_repos_summary"),
|
||||
cachedListReposSummary(),
|
||||
invoke<string[]>("seeded_not_replicated"),
|
||||
]);
|
||||
}
|
||||
|
||||
+11
-3
@@ -1,12 +1,18 @@
|
||||
import { LRUCache } from "lru-cache";
|
||||
|
||||
export interface Cached<Args extends unknown[], V> {
|
||||
(...args: Args): Promise<V>;
|
||||
/** Drop all cached entries, forcing the next call to re-fetch. */
|
||||
clear(): void;
|
||||
}
|
||||
|
||||
export function cached<Args extends unknown[], V>(
|
||||
f: (...args: Args) => Promise<V>,
|
||||
makeKey: (...args: Args) => string,
|
||||
options?: LRUCache.Options<string, { promise: Promise<V> }, unknown>,
|
||||
): (...args: Args) => Promise<V> {
|
||||
): Cached<Args, V> {
|
||||
const cache = new LRUCache(options || { max: 500 });
|
||||
return function (...args: Args): Promise<V> {
|
||||
const fn = function (...args: Args): Promise<V> {
|
||||
const key = makeKey(...args);
|
||||
const hit = cache.get(key);
|
||||
if (hit !== undefined) return hit.promise;
|
||||
@@ -18,5 +24,7 @@ export function cached<Args extends unknown[], V>(
|
||||
});
|
||||
cache.set(key, { promise });
|
||||
return promise;
|
||||
};
|
||||
} as Cached<Args, V>;
|
||||
fn.clear = () => cache.clear();
|
||||
return fn;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { DiffOptions } from "@bindings/cob/DiffOptions";
|
||||
import type { Diff } from "@bindings/diff/Diff";
|
||||
import type { Stats } from "@bindings/diff/Stats";
|
||||
import type { Commit } from "@bindings/repo/Commit";
|
||||
import type { RepoSummary } from "@bindings/repo/RepoSummary";
|
||||
|
||||
import * as tauri from "@tauri-apps/api/core";
|
||||
|
||||
@@ -154,6 +155,22 @@ export const cachedGetCommitDiff = cached(
|
||||
{ max: 100 },
|
||||
);
|
||||
|
||||
/**
|
||||
* Enumerating repos loads and verifies the identity doc of every repo in
|
||||
* storage, which takes ~100ms with many repos. The list rarely changes, so it
|
||||
* is cached with a short TTL; mutations that change it (adding, seeding,
|
||||
* unseeding) go through invalidateReposSummary().
|
||||
*/
|
||||
export const cachedListReposSummary = cached(
|
||||
() => invoke<RepoSummary[]>("list_repos_summary"),
|
||||
() => "list_repos_summary",
|
||||
{ max: 1, ttl: 30_000 },
|
||||
);
|
||||
|
||||
export function invalidateReposSummary(): void {
|
||||
cachedListReposSummary.clear();
|
||||
}
|
||||
|
||||
export async function writeToClipboard(
|
||||
text: string,
|
||||
opts?: {
|
||||
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
loadRepoHome,
|
||||
} from "@app/views/repo/router";
|
||||
|
||||
import { invoke } from "@app/lib/invoke";
|
||||
import { cachedListReposSummary, invoke } from "@app/lib/invoke";
|
||||
|
||||
interface BootingRoute {
|
||||
resource: "booting";
|
||||
@@ -72,7 +72,7 @@ export async function loadSidebarData(): Promise<SidebarData> {
|
||||
const [config, repos, notificationCount, seededNotReplicated] =
|
||||
await Promise.all([
|
||||
invoke<Config>("config"),
|
||||
invoke<RepoSummary[]>("list_repos_summary"),
|
||||
cachedListReposSummary(),
|
||||
invoke<number>("notification_count"),
|
||||
invoke<string[]>("seeded_not_replicated"),
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user