-
Notifications
You must be signed in to change notification settings - Fork 0
Feature: caching #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stritti
wants to merge
7
commits into
main
Choose a base branch
from
feature/caching
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature: caching #178
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
59d4d10
feat: add DataCacheService and cache reads/FTS in SurrealdbService
stritti de1f7ad
fix: key generation
stritti 5816559
feat: add IndexedDB persistence DataCacheService and robust id handling
stritti 3b0285e
fix: return value from cache entry in DataCacheService.get
stritti 725293d
Merge branch 'main' into feature/caching
stritti be1f716
Merge branch 'main' into feature/caching
stritti 6321e59
refactor: only in mem cache, no indexedDB any more
stritti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { Injectable } from '@angular/core' | ||
|
|
||
| type CacheEntry<T> = { | ||
| value: T | ||
| expiresAt: number | ||
| } | ||
|
|
||
| @Injectable({ providedIn: 'root' }) | ||
| export class DataCacheService { | ||
| /** | ||
| * Lightweight in-memory TTL cache used by {@link SurrealdbService}. | ||
| * | ||
| * Warum existiert dieser Service? | ||
| * - Der SurrealDB-Service braucht ein kleines Cache-Overlay, um doppelte SELECT/QUERY-Requests | ||
| * im öffentlichen Bereich zu reduzieren (Kategorie-/Detailseiten, Volltextsuche). | ||
| * - Für Admin-/Schreiboperationen wird der Cache entweder komplett invalidiert (Login, Mutationen) | ||
| * oder bewusst umgangen (SurrealdbService prüft die aktuelle Route). Dadurch sehen Admins immer | ||
| * Live-Daten. | ||
| * - Der Cache bleibt bewusst in einem eigenen Service, damit: | ||
| * 1. die Logik testbar und wiederverwendbar bleibt, | ||
| * 2. keine Angular-spezifischen Abhängigkeiten (Router, environment) in diesen Store wandern, | ||
| * 3. wir bei Bedarf eine andere Persistenzstrategie hinterlegen könnten, ohne den Surreal-Service | ||
| * anzupassen. | ||
| * | ||
| * Architektur: | ||
| * - `store`: Map-Key → { value, expiresAt } für schnelle TTL-Lookups. | ||
| * - `inFlight`: Promise-Dedupe, damit parallele `getOrFetch`-Aufrufe dieselbe Anfrage teilen. | ||
| * - Keine Persistenz (IndexedDB/localStorage), damit Logout/Sitzungswechsel garantiert frische Daten | ||
| * liefert und der Service SSR-/Node-Tests nicht blockiert. | ||
| */ | ||
| private readonly store = new Map<string, CacheEntry<unknown>>() | ||
| private readonly inFlight = new Map<string, Promise<unknown>>() | ||
|
|
||
| /** | ||
| * Returns the cached value if present and still valid. | ||
| * Expired entries are evicted eagerly. | ||
| */ | ||
| get<T>(key: string): T | undefined { | ||
| const entry = this.store.get(key) as CacheEntry<T> | undefined | ||
| if (!entry) return undefined | ||
| if (entry.expiresAt <= Date.now()) { | ||
| this.store.delete(key) | ||
| return undefined | ||
| } | ||
| return entry.value | ||
| } | ||
|
|
||
| set<T>(key: string, value: T, ttlMs: number): void { | ||
| if (value === undefined || value === null) { | ||
| this.store.delete(key) | ||
| return | ||
| } | ||
| const expiresAt = Date.now() + ttlMs | ||
| this.store.set(key, { value, expiresAt }) | ||
| } | ||
|
|
||
| /** | ||
| * Returns cached data or executes the provided fetcher while deduplicating concurrent calls. | ||
| * The result is stored for the specified TTL unless the fetcher resolves to null/undefined. | ||
| */ | ||
| async getOrFetch<T>(key: string, fetcher: () => Promise<T>, ttlMs: number): Promise<T> { | ||
| const cached = this.get<T>(key) | ||
| if (cached !== undefined) { | ||
| return cached | ||
| } | ||
|
|
||
| if (this.inFlight.has(key)) { | ||
| return (this.inFlight.get(key) as Promise<T>)! | ||
| } | ||
|
|
||
| const request = fetcher() | ||
| .then((result) => { | ||
| this.set(key, result, ttlMs) | ||
| return result | ||
| }) | ||
| .finally(() => { | ||
| this.inFlight.delete(key) | ||
| }) | ||
|
|
||
| this.inFlight.set(key, request as unknown as Promise<unknown>) | ||
| return request | ||
| } | ||
|
|
||
| /** | ||
| * Alias that keeps older call sites readable until they are updated to `getOrFetch`. | ||
| */ | ||
| async remember<T>(key: string, ttlMs: number, fetcher: () => Promise<T>): Promise<T> { | ||
| return await this.getOrFetch(key, fetcher, ttlMs) | ||
| } | ||
|
|
||
| /** Removes a single cache entry plus any inflight request for the same key. */ | ||
| invalidate(key: string): void { | ||
| this.store.delete(key) | ||
| this.inFlight.delete(key) | ||
| } | ||
|
|
||
| /** Bulk invalidation helper used when tables or generic queries change. */ | ||
| invalidatePrefix(prefix: string): void { | ||
| for (const key of this.store.keys()) { | ||
| if (key.startsWith(prefix)) { | ||
| this.store.delete(key) | ||
| } | ||
| } | ||
| for (const key of this.inFlight.keys()) { | ||
| if (key.startsWith(prefix)) { | ||
| this.inFlight.delete(key) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Clears the entire cache – typically used during logout. */ | ||
| clear(): void { | ||
| this.store.clear() | ||
| this.inFlight.clear() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.