Releases: Dias1c/hungry-local-storage
v1.0.1
Release 1.0.1 version of hungry-local-storage
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.
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 oldautoFlush
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 runslocalStorage.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
Release v0.1.2 - "hungry-local-storage"
Release of version 0.1.2 of hungry-local-storage, a powerful JavaScript library for managing data in localStorage with built-in expiration time handling.
Features and Highlights:
Expiration Time Support: Easily store data in localStorage with specified expiration times, making it ideal for managing temporary data or data that is no longer needed after a certain period.
Automatic Expiration Checking: The library automatically checks for expired items in localStorage, ensuring that you always retrieve up-to-date data.
Flexible Time Units: Set expiration times in seconds, minutes, hours, days, weeks, months, and years, providing fine-grained control over data retention.
Auto Flush: Enable automatic removal of expired items at specified intervals to keep your storage clean and efficient.
Simple library: The library provides a straightforward library for setting, retrieving, and managing data, making it easy to work with.
Convenient Auto Flush: Schedule periodic automatic flushes to keep your localStorage in top shape, without manual intervention.
Getting Started:
To start using hungry-local-storage, follow the installation and usage instructions in the README. The README contains detailed information on how to install the library and examples for each of its methods.