mirror of
https://github.com/moku-project/Moku.git
synced 2026-06-13 09:19:56 -05:00
Basic caching
This commit is contained in:
Vendored
+1
@@ -147,6 +147,7 @@ export const CACHE_GROUPS = {
|
|||||||
|
|
||||||
export const CACHE_KEYS = {
|
export const CACHE_KEYS = {
|
||||||
LIBRARY: "library",
|
LIBRARY: "library",
|
||||||
|
RECENT_UPDATES: "recent_updates",
|
||||||
ALL_MANGA: "all_manga_unfiltered",
|
ALL_MANGA: "all_manga_unfiltered",
|
||||||
CATEGORIES: "categories",
|
CATEGORIES: "categories",
|
||||||
SEARCH: "search_all_manga",
|
SEARCH: "search_all_manga",
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
import { BookOpen, CircleNotch } from "phosphor-svelte";
|
import { BookOpen, CircleNotch } from "phosphor-svelte";
|
||||||
import { gql } from "@api/client";
|
import { gql } from "@api/client";
|
||||||
import { GET_RECENTLY_UPDATED, GET_CHAPTERS } from "@api/queries";
|
import { GET_RECENTLY_UPDATED, GET_CHAPTERS } from "@api/queries";
|
||||||
|
import { cache, CACHE_GROUPS, CACHE_KEYS } from "@core/cache";
|
||||||
import { store, openReader, setActiveManga, addToast } from "@store/state.svelte";
|
import { store, openReader, setActiveManga, addToast } from "@store/state.svelte";
|
||||||
import { dayLabel } from "@core/util";
|
import { dayLabel } from "@core/util";
|
||||||
import { buildReaderChapterList } from "@features/series/lib/chapterList";
|
import { buildReaderChapterList } from "@features/series/lib/chapterList";
|
||||||
@@ -30,9 +31,10 @@
|
|||||||
let openingId = $state<number | null>(null);
|
let openingId = $state<number | null>(null);
|
||||||
|
|
||||||
let ctrl: AbortController | null = null;
|
let ctrl: AbortController | null = null;
|
||||||
|
const RECENT_UPDATES_TTL_MS = 60 * 1_000;
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
onRegisterRefresh?.(loadUpdates);
|
onRegisterRefresh?.(() => loadUpdates(true));
|
||||||
void loadUpdates();
|
void loadUpdates();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -46,13 +48,13 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
const groups = $derived.by(() => {
|
const groups = $derived.by(() => {
|
||||||
const map = new Map<string, RecentUpdate[]>();
|
const grouped: Record<string, RecentUpdate[]> = {};
|
||||||
for (const item of updates) {
|
for (const item of updates) {
|
||||||
const label = dayLabel(fetchedAtMs(item));
|
const label = dayLabel(fetchedAtMs(item));
|
||||||
if (!map.has(label)) map.set(label, []);
|
if (!grouped[label]) grouped[label] = [];
|
||||||
map.get(label)!.push(item);
|
grouped[label].push(item);
|
||||||
}
|
}
|
||||||
return Array.from(map.entries()).map(([label, items]) => ({ label, items })) as UpdateGroup[];
|
return Object.entries(grouped).map(([label, items]) => ({ label, items })) as UpdateGroup[];
|
||||||
});
|
});
|
||||||
|
|
||||||
const lastCheckedTs = $derived(
|
const lastCheckedTs = $derived(
|
||||||
@@ -86,7 +88,7 @@
|
|||||||
return "Chapter";
|
return "Chapter";
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadUpdates() {
|
async function loadUpdates(force = false) {
|
||||||
ctrl?.abort();
|
ctrl?.abort();
|
||||||
const nextCtrl = new AbortController();
|
const nextCtrl = new AbortController();
|
||||||
ctrl = nextCtrl;
|
ctrl = nextCtrl;
|
||||||
@@ -94,7 +96,15 @@
|
|||||||
error = null;
|
error = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await gql<{ chapters: { nodes: RecentUpdate[] } }>(GET_RECENTLY_UPDATED, {}, nextCtrl.signal);
|
const key = CACHE_KEYS.RECENT_UPDATES;
|
||||||
|
if (force) cache.clear(key);
|
||||||
|
|
||||||
|
const res = await cache.get<{ chapters: { nodes: RecentUpdate[] } }>(
|
||||||
|
key,
|
||||||
|
() => gql<{ chapters: { nodes: RecentUpdate[] } }>(GET_RECENTLY_UPDATED, {}, nextCtrl.signal),
|
||||||
|
RECENT_UPDATES_TTL_MS,
|
||||||
|
CACHE_GROUPS.LIBRARY,
|
||||||
|
);
|
||||||
if (nextCtrl.signal.aborted) return;
|
if (nextCtrl.signal.aborted) return;
|
||||||
|
|
||||||
updates = res.chapters.nodes
|
updates = res.chapters.nodes
|
||||||
|
|||||||
Reference in New Issue
Block a user