Offline first logic for yor application
npm i -S offline-first-storage
const OFS = require('offline-first-storage');
const cache = new OFS({
async getDataFromSource() {},
async getDataFromCache() {},
async setDataToCache(data) {},
async getCacheDate() {},
async updateCacheDate() {},
timeoutErrorHandler(error) {},
ttl: 86400, // 24 hours
});
const data = await cache.getData();
const Redis = require('ioredis');
const redis = new Redis();
const key = 'test';
const cache = new Ofs({
async getDataFromSource() {
return test;
},
async getDataFromCache() {
return redis.get(key);
},
async setDataToCache(data) {
await redis.set(key, data);
},
async getCacheDate() {
return redis.get(`date_${key}`);
},
async updateCacheDate() {
await redis.set(`date_${key}`, new Date().valueOf());
},
timeoutErrorHandler(error) {
debug(error);
},
ttl: 86400,
});
const data = await cache.getData();