4.8 KiB
NDK Mobile Cache Integration Plan
This document outlines our plan to leverage the NDK mobile SQLite cache system throughout the POWR app to improve offline functionality, reduce network usage, and enhance performance.
Overview
The NDK mobile library provides a robust SQLite-based caching system that includes:
- Profile Caching: Stores user profiles with metadata
- Event Caching: Stores Nostr events with efficient indexing
- Unpublished Event Queue: Manages events pending publication
- Web of Trust Storage: Maintains relationship scores
We will integrate this caching system across multiple components of our app to provide a better offline experience.
Implementation Priorities
1. Profile Image Caching
Files to Modify:
components/UserAvatar.tsx
- Create new:
lib/db/services/ProfileImageCache.ts
Functions to Implement:
getProfileImageUri(pubkey, imageUrl)
: Get a cached image URI or download if neededclearOldCache(maxAgeDays)
: Remove old cached images
Integration Points:
- Update
UserAvatar
to use the cache service - Add cache invalidation based on profile updates
2. Publication Queue Service
Files to Modify:
lib/db/services/PublicationQueueService.ts
Functions to Enhance:
queueEvent(event)
: Use NDK's unpublished events systemprocessQueue()
: Process events from NDK cachegetPendingEvents(limit)
: Get events from NDK cachegetPendingCount()
: Get count from NDK cache
Migration Strategy:
- Add NDK cache support
- Dual-write period
- Migrate existing queue
- Remove custom implementation
3. Social Feed Caching
Files to Modify:
lib/social/socialFeedService.ts
lib/hooks/useSocialFeed.ts
Functions to Enhance:
subscribeFeed(options)
: Check cache before subscriptiongetComments(eventId)
: Use cache for commentsresolveQuotedContent(event)
: Use cache for quoted content
Benefits:
- Immediate display of previously viewed content
- Reduced network requests
- Offline browsing of previously viewed feeds
4. Workout History
Files to Modify:
lib/db/services/UnifiedWorkoutHistoryService.ts
Functions to Enhance:
getNostrWorkouts()
: Use NDK cache directlyimportNostrWorkoutToLocal(eventId)
: Leverage cache for importssubscribeToNostrWorkouts(pubkey, callback)
: Use cache for initial data
Benefits:
- Faster workout history loading
- Offline access to workout history
- Reduced network usage
5. Exercise Library
Files to Modify:
lib/db/services/ExerciseService.ts
lib/hooks/useExercises.ts
Functions to Implement:
getExercisesFromNostr()
: Use cache for exercisesgetExerciseDetails(id)
: Get details from cache
Benefits:
- Offline access to exercise library
- Faster exercise loading
6. Workout Templates
Files to Modify:
lib/db/services/TemplateService.ts
lib/hooks/useTemplates.ts
Functions to Enhance:
getTemplateFromNostr(id)
: Use cache for templatesgetTemplatesFromNostr()
: Get templates from cache
Benefits:
- Offline access to templates
- Faster template loading
7. Contact List & Following
Files to Modify:
lib/hooks/useContactList.ts
lib/hooks/useFeedState.ts
Functions to Enhance:
getContactList()
: Use cache for contact listgetFollowingList()
: Use cache for following list
Benefits:
- Offline access to contacts
- Faster contact list loading
8. General Media Cache
Files to Create:
lib/db/services/MediaCacheService.ts
Functions to Implement:
cacheMedia(url, mimeType)
: Download and cache mediagetMediaUri(url)
: Get cached media URIclearOldCache(maxAgeDays)
: Remove old cached media
Integration Points:
- Profile banners
- Exercise images
- Other media content
Implementation Approach
For each component, we will:
- Analyze Current Implementation: Understand how data is currently fetched and stored
- Design Cache Integration: Determine how to leverage NDK cache
- Implement Changes: Modify code to use cache
- Test Offline Functionality: Verify behavior when offline
- Measure Performance: Compare before and after metrics
Technical Considerations
Cache Size Management
- Implement cache size limits
- Add cache eviction policies
- Prioritize frequently accessed data
Cache Invalidation
- Track data freshness
- Implement TTL (Time To Live) for cached data
- Update cache when new data is received
Error Handling
- Graceful fallbacks when cache misses
- Recovery from cache corruption
- Logging for debugging
Success Metrics
- Reduced network requests
- Faster app startup time
- Improved offline experience
- Reduced data usage
- Better battery life
Next Steps
- Begin with Profile Image Cache implementation
- Move to Publication Queue Service
- Continue with remaining components in priority order