Git Product home page Git Product logo

dias1c / hungry-local-storage Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 188 KB

๐Ÿ’พโŒ› Tool for store temporary data in local storage (with autodelete expired data).

Home Page: https://www.npmjs.com/package/@diaskappassov/hungry-local-storage

License: MIT License

TypeScript 100.00%
local-storage localstorage npm-module npm-package local-storage-with-expiration temporary-local-storage

hungry-local-storage's Introduction

hungry-local-storage

๐Ÿ’พโŒ› Tool for store temporary data in local storage (with autodelete expired data).

ts Visit the NPM page Visit package GitHub page

About

This is a simple tool for storing temporary data in localStorage. It's useful for storing temporary data, such as user preferences, but also for storing data that is not needed after a certain period of time.

Why hungry-local-storage?

The reason why I called this package hungry-local-storage is that he is endlessly hungry thats allowed to eat only after the item's store time expires. And it checks at specified intervals (every N seconds you specify) whether the object's storage period has expired so that it can be eaten. Such an obedient, hungry tool.

Usage

Installing

$ npm i "@diaskappassov/hungry-local-storage"

Usage

Setting values to localStorage with expiration

import { hls } from "@diaskappassov/hungry-local-storage";

// No expire time
hls.set("cherry", "string");

const curTimestamp = new Date().getTime() / 1000;
// Expires after 60 seconds
hls.set("banana", 123, curTimestamp + 60);

// Expires after 3600 seconds
hls.set("apple", [1, 2, 3], curTimestamp + 3600);

// Expires after 10 minutes
hls.set("avocado", {}, { minutes: 10 });

// Expires after 3 days
hls.set("tea", { type: "green", count: 2 }, { days: 3 });

// Expires after 7 seconds + 6 minutes + 5 hours + 4 days+ 3 weeks + 2 months + 1 year
hls.set("cake", "CAAAKE!", {
  seconds: 7,
  minutes: 6,
  hours: 5,
  days: 4,
  weeks: 3,
  months: 2,
  years: 1,
});

Getting value from localStorage

Imagine that you run hls.set("apple", [1, 2, 3], { hours: 1 });, and you want to get this value from localStorage.

import { hls } from "@diaskappassov/hungry-local-storage";

// Returns parsed value if not expired, otherwise returns null with removing expired item from localStorage
const apple = hls.get("apple");

console.log(apple); // [1, 2, 3]

Removing expired items from localStorage

import { hls } from "@diaskappassov/hungry-local-storage";

// Removes all expired items and returns count of removed items by function flush
const countRemoved = hls.flush();
console.log("flush returned:", countRemoved);

Don't worry, flush not removes localStorage items that's not have expiration field on top level of structure.

Setting Auto Flush

By default auto flush is disabled. To enable it, execute setAutoFlush method with setting values in seconds.

import { hls } from "@diaskappassov/hungry-local-storage";

// Set auto flush that runs flush every 10 minutes
hls.setAutoFlush(60 * 10);

If you run setAutoFlush method twice with different values, then the last value will be used, and it will finish all old autoFlush timers.

If you set auto flush value to null then auto flush will be disabled.

import { hls } from "@diaskappassov/hungry-local-storage";

// Disable auto flush
hls.setAutoFlush(null);

Removing item from localStorage

import { hls } from "@diaskappassov/hungry-local-storage";

// Removes item with key "apple".
hls.remove("apple");

Under code hls.remove just runs localStorage.removeItem method

Structure of created item by hls

After running code hls.set("apple", [1, 2, 3], { hours: 5 });, in local storage it will be stored as follows:

{
  creation: CURRENT_TIMESTAMP,
  data: "[1,2,3]",
  expiration?: CURRENT_TIMESTAMP + 5_HOURS_IN_TIMESTAMP,
}

Property expiration is optional, it means, if expiration is not exists, it will be stored in local storage while you will not delete it.

License

MIT

Authors

hungry-local-storage's People

Contributors

dias1c avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 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.