@umituz/react-native-ai-gemini-provider 1.14.28 → 1.14.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/domain/README.md +232 -0
- package/src/domain/constants/README.md +191 -0
- package/src/domain/entities/README.md +238 -0
- package/src/infrastructure/README.md +252 -0
- package/src/infrastructure/cache/CACHE_SYSTEM.md +213 -0
- package/src/infrastructure/cache/README.md +213 -0
- package/src/infrastructure/content/CONTENT_BUILDER.md +175 -0
- package/src/infrastructure/content/README.md +175 -0
- package/src/infrastructure/interceptors/README.md +226 -0
- package/src/infrastructure/interceptors/REQUEST_INTERCEPTORS.md +171 -0
- package/src/infrastructure/job/JOB_MANAGER.md +174 -0
- package/src/infrastructure/job/README.md +194 -0
- package/src/infrastructure/response/README.md +187 -0
- package/src/infrastructure/response/RESPONSE_FORMATTER.md +185 -0
- package/src/infrastructure/services/CORE_CLIENT_SERVICE.md +202 -0
- package/src/infrastructure/services/FEATURE_MODEL_SELECTOR_SERVICE.md +206 -0
- package/src/infrastructure/services/GENERATION_EXECUTOR_SERVICE.md +176 -0
- package/src/infrastructure/services/IMAGE_EDIT_SERVICE.md +169 -0
- package/src/infrastructure/services/IMAGE_GENERATION_SERVICE.md +166 -0
- package/src/infrastructure/services/JOB_PROCESSOR_SERVICE.md +174 -0
- package/src/infrastructure/services/PROVIDER_INITIALIZER_SERVICE.md +176 -0
- package/src/infrastructure/services/README.md +233 -0
- package/src/infrastructure/services/RETRY_SERVICE.md +178 -0
- package/src/infrastructure/services/STREAMING_SERVICE.md +166 -0
- package/src/infrastructure/services/STRUCTURED_TEXT_SERVICE.md +175 -0
- package/src/infrastructure/services/TEXT_GENERATION_SERVICE.md +160 -0
- package/src/infrastructure/services/VEO_HTTP_CLIENT_SERVICE.md +179 -0
- package/src/infrastructure/services/VEO_POLLING_SERVICE.md +173 -0
- package/src/infrastructure/services/VIDEO_DOWNLOADER_SERVICE.md +166 -0
- package/src/infrastructure/services/VIDEO_ERROR_HANDLER_SERVICE.md +185 -0
- package/src/infrastructure/services/VIDEO_GENERATION_SERVICE.md +176 -0
- package/src/infrastructure/services/VIDEO_URL_EXTRACTOR_SERVICE.md +186 -0
- package/src/infrastructure/telemetry/README.md +203 -0
- package/src/infrastructure/telemetry/TELEMETRY_SYSTEM.md +200 -0
- package/src/infrastructure/utils/DATA_TRANSFORMER_UTILS.md +175 -0
- package/src/infrastructure/utils/ERROR_MAPPER.md +170 -0
- package/src/infrastructure/utils/ERROR_UTILITIES.md +208 -0
- package/src/infrastructure/utils/IMAGE_PREPARER_UTILS.md +185 -0
- package/src/infrastructure/utils/INPUT_BUILDERS.md +214 -0
- package/src/infrastructure/utils/MODEL_VALIDATION_UTILS.md +189 -0
- package/src/infrastructure/utils/PERFORMANCE_UTILITIES.md +477 -0
- package/src/infrastructure/utils/PERFORMANCE_UTILS.md +219 -0
- package/src/infrastructure/utils/README.md +289 -0
- package/src/presentation/README.md +187 -0
- package/src/presentation/hooks/README.md +188 -0
- package/src/presentation/hooks/USE_GEMINI_HOOK.md +226 -0
- package/src/providers/README.md +247 -0
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
# Infrastructure Layer
|
|
2
|
+
|
|
3
|
+
Performance optimization, monitoring, and extensibility features. Includes cache, telemetry, and interceptor mechanisms.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import {
|
|
9
|
+
SimpleCache,
|
|
10
|
+
modelSelectionCache,
|
|
11
|
+
telemetryHooks,
|
|
12
|
+
requestInterceptors,
|
|
13
|
+
responseInterceptors
|
|
14
|
+
} from '@umituz/react-native-ai-gemini-provider';
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 🎯 Purpose
|
|
18
|
+
|
|
19
|
+
Use infrastructure features to optimize performance, monitor operations, and extend functionality. Provides caching, telemetry, and middleware capabilities.
|
|
20
|
+
|
|
21
|
+
**When to use:**
|
|
22
|
+
- Cache expensive operations
|
|
23
|
+
- Monitor AI operations
|
|
24
|
+
- Intercept requests/responses
|
|
25
|
+
- Optimize performance
|
|
26
|
+
- Track metrics
|
|
27
|
+
- Extend functionality
|
|
28
|
+
|
|
29
|
+
## 📌 Strategy
|
|
30
|
+
|
|
31
|
+
Infrastructure features are cross-cutting concerns. These systems:
|
|
32
|
+
- Improve performance through caching
|
|
33
|
+
- Enable observability through telemetry
|
|
34
|
+
- Provide extensibility through interceptors
|
|
35
|
+
- Support monitoring and debugging
|
|
36
|
+
- Optimize resource usage
|
|
37
|
+
|
|
38
|
+
**Key Decision**: Use infrastructure features to add capabilities without modifying core services. Keep infrastructure separate from business logic.
|
|
39
|
+
|
|
40
|
+
## ⚠️ Rules
|
|
41
|
+
|
|
42
|
+
### Usage Rules
|
|
43
|
+
- **MUST** initialize features before use
|
|
44
|
+
- **SHOULD** clean up resources appropriately
|
|
45
|
+
- **MUST** handle cache misses gracefully
|
|
46
|
+
- **SHOULD** monitor telemetry for insights
|
|
47
|
+
- **MUST NOT** create circular dependencies
|
|
48
|
+
|
|
49
|
+
### Cache Rules
|
|
50
|
+
- **MUST** set appropriate TTL values
|
|
51
|
+
- **SHOULD** monitor cache hit rates
|
|
52
|
+
- **MUST** handle cache size limits
|
|
53
|
+
- **SHOULD** invalidate stale data
|
|
54
|
+
- **MUST NOT** cache everything indiscriminately
|
|
55
|
+
|
|
56
|
+
### Telemetry Rules
|
|
57
|
+
- **SHOULD** subscribe to events early
|
|
58
|
+
- **MUST** unsubscribe when done
|
|
59
|
+
- **SHOULD NOT** emit sensitive data
|
|
60
|
+
- **MUST** handle listener errors
|
|
61
|
+
- **SHOULD** aggregate metrics
|
|
62
|
+
|
|
63
|
+
### Interceptor Rules
|
|
64
|
+
- **MUST** return modified context
|
|
65
|
+
- **SHOULD** handle errors in interceptors
|
|
66
|
+
- **MUST** unsubscribe when done
|
|
67
|
+
- **SHOULD NOT** block execution excessively
|
|
68
|
+
- **MUST** maintain order dependency
|
|
69
|
+
|
|
70
|
+
## 🤖 AI Agent Guidelines
|
|
71
|
+
|
|
72
|
+
### When Modifying Infrastructure
|
|
73
|
+
1. **READ** existing implementations first
|
|
74
|
+
2. **UNDERSTAND** feature dependencies
|
|
75
|
+
3. **MAINTAIN** backward compatibility
|
|
76
|
+
4. **ADD** tests for new functionality
|
|
77
|
+
5. **UPDATE** documentation
|
|
78
|
+
|
|
79
|
+
### When Adding New Features
|
|
80
|
+
1. **CHECK** if similar feature exists
|
|
81
|
+
2. **FOLLOW** existing patterns
|
|
82
|
+
3. **USE** established error handling
|
|
83
|
+
4. **DOCUMENT** in code comments
|
|
84
|
+
5. **ADD** examples to tests (not docs)
|
|
85
|
+
|
|
86
|
+
### When Fixing Bugs
|
|
87
|
+
1. **REPRODUCE** bug locally first
|
|
88
|
+
2. **IDENTIFY** root cause
|
|
89
|
+
3. **FIX** with minimal changes
|
|
90
|
+
4. **ADD** regression test
|
|
91
|
+
5. **VERIFY** all tests pass
|
|
92
|
+
|
|
93
|
+
### Code Style Rules
|
|
94
|
+
- **USE** dependency injection
|
|
95
|
+
- **VALIDATE** inputs
|
|
96
|
+
- **HANDLE** errors gracefully
|
|
97
|
+
- **LOG** important operations
|
|
98
|
+
- **COMMENT** complex logic only
|
|
99
|
+
|
|
100
|
+
## 📦 Available Features
|
|
101
|
+
|
|
102
|
+
### Cache System
|
|
103
|
+
|
|
104
|
+
LRU cache implementation for performance optimization.
|
|
105
|
+
|
|
106
|
+
**Refer to**: [`cache/README.md`](./cache/README.md)
|
|
107
|
+
|
|
108
|
+
**Files:**
|
|
109
|
+
- [`cache/SimpleCache.ts`](./cache/SimpleCache.ts) - Cache implementation
|
|
110
|
+
- [`cache/index.ts`](./cache/index.ts) - Cache exports
|
|
111
|
+
|
|
112
|
+
### Telemetry System
|
|
113
|
+
|
|
114
|
+
Event-based monitoring for AI operations.
|
|
115
|
+
|
|
116
|
+
**Refer to**: [`telemetry/README.md`](./telemetry/README.md)
|
|
117
|
+
|
|
118
|
+
**Files:**
|
|
119
|
+
- [`telemetry/TelemetryHooks.ts`](./telemetry/TelemetryHooks.ts) - Telemetry implementation
|
|
120
|
+
- [`telemetry/index.ts`](./telemetry/index.ts) - Telemetry exports
|
|
121
|
+
|
|
122
|
+
### Interceptors
|
|
123
|
+
|
|
124
|
+
Middleware for request/response transformation.
|
|
125
|
+
|
|
126
|
+
**Refer to**: [`interceptors/README.md`](./interceptors/README.md)
|
|
127
|
+
|
|
128
|
+
**Files:**
|
|
129
|
+
- [`interceptors/RequestInterceptors.ts`](./interceptors/RequestInterceptors.ts) - Request interceptors
|
|
130
|
+
- [`interceptors/ResponseInterceptors.ts`](./interceptors/ResponseInterceptors.ts) - Response interceptors
|
|
131
|
+
- [`interceptors/index.ts`](./interceptors/index.ts) - Interceptor exports
|
|
132
|
+
|
|
133
|
+
## 🔗 Related Modules
|
|
134
|
+
|
|
135
|
+
- **Services**: [`services/README.md`](./services/README.md)
|
|
136
|
+
- **Utils**: [`utils/README.md`](./utils/README.md)
|
|
137
|
+
- **Domain Types**: [`../domain/README.md`](../domain/README.md)
|
|
138
|
+
|
|
139
|
+
## 📋 Feature Reference
|
|
140
|
+
|
|
141
|
+
### SimpleCache
|
|
142
|
+
|
|
143
|
+
LRU cache with TTL support.
|
|
144
|
+
|
|
145
|
+
**Methods:**
|
|
146
|
+
- `set(key, value, ttl?)` - Store value
|
|
147
|
+
- `get(key)` - Retrieve value
|
|
148
|
+
- `has(key)` - Check existence
|
|
149
|
+
- `delete(key)` - Remove entry
|
|
150
|
+
- `clear()` - Clear all entries
|
|
151
|
+
- `getStats()` - Get cache statistics
|
|
152
|
+
|
|
153
|
+
**Configuration:**
|
|
154
|
+
- `maxSize`: Maximum entries (default: 100)
|
|
155
|
+
- `ttl`: Time-to-live in milliseconds (default: 5 minutes)
|
|
156
|
+
|
|
157
|
+
### Telemetry Hooks
|
|
158
|
+
|
|
159
|
+
Event system for monitoring operations.
|
|
160
|
+
|
|
161
|
+
**Events:**
|
|
162
|
+
- `request` - Operation started
|
|
163
|
+
- `response` - Operation completed
|
|
164
|
+
- `error` - Operation failed
|
|
165
|
+
- `retry` - Retry attempted
|
|
166
|
+
|
|
167
|
+
**Methods:**
|
|
168
|
+
- `subscribe(listener)` - Add event listener
|
|
169
|
+
- `emit(event)` - Emit event
|
|
170
|
+
- `logRequest(model, feature)` - Log request start
|
|
171
|
+
- `logResponse(model, startTime, feature)` - Log response
|
|
172
|
+
- `logError(model, error, feature)` - Log error
|
|
173
|
+
- `clear()` - Clear all listeners
|
|
174
|
+
|
|
175
|
+
### Request Interceptors
|
|
176
|
+
|
|
177
|
+
Middleware for request transformation.
|
|
178
|
+
|
|
179
|
+
**Methods:**
|
|
180
|
+
- `use(interceptor)` - Add interceptor
|
|
181
|
+
- Returns unsubscribe function
|
|
182
|
+
|
|
183
|
+
**Execution Order:** First added runs first
|
|
184
|
+
|
|
185
|
+
### Response Interceptors
|
|
186
|
+
|
|
187
|
+
Middleware for response transformation.
|
|
188
|
+
|
|
189
|
+
**Methods:**
|
|
190
|
+
- `use(interceptor)` - Add interceptor
|
|
191
|
+
- Returns unsubscribe function
|
|
192
|
+
|
|
193
|
+
**Execution Order:** Last added runs first (reverse order)
|
|
194
|
+
|
|
195
|
+
## 🎓 Usage Patterns
|
|
196
|
+
|
|
197
|
+
### Caching Strategy
|
|
198
|
+
1. Import `SimpleCache` or use global cache
|
|
199
|
+
2. Configure cache size and TTL
|
|
200
|
+
3. Check cache before operation
|
|
201
|
+
4. Store result in cache after operation
|
|
202
|
+
5. Handle cache hits and misses
|
|
203
|
+
|
|
204
|
+
### Monitoring Setup
|
|
205
|
+
1. Subscribe to telemetry events early
|
|
206
|
+
2. Track metrics in event handler
|
|
207
|
+
3. Send to analytics/monitoring service
|
|
208
|
+
4. Aggregate and analyze metrics
|
|
209
|
+
5. Unsubscribe when done
|
|
210
|
+
|
|
211
|
+
### Request Interception
|
|
212
|
+
1. Add interceptor before operations
|
|
213
|
+
2. Modify request context
|
|
214
|
+
3. Return modified context
|
|
215
|
+
4. Clean up interceptor when done
|
|
216
|
+
5. Handle errors appropriately
|
|
217
|
+
|
|
218
|
+
### Response Interception
|
|
219
|
+
1. Add interceptor before operations
|
|
220
|
+
2. Process response data
|
|
221
|
+
3. Return modified context
|
|
222
|
+
4. Clean up interceptor when done
|
|
223
|
+
5. Handle errors appropriately
|
|
224
|
+
|
|
225
|
+
### Performance Optimization
|
|
226
|
+
1. Cache expensive operations
|
|
227
|
+
2. Monitor cache hit rates
|
|
228
|
+
3. Adjust TTL based on data freshness
|
|
229
|
+
4. Track operation durations
|
|
230
|
+
5. Optimize based on metrics
|
|
231
|
+
|
|
232
|
+
## 🚨 Common Pitfalls
|
|
233
|
+
|
|
234
|
+
### Don't
|
|
235
|
+
- Cache everything indiscriminately
|
|
236
|
+
- Forget to unsubscribe from events
|
|
237
|
+
- Block execution in interceptors
|
|
238
|
+
- Emit sensitive data in telemetry
|
|
239
|
+
- Create circular dependencies
|
|
240
|
+
|
|
241
|
+
### Do
|
|
242
|
+
- Set appropriate cache TTL values
|
|
243
|
+
- Monitor cache hit rates
|
|
244
|
+
- Clean up resources
|
|
245
|
+
- Keep interceptors lightweight
|
|
246
|
+
- Aggregate telemetry metrics
|
|
247
|
+
- Handle errors gracefully
|
|
248
|
+
|
|
249
|
+
---
|
|
250
|
+
|
|
251
|
+
**Last Updated**: 2025-01-08
|
|
252
|
+
**See Also**: [AI_GUIDELINES.md](../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# Cache System
|
|
2
|
+
|
|
3
|
+
LRU (Least Recently Used) cache implementation for performance optimization. Supports TTL-based expiration and automatic cleanup.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import {
|
|
9
|
+
SimpleCache,
|
|
10
|
+
modelSelectionCache
|
|
11
|
+
} from '@umituz/react-native-ai-gemini-provider';
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## 🎯 Purpose
|
|
15
|
+
|
|
16
|
+
Use cache to store expensive operation results and avoid redundant API calls. Improves performance and reduces costs.
|
|
17
|
+
|
|
18
|
+
**When to use:**
|
|
19
|
+
- Cache API responses
|
|
20
|
+
- Store model selection results
|
|
21
|
+
- Optimize expensive operations
|
|
22
|
+
- Reduce API calls
|
|
23
|
+
- Improve response times
|
|
24
|
+
|
|
25
|
+
## 📌 Strategy
|
|
26
|
+
|
|
27
|
+
Caching significantly improves performance. This system:
|
|
28
|
+
- Implements LRU eviction policy
|
|
29
|
+
- Supports TTL (time-to-live) for entries
|
|
30
|
+
- Provides type-safe caching
|
|
31
|
+
- Automatically expires stale entries
|
|
32
|
+
- Offers global and instance caching
|
|
33
|
+
|
|
34
|
+
**Key Decision**: Use `modelSelectionCache` for model selection. Create custom `SimpleCache` instances for specific use cases.
|
|
35
|
+
|
|
36
|
+
## ⚠️ Rules
|
|
37
|
+
|
|
38
|
+
### Usage Rules
|
|
39
|
+
- **MUST** configure appropriate cache size
|
|
40
|
+
- **SHOULD** set reasonable TTL values
|
|
41
|
+
- **MUST** handle cache misses gracefully
|
|
42
|
+
- **SHOULD** monitor cache hit rates
|
|
43
|
+
- **MUST NOT** cache everything indiscriminately
|
|
44
|
+
|
|
45
|
+
### Configuration Rules
|
|
46
|
+
- **MUST** set `maxSize` based on memory constraints
|
|
47
|
+
- **SHOULD** set `ttl` based on data freshness needs
|
|
48
|
+
- **MUST** consider cache entry size
|
|
49
|
+
- **SHOULD** invalidate stale data
|
|
50
|
+
- **MUST NOT** use excessive cache sizes
|
|
51
|
+
|
|
52
|
+
### Cache Key Rules
|
|
53
|
+
- **MUST** use descriptive cache keys
|
|
54
|
+
- **SHOULD** include relevant parameters in key
|
|
55
|
+
- **MUST NOT** use duplicate keys for different data
|
|
56
|
+
- **SHOULD** namespace keys appropriately
|
|
57
|
+
- **MUST** handle key collisions
|
|
58
|
+
|
|
59
|
+
### Error Handling Rules
|
|
60
|
+
- **MUST** handle cache failures gracefully
|
|
61
|
+
- **SHOULD** log cache errors in development
|
|
62
|
+
- **MUST** provide fallback on cache miss
|
|
63
|
+
- **SHOULD NOT** throw errors from cache operations
|
|
64
|
+
- **MUST** return data on cache miss
|
|
65
|
+
|
|
66
|
+
## 🤖 AI Agent Guidelines
|
|
67
|
+
|
|
68
|
+
### When Modifying Cache Module
|
|
69
|
+
1. **READ** existing cache implementation first
|
|
70
|
+
2. **UNDERSTAND** LRU eviction logic
|
|
71
|
+
3. **MAINTAIN** backward compatibility
|
|
72
|
+
4. **ADD** tests for new functionality
|
|
73
|
+
5. **UPDATE** documentation
|
|
74
|
+
|
|
75
|
+
### When Adding New Caches
|
|
76
|
+
1. **CHECK** if similar cache exists
|
|
77
|
+
2. **FOLLOW** existing cache patterns
|
|
78
|
+
3. **USE** appropriate configuration
|
|
79
|
+
4. **DOCUMENT** cache purpose
|
|
80
|
+
5. **ADD** usage examples to tests
|
|
81
|
+
|
|
82
|
+
### When Optimizing Cache
|
|
83
|
+
1. **MEASURE** cache hit rates
|
|
84
|
+
2. **ADJUST** TTL values based on usage
|
|
85
|
+
3. **MONITOR** memory usage
|
|
86
|
+
4. **OPTIMIZE** cache size
|
|
87
|
+
5. **TEST** performance improvements
|
|
88
|
+
|
|
89
|
+
### Code Style Rules
|
|
90
|
+
- **USE** generic type parameters
|
|
91
|
+
- **VALIDATE** cache inputs
|
|
92
|
+
- **HANDLE** edge cases (null, undefined)
|
|
93
|
+
- **LOG** cache operations in development
|
|
94
|
+
- **COMMENT** complex logic only
|
|
95
|
+
|
|
96
|
+
## 📦 Available Caches
|
|
97
|
+
|
|
98
|
+
### SimpleCache
|
|
99
|
+
|
|
100
|
+
**Refer to**: [`SimpleCache.ts`](./SimpleCache.ts)
|
|
101
|
+
|
|
102
|
+
Generic LRU cache implementation.
|
|
103
|
+
|
|
104
|
+
**Methods:**
|
|
105
|
+
- `set(key, value, ttl?)` - Store value
|
|
106
|
+
- `get(key)` - Retrieve value
|
|
107
|
+
- `has(key)` - Check existence
|
|
108
|
+
- `delete(key)` - Remove entry
|
|
109
|
+
- `clear()` - Clear all entries
|
|
110
|
+
- `size()` - Get cache size
|
|
111
|
+
- `keys()` - Get all keys
|
|
112
|
+
- `getStats()` - Get statistics
|
|
113
|
+
|
|
114
|
+
### modelSelectionCache
|
|
115
|
+
|
|
116
|
+
**Refer to**: [`index.ts`](./index.ts)
|
|
117
|
+
|
|
118
|
+
Global cache for model selection results.
|
|
119
|
+
|
|
120
|
+
**Configuration:**
|
|
121
|
+
- `maxSize`: 50 entries
|
|
122
|
+
- `ttl`: 10 minutes
|
|
123
|
+
- Automatic model-feature mapping
|
|
124
|
+
|
|
125
|
+
## 🔗 Related Modules
|
|
126
|
+
|
|
127
|
+
- **Infrastructure README**: [`../infrastructure/README.md`](../infrastructure/README.md)
|
|
128
|
+
- **Services**: [`../services/README.md`](../services/README.md)
|
|
129
|
+
- **Performance Utils**: [`../utils/PERFORMANCE_UTILS.md`](../utils/PERFORMANCE_UTILS.md)
|
|
130
|
+
|
|
131
|
+
## 📋 Configuration Reference
|
|
132
|
+
|
|
133
|
+
### Cache Options
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
interface CacheOptions {
|
|
137
|
+
maxSize?: number; // Maximum entries (default: 100)
|
|
138
|
+
ttl?: number; // Time-to-live in milliseconds (default: 5 minutes)
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Typical TTL Values
|
|
143
|
+
|
|
144
|
+
- **Hot data**: 30 seconds - 1 minute
|
|
145
|
+
- **Warm data**: 5 - 10 minutes
|
|
146
|
+
- **Cold data**: 30 - 60 minutes
|
|
147
|
+
- **Static data**: 1+ hours
|
|
148
|
+
|
|
149
|
+
### Cache Size Guidelines
|
|
150
|
+
|
|
151
|
+
- **Small cache**: 20-50 entries
|
|
152
|
+
- **Medium cache**: 100-200 entries
|
|
153
|
+
- **Large cache**: 500-1000 entries
|
|
154
|
+
- **Consider memory constraints**
|
|
155
|
+
|
|
156
|
+
## 🎓 Usage Patterns
|
|
157
|
+
|
|
158
|
+
### Basic Caching
|
|
159
|
+
1. Create `SimpleCache` instance
|
|
160
|
+
2. Configure size and TTL
|
|
161
|
+
3. Check cache before operation
|
|
162
|
+
4. Store result in cache
|
|
163
|
+
5. Handle cache hits and misses
|
|
164
|
+
|
|
165
|
+
### Model Selection Caching
|
|
166
|
+
1. Import `modelSelectionCache`
|
|
167
|
+
2. Check cache for feature-model mapping
|
|
168
|
+
3. Store selection in cache
|
|
169
|
+
4. Use cached model ID
|
|
170
|
+
5. Handle cache misses
|
|
171
|
+
|
|
172
|
+
### Cache Invalidation
|
|
173
|
+
1. Get all cache keys
|
|
174
|
+
2. Filter by pattern
|
|
175
|
+
3. Delete matching entries
|
|
176
|
+
4. Clear specific data
|
|
177
|
+
5. Update affected entries
|
|
178
|
+
|
|
179
|
+
### Multi-Level Caching
|
|
180
|
+
1. Create multiple cache instances
|
|
181
|
+
2. Use different TTL for each level
|
|
182
|
+
3. Check caches in order (fast to slow)
|
|
183
|
+
4. Promote data between levels
|
|
184
|
+
5. Handle multi-level misses
|
|
185
|
+
|
|
186
|
+
### Cache Monitoring
|
|
187
|
+
1. Call `getStats()` periodically
|
|
188
|
+
2. Calculate cache hit rate
|
|
189
|
+
3. Monitor memory usage
|
|
190
|
+
4. Adjust configuration based on metrics
|
|
191
|
+
5. Log performance data
|
|
192
|
+
|
|
193
|
+
## 🚨 Common Pitfalls
|
|
194
|
+
|
|
195
|
+
### Don't
|
|
196
|
+
- Cache everything indiscriminately
|
|
197
|
+
- Set excessive cache sizes
|
|
198
|
+
- Use very long TTL for dynamic data
|
|
199
|
+
- Forget to handle cache misses
|
|
200
|
+
- Cache sensitive or user-specific data without care
|
|
201
|
+
|
|
202
|
+
### Do
|
|
203
|
+
- Monitor cache hit rates
|
|
204
|
+
- Set appropriate TTL values
|
|
205
|
+
- Handle cache misses gracefully
|
|
206
|
+
- Clear cache periodically
|
|
207
|
+
- Use namespace prefixes for keys
|
|
208
|
+
- Consider memory constraints
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
**Last Updated**: 2025-01-08
|
|
213
|
+
**See Also**: [AI_GUIDELINES.md](../../../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# Cache Module
|
|
2
|
+
|
|
3
|
+
LRU (Least Recently Used) cache implementation. Used for performance optimization and cost savings.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import {
|
|
9
|
+
SimpleCache,
|
|
10
|
+
modelSelectionCache
|
|
11
|
+
} from '@umituz/react-native-ai-gemini-provider';
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## 🎯 Purpose
|
|
15
|
+
|
|
16
|
+
Use cache to store expensive operation results and avoid redundant API calls. Improves performance and reduces costs.
|
|
17
|
+
|
|
18
|
+
**When to use:**
|
|
19
|
+
- Cache API responses
|
|
20
|
+
- Store model selection results
|
|
21
|
+
- Optimize expensive operations
|
|
22
|
+
- Reduce API calls
|
|
23
|
+
- Improve response times
|
|
24
|
+
|
|
25
|
+
## 📌 Strategy
|
|
26
|
+
|
|
27
|
+
Caching significantly improves performance. This module:
|
|
28
|
+
- Implements LRU eviction policy
|
|
29
|
+
- Supports TTL (time-to-live) for entries
|
|
30
|
+
- Provides type-safe caching
|
|
31
|
+
- Automatically expires stale entries
|
|
32
|
+
- Offers global and instance caching
|
|
33
|
+
|
|
34
|
+
**Key Decision**: Use `modelSelectionCache` for model selection. Create custom `SimpleCache` instances for specific use cases.
|
|
35
|
+
|
|
36
|
+
## ⚠️ Rules
|
|
37
|
+
|
|
38
|
+
### Usage Rules
|
|
39
|
+
- **MUST** configure appropriate cache size
|
|
40
|
+
- **SHOULD** set reasonable TTL values
|
|
41
|
+
- **MUST** handle cache misses gracefully
|
|
42
|
+
- **SHOULD** monitor cache hit rates
|
|
43
|
+
- **MUST NOT** cache everything indiscriminately
|
|
44
|
+
|
|
45
|
+
### Configuration Rules
|
|
46
|
+
- **MUST** set `maxSize` based on memory constraints
|
|
47
|
+
- **SHOULD** set `ttl` based on data freshness needs
|
|
48
|
+
- **MUST** consider cache entry size
|
|
49
|
+
- **SHOULD** invalidate stale data
|
|
50
|
+
- **MUST NOT** use excessive cache sizes
|
|
51
|
+
|
|
52
|
+
### Cache Key Rules
|
|
53
|
+
- **MUST** use descriptive cache keys
|
|
54
|
+
- **SHOULD** include relevant parameters in key
|
|
55
|
+
- **MUST NOT** use duplicate keys for different data
|
|
56
|
+
- **SHOULD** namespace keys appropriately
|
|
57
|
+
- **MUST** handle key collisions
|
|
58
|
+
|
|
59
|
+
### Error Handling Rules
|
|
60
|
+
- **MUST** handle cache failures gracefully
|
|
61
|
+
- **SHOULD** log cache errors in development
|
|
62
|
+
- **MUST** provide fallback on cache miss
|
|
63
|
+
- **SHOULD NOT** throw errors from cache operations
|
|
64
|
+
- **MUST** return data on cache miss
|
|
65
|
+
|
|
66
|
+
## 🤖 AI Agent Guidelines
|
|
67
|
+
|
|
68
|
+
### When Modifying Cache Module
|
|
69
|
+
1. **READ** existing cache implementation first
|
|
70
|
+
2. **UNDERSTAND** LRU eviction logic
|
|
71
|
+
3. **MAINTAIN** backward compatibility
|
|
72
|
+
4. **ADD** tests for new functionality
|
|
73
|
+
5. **UPDATE** documentation
|
|
74
|
+
|
|
75
|
+
### When Adding New Caches
|
|
76
|
+
1. **CHECK** if similar cache exists
|
|
77
|
+
2. **FOLLOW** existing cache patterns
|
|
78
|
+
3. **USE** appropriate configuration
|
|
79
|
+
4. **DOCUMENT** cache purpose
|
|
80
|
+
5. **ADD** usage examples to tests
|
|
81
|
+
|
|
82
|
+
### When Optimizing Cache
|
|
83
|
+
1. **MEASURE** cache hit rates
|
|
84
|
+
2. **ADJUST** TTL values based on usage
|
|
85
|
+
3. **MONITOR** memory usage
|
|
86
|
+
4. **OPTIMIZE** cache size
|
|
87
|
+
5. **TEST** performance improvements
|
|
88
|
+
|
|
89
|
+
### Code Style Rules
|
|
90
|
+
- **USE** generic type parameters
|
|
91
|
+
- **VALIDATE** cache inputs
|
|
92
|
+
- **HANDLE** edge cases (null, undefined)
|
|
93
|
+
- **LOG** cache operations in development
|
|
94
|
+
- **COMMENT** complex logic only
|
|
95
|
+
|
|
96
|
+
## 📦 Available Caches
|
|
97
|
+
|
|
98
|
+
### SimpleCache
|
|
99
|
+
|
|
100
|
+
Generic LRU cache implementation.
|
|
101
|
+
|
|
102
|
+
**Refer to**: [`SimpleCache.ts`](./SimpleCache.ts)
|
|
103
|
+
|
|
104
|
+
**Methods:**
|
|
105
|
+
- `set(key, value, ttl?)` - Store value
|
|
106
|
+
- `get(key)` - Retrieve value
|
|
107
|
+
- `has(key)` - Check existence
|
|
108
|
+
- `delete(key)` - Remove entry
|
|
109
|
+
- `clear()` - Clear all entries
|
|
110
|
+
- `size()` - Get cache size
|
|
111
|
+
- `keys()` - Get all keys
|
|
112
|
+
- `getStats()` - Get statistics
|
|
113
|
+
|
|
114
|
+
### modelSelectionCache
|
|
115
|
+
|
|
116
|
+
Global cache for model selection results.
|
|
117
|
+
|
|
118
|
+
**Refer to**: [`index.ts`](./index.ts)
|
|
119
|
+
|
|
120
|
+
**Configuration:**
|
|
121
|
+
- `maxSize`: 50 entries
|
|
122
|
+
- `ttl`: 10 minutes
|
|
123
|
+
- Automatic model-feature mapping
|
|
124
|
+
|
|
125
|
+
## 🔗 Related Modules
|
|
126
|
+
|
|
127
|
+
- **Infrastructure README**: [`../infrastructure/README.md`](../infrastructure/README.md)
|
|
128
|
+
- **Services**: [`../services/README.md`](../services/README.md)
|
|
129
|
+
- **Performance Utils**: [`../utils/PERFORMANCE_UTILS.md`](../utils/PERFORMANCE_UTILS.md)
|
|
130
|
+
|
|
131
|
+
## 📋 Configuration Reference
|
|
132
|
+
|
|
133
|
+
### Cache Options
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
interface CacheOptions {
|
|
137
|
+
maxSize?: number; // Maximum entries (default: 100)
|
|
138
|
+
ttl?: number; // Time-to-live in milliseconds (default: 5 minutes)
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Typical TTL Values
|
|
143
|
+
|
|
144
|
+
- **Hot data**: 30 seconds - 1 minute
|
|
145
|
+
- **Warm data**: 5 - 10 minutes
|
|
146
|
+
- **Cold data**: 30 - 60 minutes
|
|
147
|
+
- **Static data**: 1+ hours
|
|
148
|
+
|
|
149
|
+
### Cache Size Guidelines
|
|
150
|
+
|
|
151
|
+
- **Small cache**: 20-50 entries
|
|
152
|
+
- **Medium cache**: 100-200 entries
|
|
153
|
+
- **Large cache**: 500-1000 entries
|
|
154
|
+
- **Consider memory constraints**
|
|
155
|
+
|
|
156
|
+
## 🎓 Usage Patterns
|
|
157
|
+
|
|
158
|
+
### Basic Caching
|
|
159
|
+
1. Create `SimpleCache` instance
|
|
160
|
+
2. Configure size and TTL
|
|
161
|
+
3. Check cache before operation
|
|
162
|
+
4. Store result in cache
|
|
163
|
+
5. Handle cache hits and misses
|
|
164
|
+
|
|
165
|
+
### Model Selection Caching
|
|
166
|
+
1. Import `modelSelectionCache`
|
|
167
|
+
2. Check cache for feature-model mapping
|
|
168
|
+
3. Store selection in cache
|
|
169
|
+
4. Use cached model ID
|
|
170
|
+
5. Handle cache misses
|
|
171
|
+
|
|
172
|
+
### Cache Invalidation
|
|
173
|
+
1. Get all cache keys
|
|
174
|
+
2. Filter by pattern
|
|
175
|
+
3. Delete matching entries
|
|
176
|
+
4. Clear specific data
|
|
177
|
+
5. Update affected entries
|
|
178
|
+
|
|
179
|
+
### Multi-Level Caching
|
|
180
|
+
1. Create multiple cache instances
|
|
181
|
+
2. Use different TTL for each level
|
|
182
|
+
3. Check caches in order (fast to slow)
|
|
183
|
+
4. Promote data between levels
|
|
184
|
+
5. Handle multi-level misses
|
|
185
|
+
|
|
186
|
+
### Cache Monitoring
|
|
187
|
+
1. Call `getStats()` periodically
|
|
188
|
+
2. Calculate cache hit rate
|
|
189
|
+
3. Monitor memory usage
|
|
190
|
+
4. Adjust configuration based on metrics
|
|
191
|
+
5. Log performance data
|
|
192
|
+
|
|
193
|
+
## 🚨 Common Pitfalls
|
|
194
|
+
|
|
195
|
+
### Don't
|
|
196
|
+
- Cache everything indiscriminately
|
|
197
|
+
- Set excessive cache sizes
|
|
198
|
+
- Use very long TTL for dynamic data
|
|
199
|
+
- Forget to handle cache misses
|
|
200
|
+
- Cache sensitive or user-specific data without care
|
|
201
|
+
|
|
202
|
+
### Do
|
|
203
|
+
- Monitor cache hit rates
|
|
204
|
+
- Set appropriate TTL values
|
|
205
|
+
- Handle cache misses gracefully
|
|
206
|
+
- Clear cache periodically
|
|
207
|
+
- Use namespace prefixes for keys
|
|
208
|
+
- Consider memory constraints
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
**Last Updated**: 2025-01-08
|
|
213
|
+
**See Also**: [AI_GUIDELINES.md](../../../AI_GUIDELINES.md)
|