@geekmidas/cache 0.0.6 → 0.0.7
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/README.md +68 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,11 +6,12 @@ A type-safe, flexible caching library for TypeScript applications with support f
|
|
|
6
6
|
|
|
7
7
|
- **Type-safe**: Full TypeScript support with generics for strongly-typed cache values
|
|
8
8
|
- **Unified interface**: Common `Cache<T>` interface for all implementations
|
|
9
|
-
- **Multiple backends**: In-memory
|
|
9
|
+
- **Multiple backends**: In-memory, Redis (Upstash), and Expo Secure Store implementations
|
|
10
10
|
- **Async API**: Promise-based interface for consistency across implementations
|
|
11
|
-
- **Flexible TTL**: Support for time-to-live
|
|
11
|
+
- **Flexible TTL**: Support for time-to-live across all implementations
|
|
12
12
|
- **Easy testing**: Simple interface makes mocking and testing straightforward
|
|
13
13
|
- **Modular exports**: Import only what you need
|
|
14
|
+
- **React Native support**: Expo Secure Store implementation for mobile apps
|
|
14
15
|
|
|
15
16
|
## Installation
|
|
16
17
|
|
|
@@ -25,6 +26,11 @@ For Redis support via Upstash:
|
|
|
25
26
|
npm install @upstash/redis
|
|
26
27
|
```
|
|
27
28
|
|
|
29
|
+
For React Native support via Expo:
|
|
30
|
+
```bash
|
|
31
|
+
npm install expo-secure-store
|
|
32
|
+
```
|
|
33
|
+
|
|
28
34
|
## Quick Start
|
|
29
35
|
|
|
30
36
|
### In-Memory Cache
|
|
@@ -208,6 +214,58 @@ await cache.set('user:1', {
|
|
|
208
214
|
const user = await cache.get('user:1');
|
|
209
215
|
```
|
|
210
216
|
|
|
217
|
+
### ExpoSecureCache
|
|
218
|
+
|
|
219
|
+
Secure storage for React Native apps using Expo SecureStore.
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
import { ExpoSecureCache } from '@geekmidas/cache/expo';
|
|
223
|
+
|
|
224
|
+
class ExpoSecureCache<T> implements Cache<T>
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
#### Features
|
|
228
|
+
|
|
229
|
+
- ✅ Secure, encrypted storage on device
|
|
230
|
+
- ✅ TTL support with automatic expiration
|
|
231
|
+
- ✅ Persistent across app restarts
|
|
232
|
+
- ✅ iOS Keychain & Android Keystore backed
|
|
233
|
+
- ❌ React Native/Expo only
|
|
234
|
+
- ❌ Limited storage capacity (varies by platform)
|
|
235
|
+
- ❌ Slower than in-memory cache
|
|
236
|
+
|
|
237
|
+
#### Example
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
import { ExpoSecureCache } from '@geekmidas/cache/expo';
|
|
241
|
+
|
|
242
|
+
interface AuthTokens {
|
|
243
|
+
accessToken: string;
|
|
244
|
+
refreshToken: string;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const cache = new ExpoSecureCache<AuthTokens>();
|
|
248
|
+
|
|
249
|
+
// Store tokens securely with 1 hour TTL
|
|
250
|
+
await cache.set('auth:tokens', {
|
|
251
|
+
accessToken: 'eyJhbGciOiJIUzI1NiIs...',
|
|
252
|
+
refreshToken: 'dGhpcyBpcyBhIHJlZnJlc2ggdG9rZW4...'
|
|
253
|
+
}, 3600);
|
|
254
|
+
|
|
255
|
+
// Retrieve tokens
|
|
256
|
+
const tokens = await cache.get('auth:tokens');
|
|
257
|
+
|
|
258
|
+
// Clear tokens on logout
|
|
259
|
+
await cache.delete('auth:tokens');
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
#### Platform Considerations
|
|
263
|
+
|
|
264
|
+
- **iOS**: Uses Keychain Services, data persists across app reinstalls
|
|
265
|
+
- **Android**: Uses Android Keystore, data cleared on app uninstall
|
|
266
|
+
- **Storage Limits**: Approximately 2KB per item on most devices
|
|
267
|
+
- **Performance**: Async operations can be slower than memory cache
|
|
268
|
+
|
|
211
269
|
## Advanced Usage
|
|
212
270
|
|
|
213
271
|
### Type Safety
|
|
@@ -289,7 +347,7 @@ Create cache instances based on configuration:
|
|
|
289
347
|
|
|
290
348
|
```typescript
|
|
291
349
|
interface CacheConfig {
|
|
292
|
-
type: 'memory' | 'redis';
|
|
350
|
+
type: 'memory' | 'redis' | 'expo';
|
|
293
351
|
redis?: {
|
|
294
352
|
url: string;
|
|
295
353
|
token: string;
|
|
@@ -303,6 +361,8 @@ function createCache<T>(config: CacheConfig): Cache<T> {
|
|
|
303
361
|
case 'redis':
|
|
304
362
|
if (!config.redis) throw new Error('Redis config required');
|
|
305
363
|
return new UpstashCache<T>(config.redis.url, config.redis.token);
|
|
364
|
+
case 'expo':
|
|
365
|
+
return new ExpoSecureCache<T>();
|
|
306
366
|
default:
|
|
307
367
|
throw new Error(`Unsupported cache type: ${config.type}`);
|
|
308
368
|
}
|
|
@@ -418,6 +478,11 @@ describe('UserService', () => {
|
|
|
418
478
|
- **Cons**: Network latency, external dependency
|
|
419
479
|
- **Best for**: Production, multi-instance apps, shared cache
|
|
420
480
|
|
|
481
|
+
### ExpoSecureCache
|
|
482
|
+
- **Pros**: Secure, encrypted, persistent on device
|
|
483
|
+
- **Cons**: React Native only, storage limits, slower operations
|
|
484
|
+
- **Best for**: Mobile apps, sensitive data (tokens, credentials)
|
|
485
|
+
|
|
421
486
|
### Key Strategies
|
|
422
487
|
|
|
423
488
|
1. **Use appropriate TTL**: Balance between cache freshness and performance
|