Key-value based cache service for React Native.
npm i --save react-native-cache-async
import { CacheService } from "react-native-cache-async";
import { CacheService } from "react-native-cache-async";
class MyApp {
constructor() {
this.cacheService = new CacheService();
}
async insertCacheValue() {
const userData = {
name: 'Maikon',
age: 23
}
// Means that if now is 8:30 PM, will expire at 8:35PM
const timeToExpire = 5;
await this.cacheService.set('userData', userData, timeToExpire);
}
async getCacheValue() {
const isExpired = await this.cacheService.isExpired('userData');
if (!isExpired) {
// If exists returns the object, else returns false.
return await this.cacheService.get('userData');
}
return 'Expired key!';
}
async removeCacheKey() {
return await this.cacheService.removeKey('userData');
}
}