Skip to content

Migrate cache service from localStorage to IndexedDB#84

Merged
IvanildoBarauna merged 1 commit intomainfrom
claude/indexeddb-cache-migration-ZPAQH
Mar 3, 2026
Merged

Migrate cache service from localStorage to IndexedDB#84
IvanildoBarauna merged 1 commit intomainfrom
claude/indexeddb-cache-migration-ZPAQH

Conversation

@IvanildoBarauna
Copy link
Member

Summary

Migrated the BrowserCache service from localStorage-based implementation to IndexedDB, providing significantly larger storage capacity (GB vs ~5MB) while maintaining the same API surface and type safety.

Key Changes

Core Implementation

  • Storage Backend: Replaced localStorage with IndexedDB for the cache service

    • Implemented openDB() helper to manage IndexedDB connections with proper initialization
    • Created object store schema with key as keyPath for efficient lookups
    • All cache operations now return Promises to handle async IndexedDB API
  • API Changes: Made all BrowserCache methods async

    • get<T>(key)async get<T>(key): Promise<T | null>
    • set<T>(key, data, ttl)async set<T>(key, data, ttl): Promise<void>
    • remove(key)async remove(key): Promise<void>
    • clearAll()async clearAll(): Promise<void>
    • clearExpired()async clearExpired(): Promise<void>
    • getStats()async getStats(): Promise<CacheStats>
    • clearPortfolioCache()async clearPortfolioCache(resourceType?): Promise<void>
  • Data Structure: Updated cache entry format for IndexedDB

    • Entries now include key field (required for keyPath)
    • Maintained data, timestamp, and expiresAt fields
    • Removed JSON serialization overhead (IndexedDB handles structured cloning)

Hook Updates

Updated all data-fetching hooks to await cache operations:

  • useExperience.ts
  • useEducation.ts
  • useProjects.ts
  • useSocialLinks.ts

Testing Infrastructure

  • Test Setup: Added fake-indexeddb polyfill for Node.js test environment
  • Test Helpers: Created resetDB() and insertRawEntry() utilities for test isolation
  • Test Updates:
    • Converted all test cases to async/await pattern
    • Updated assertions to work with IndexedDB cursor-based operations
    • Removed localStorage-specific test assertions
    • Enhanced error handling tests for IndexedDB-specific scenarios

Removed Features

  • Removed QuotaExceededError handling logic (IndexedDB has much larger quota)
  • Removed JSON parse error handling (IndexedDB uses structured cloning)

Notable Implementation Details

  • Proper database connection lifecycle management with db.close() in all code paths
  • Cursor-based iteration for clearAll(), clearExpired(), and getStats() operations
  • Fire-and-forget async cleanup on expired cache reads (doesn't block the return)
  • Graceful error handling maintains backward compatibility (methods never throw)
  • SSR compatibility preserved through isBrowser() check for IndexedDB availability

https://claude.ai/code/session_01CWajAL77hbhWSCYir3UKMS

Replace the BrowserCache localStorage implementation with IndexedDB to reduce
backend requests. IndexedDB offers much larger storage quota (GB vs ~5MB) and
persists across browser sessions just like localStorage, but with better
capacity for caching API responses long-term.

Changes:
- Rewrite cacheService.ts using IndexedDB with async methods (Promise-based)
- All BrowserCache methods are now async: get, set, remove, clearAll,
  clearExpired, getStats - same class name and exports preserved
- clearPortfolioCache() is now async to ensure cache is cleared before logging
- Update all 4 data-fetching hooks to await async cache calls
- Add fake-indexeddb dev dependency for test environment (jsdom lacks IndexedDB)
- Rewrite unit and integration tests: async/await, IndexedDB reset between
  tests, shared insertRawEntry helper for edge-case scenarios
- Retain 30-day TTL, portfolio_cache_ key namespacing, and cache invalidation
  endpoint compatibility (window.clearPortfolioCache still works)

All 65 tests pass (49 cache tests + 16 other tests).

Co-authored-by: Ivanildo Barauna de Souza Junior <ivanildo.jnr@outlook.com>
https://claude.ai/code/session_01CWajAL77hbhWSCYir3UKMS
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@IvanildoBarauna IvanildoBarauna merged commit e1e08dd into main Mar 3, 2026
5 checks passed
@IvanildoBarauna IvanildoBarauna deleted the claude/indexeddb-cache-migration-ZPAQH branch March 3, 2026 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants