Skip to content

Commit 5950d1e

Browse files
authored
Merge pull request #82 from agnosticeng/feat/cache
feat: add cache
2 parents dbf9b4d + b145d7d commit 5950d1e

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/lib/icons/Bold.svelte

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script lang="ts">
2+
import type { SvelteHTMLElements } from 'svelte/elements';
3+
4+
interface Props extends Omit<SvelteHTMLElements['svg'], 'width' | 'height'> {
5+
size?: string | number | null;
6+
}
7+
8+
let { size = 24, ...rest }: Props = $props();
9+
</script>
10+
11+
<svg viewBox="0 0 24 24" aria-hidden="true" width={size} height={size} {...rest} data-name="bold">
12+
<path
13+
fill="none"
14+
stroke-width="1.5"
15+
stroke="currentColor"
16+
stroke-linecap="round"
17+
stroke-linejoin="round"
18+
d="m3.75 13.5 10.5-11.25L12 10.5h8.25L9.75 21.75 12 13.5H3.75Z"
19+
/>
20+
</svg>

src/routes/+page.svelte

+37-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import { setAppContext } from '$lib/context';
1515
import { FileDropEventManager } from '$lib/FileDropEventManager';
1616
import Bars3 from '$lib/icons/Bars3.svelte';
17+
import Bold from '$lib/icons/Bold.svelte';
1718
import Copy from '$lib/icons/Copy.svelte';
1819
import MagicWand from '$lib/icons/MagicWand.svelte';
1920
import PanelBottom from '$lib/icons/PanelBottom.svelte';
@@ -27,6 +28,7 @@
2728
import { historyRepository, type HistoryEntry } from '$lib/repositories/history';
2829
import { queryRepository, type Query } from '$lib/repositories/queries';
2930
import { tabRepository, type Tab } from '$lib/repositories/tabs';
31+
import { IndexedDBCache } from '@agnosticeng/cache';
3032
import { SplitPane } from '@rich_harris/svelte-split-pane';
3133
import debounce from 'p-debounce';
3234
import { format } from 'sql-formatter';
@@ -36,14 +38,31 @@
3638
let loading = $state(false);
3739
let counter = $state<ReturnType<typeof TimeCounter>>();
3840
39-
async function handleExec() {
41+
const cache = new IndexedDBCache({ dbName: 'query-cache', storeName: 'response-data' });
42+
let cached = $state(false);
43+
44+
async function handleExec(force = false) {
4045
const query = currentTab.content;
4146
if (loading || !query) return;
4247
4348
loading = true;
4449
counter?.start();
4550
try {
51+
if (!force) {
52+
const r = await cache.get(query);
53+
if (r) {
54+
cached = true;
55+
response = r;
56+
cached = true;
57+
bottomPanel.open = true;
58+
if (bottomPanelTab === 'logs') bottomPanelTab = 'data';
59+
return;
60+
}
61+
}
62+
4663
response = await engine.exec(query);
64+
cached = false;
65+
await cache.set(query, response);
4766
} finally {
4867
loading = false;
4968
counter?.stop();
@@ -390,9 +409,22 @@ LIMIT 100;`;
390409
<button class="action" title="Save" onclick={handleSaveQuery} disabled={!canSave}>
391410
<Save size="12" />
392411
</button>
393-
<button class="action" title="Run" onclick={handleExec} disabled={loading}>
412+
<button
413+
class="action"
414+
title="Run"
415+
onclick={() => handleExec()}
416+
disabled={loading}
417+
>
394418
<Play size="12" />
395419
</button>
420+
<button
421+
class="action"
422+
title="Force run"
423+
onclick={() => handleExec(true)}
424+
disabled={loading}
425+
>
426+
<Bold size="12" />
427+
</button>
396428
</div>
397429
</nav>
398430
{#each tabs as tab, i (tab.id)}
@@ -424,6 +456,9 @@ LIMIT 100;`;
424456
<PanelLeft size="12" />
425457
</button>
426458
<div class="spacer"></div>
459+
{#if cached}
460+
<span>from cache</span>
461+
{/if}
427462
<TimeCounter bind:this={counter} />
428463
{#if BUILD}
429464
<span class="label">build-{BUILD}</span>

0 commit comments

Comments
 (0)