@@ -149,31 +149,39 @@ Automatically detects image type from HTTP `Content-Type` header:
149149
150150### 2. Database Caching
151151
152- Intelligent daily caching for app data to minimize API calls and improve performance.
152+ Intelligent caching system for app data with configurable expiration times to minimize API calls and improve performance.
153153
154154#### Architecture
155155- ** Database** : SQLite (` ~/.local/share/com.gatorand.klia-store/kliastore.db ` )
156156- ** Tables** :
157- - ` destacados ` : Stores app of the day data
158- - ` apps_of_the_week ` : Stores weekly featured apps
157+ - ` destacados ` : Stores app of the day data (daily cache)
158+ - ` apps_of_the_week ` : Stores weekly featured apps (daily cache)
159+ - ` categories ` : Stores Flathub categories (weekly cache - 7 days)
159160 - ` cache_metadata ` : Tracks last update dates per section
160161
161162#### Cache Strategy
162- The system uses date-based cache invalidation:
163- 1 . ** On App Launch** : Check if cached data is from current day
164- 2 . ** Same Day** : Load from SQLite database (no API calls)
165- 3 . ** New Day** : Fetch from API and update database
163+ The system uses date-based cache invalidation with configurable durations:
164+ 1 . ** On App Launch** : Check if cached data is older than specified duration
165+ 2 . ** Valid Cache** : Load from SQLite database (no API calls)
166+ 3 . ** Expired Cache** : Fetch from API and update database
167+
168+ ** Cache Durations** :
169+ - App of the Day: Daily (0 days = current day only)
170+ - Apps of the Week: Daily (0 days = current day only)
171+ - Categories: Weekly (7 days)
166172
167173#### Components
168174
169175** Database Cache Manager** (` src/utils/dbCache.ts ` ):
170176- ` DBCacheManager ` : Singleton managing all DB cache operations
171177- Key methods:
172- - ` shouldUpdateSection(sectionName) ` : Returns true if data is from a previous day
178+ - ` shouldUpdateSection(sectionName, maxDaysOld ) ` : Returns true if data is older than maxDaysOld days
173179 - ` getCachedAppOfTheDay() ` : Retrieves cached app of the day
174180 - ` cacheAppOfTheDay(app) ` : Stores app of the day with current date
175181 - ` getCachedAppsOfTheWeek() ` : Retrieves cached weekly apps
176182 - ` cacheAppsOfTheWeek(apps) ` : Stores weekly apps with current date
183+ - ` getCachedCategories() ` : Retrieves cached categories
184+ - ` cacheCategories(categories) ` : Stores categories with current date
177185 - ` updateSectionDate(sectionName) ` : Updates last_update_date in cache_metadata
178186
179187** Database Schema** :
@@ -205,11 +213,19 @@ CREATE TABLE cache_metadata (
205213 last_update_date TEXT NOT NULL , -- YYYY-MM-DD format
206214 updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
207215);
216+
217+ -- Categories (weekly cache)
218+ CREATE TABLE categories (
219+ category_name TEXT PRIMARY KEY ,
220+ cached_at DATETIME DEFAULT CURRENT_TIMESTAMP
221+ );
208222```
209223
210- #### Cache Flow Example (App of the Day)
224+ #### Cache Flow Examples
225+
226+ ** App of the Day (Daily Cache)** :
2112271 . ** User opens app**
212- 2 . ` useAppOfTheDay ` hook calls ` dbCacheManager.shouldUpdateSection("appOfTheDay") `
228+ 2 . ` useAppOfTheDay ` hook calls ` dbCacheManager.shouldUpdateSection("appOfTheDay", 0 ) `
2132293 . Check ` cache_metadata ` for last_update_date
2142304 . ** If same day** :
215231 - Load from ` destacados ` table
@@ -220,11 +236,25 @@ CREATE TABLE cache_metadata (
220236 - Update ` cache_metadata ` with current date
221237 - Return fresh data
222238
239+ ** Categories (Weekly Cache)** :
240+ 1 . ** User opens app**
241+ 2 . ` useCategories ` hook calls ` dbCacheManager.shouldUpdateSection("categories", 7) `
242+ 3 . Check ` cache_metadata ` for last_update_date
243+ 4 . ** If less than 7 days old** :
244+ - Load from ` categories ` table
245+ - Return cached data (no API call)
246+ 5 . ** If 7+ days old or no cache** :
247+ - Call Flathub API
248+ - Store in ` categories ` table
249+ - Update ` cache_metadata ` with current date
250+ - Return fresh data
251+
223252#### Benefits
224- - ** Reduced API Calls** : Only 1 API call per day per section ( instead of every app launch)
253+ - ** Reduced API Calls** : Only 1 API call per configured period (daily/weekly) instead of every app launch
225254- ** Faster Load Times** : SQLite queries are instant vs network requests
226255- ** Offline Support** : Can show cached data even without internet
227256- ** Bandwidth Savings** : Minimal data transfer for daily usage
257+ - ** Flexible Expiration** : Different cache durations for different data types
228258
229259## Key Implementation Details
230260
@@ -281,6 +311,11 @@ CREATE TABLE cache_metadata (
281311
282312## Development Guidelines
283313
314+ ### Code Style
315+ - ** IMPORTANT** : All code comments must be written in English
316+ - Use clear, descriptive variable and function names
317+ - Follow TypeScript best practices and type safety
318+
284319### Adding New API Endpoints
2853201 . Add TypeScript types in ` src/types/index.ts `
2863212 . Create service method in ` src/services/api.ts `
0 commit comments