Skip to content

evheniy/offline-first-storage

Repository files navigation

Offline first storage

Offline first logic for yor application

NPM

npm version Build Status Coverage Status Linux Build Windows Build

Dependency Status devDependency Status NSP Status

License GitHub stars GitHub forks GitHub issues Twitter

How to install

npm i -S offline-first-storage

How to use

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();

ioredis example

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();

Links

  • ioredis - promise based node.js redis client
  • config - node.js config
  • debug - node.js debugging utility

About

offline-first-storage

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published