Cache

IndexedDBCacheStrategy

Strategia di cache default basata su IndexedDB. Capacita elevata (~50MB+), persistente, async non-blocking. Ideale per produzione e dataset grandi.

Cosa fa

IndexedDBCacheStrategy e la strategia di cache default di @pzeta/postgrest. Persiste le entry in un object store IndexedDB, garantendo:

  • Capacita elevata — ~50MB+ (dipende dal browser e dallo spazio disco disponibile)
  • Persistenza permanente — sopravvive a refresh, chiusura tab e riavvio browser
  • Async non-blocking — non blocca il main thread (a differenza di localStorage/sessionStorage)
  • Indici secondari su timestamp ed expiry per cleanup efficiente

Internamente usa IndexedDBStore come wrapper low-level.

Quando usarla

  • Default per produzione — sempre preferibile quando IndexedDB e disponibile
  • PWA e app offline-first con cache persistente cross-sessione
  • Dataset grandi — listing, anagrafica, cataloghi
  • Tab multipli — IndexedDB e condiviso tra tab dello stesso origin
Usare CacheStrategyFactory.createWithFallback() per ottenere IndexedDB con fallback automatico su SessionStorage/LocalStorage/Memory.

Costruttore

class IndexedDBCacheStrategy implements ICacheStrategy {
  constructor(dbName?: string, storeName?: string)
}
ParametroTipoDefaultDescrizione
dbNamestring'pzeta_cache_db'Nome del database IndexedDB
storeNamestring'cache_store'Nome dell'object store all'interno del DB

Esempio

import { IndexedDBCacheStrategy, CacheService } from '@pzeta/postgrest'

// Default
const strategy = new IndexedDBCacheStrategy()

// Nomi custom (utile per isolare cache per area/MFE)
const strategyCrm = new IndexedDBCacheStrategy('crm_cache_db', 'orders_cache')

const cache = CacheService.getInstance(httpClient, strategy)

Comportamento

MetodoNote
setItem(key, value, ttl?)addOrUpdate su entry { key, data, timestamp, expiry }
getItem(key)Ritorna null se assente o scaduto. Auto-elimina entry scadute al primo accesso
removeItem(key)store.delete(key)
clear(keyPrefix?)Senza prefix: cancella tutto. Con prefix: cancella entry con chiave che inizia con keyPrefix
cleanExpired()Itera getAll(), rimuove entry con expiry < now. Ritorna il count
getStats()Calcola size (somma JSON.stringify(entry).length), keys, expired
supportsTTL()true
isPersistent()true

Schema interno

Object store con keyPath: 'key' e due indici:

  • timestamp — per ordinamento temporale
  • expiry — per cleanup mirato (utile per query future)

Schema entry:

interface CacheEntry<T> {
  key: string
  data: T
  timestamp: number
  expiry: number | null
}

Vedi anche