GithubHelp home page GithubHelp logo

pewww / browser-cache-storage Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 39km/browser-cache-storage

0.0 2.0 0.0 359 KB

simple cache library based on localStorage and sessionStorage

License: MIT License

JavaScript 0.50% TypeScript 99.50%

browser-cache-storage's Introduction

browser-cache-storage

cache data with local storage or session storage simply

Features

  1. get and set data with localStorage or sessionStorage
  2. when set data, automatically save cached time and compare data is expired when get data

Features in Development

  1. save meta data for clear all data that saved with browser-cache-storage in the localStorage (or sessionStorage)

Installing

$ npm install --save browser-cache-storage
$ yarn add browser-cache-storage

Document

(LocalCache || SessionCache).set

paramters description required
uniqId identify when or who save the cache data. O
cacheKey key of cache data. 'cache.' prefix will attached automatically. it saved in localStorage(or sessionStorage) like cache.${cacheKey}. O
data data want to save O

(LocalCache || SessionCache).get

paramters description required
uniqId identify the cache data is valid. if saved data's uniqId is different with uniqId which is passed to get functions parameter, then get method will return null. O
cacheKey key of cache data. 'cache.' prefix will attached automatically. if cacheKey is 'boardData' then LocalCache.get method return localStorage.getItem('cache.boardData') O
preserveTime relative time to check wheter the saved data is valid. it used like the "It's valid during {preserveTime} after saved". if you set preserveTime to 6000, then the cached data is valid only in 6 second after data saved. if expired then get method return null and delete the data. !! it is not automatically delete with timer like cookie. it only delete when you access data with get method. O

(LocalCache || SessionCache).clear No Paramters delete all cache data which saved through browser-cache-storage

Examples

const cacheKey = 'myCacheData';
var data = {
    hi: 'cache',
};
LocalCache.set(sessionId, cacheKey, data);
console.log(LocalCache.get(sessionId, cacheKey, data);
LocalCache.del(cacheKey);
console.log(LocalCache.get(sessionId, cacheKey, data);
console.log(localStorage.getItem(`cache.${cacheKey}`));
const cacheKey = 'myCacheData';
var data = {
    hi: 'cache',
};
SessionCache.set(sessionId, cacheKey, data);
console.log(SessionCache.get(sessionId, cacheKey, data);
SessionCache.del(cacheKey);
console.log(SessionCache.get(sessionId, cacheKey, data);
console.log(sessionStorage.getItem(`cache.${cacheKey}`));
LocalCache.clear();
SessionCache.clear();

browser-cache-storage's People

Contributors

39km avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.