Socket
Socket
Sign inDemoInstall

@empathyco/x-storage-service

Package Overview
Dependencies
Maintainers
5
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@empathyco/x-storage-service

Storage service with TTL


Version published
Weekly downloads
1.5K
increased by8.08%
Maintainers
5
Weekly downloads
 
Created
Source

Storage Service

Storage Service is a wrapper on top of the Web Storage API that provides additional features such as key prefixing and TTL.

Creating an instance

By default, Storage Service works with localStorage and using empathy as prefix. If that sounds good, you can just do:

const storage = new BrowserStorageService();

You can also customize the storage or prefix to be used in the constructor:

StorageService(storage?, prefix?)

For instance, if you want to use sessionStorage and test as prefix instead:

const storage = StorageService(sessionStorage, 'test');

Usage

Storage Service provides four public functions: setItem, getItem, removeItem and clear.

Setting an item

storage.setItem('x', { a: 'this', b: 'can', c: 'be', d: 'anything' });

With the default configuration, this will set an item in localStorage with the key empathy and no TTL.

Getting an item

const item = storage.getItem('x'); // Returns the item

Removing an item

const item = storage.removeItem('x'); // Also returns the item!

Cleaning all items

const removedItemCount = storage.clear(); // Returns the number of items that have been removed

Setting and item with TTL

The setItem function provides an optional third parameter to specify a time-to-live in milliseconds:

storage.setItem('x', 'value', 50);

After the specified time, the item won't be available via getItem or removeItem. Expired items are automatically removed from storage to avoid consuming space unnecessarily.

Keywords

FAQs

Package last updated on 24 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc