browser-cache-management-helpers
v1.2.1
Published
A simple browser cache management tool using localforage, lodash, and moment.
Downloads
7
Maintainers
Readme
CacheManagementTool
The CacheManagementTool
is a lightweight, easy-to-use caching solution for JavaScript applications, built on top
of localForage
, lodash
, and moment.js
. It provides a simple API for storing, retrieving, and managing cached data
with time-to-live (TTL) support.
Features
- Simple and intuitive API for cache management.
- Time-to-Live (TTL) support for cache items.
- Automatic removal of expired cache items.
- Built with
localForage
for asynchronous storage. - Uses
moment.js
for date/time calculations. - Utility functions from
lodash
for deep comparison.
Installation
Before you start, make sure you have localForage
, lodash
, and moment.js
installed in your project. If not, you can
install them along with CacheManagementTool
:
npm install localforage lodash moment
Then, you can add CacheManagementTool
to your project:
npm install your-package-name
Replace your-package-name
with the actual name of your package if you're publishing it.
Usage
Import CacheManagementTool
in your project:
import CacheManagementTool from 'path-to-cache-management-tool';
Create an instance of CacheManagementTool
:
const cache = new CacheManagementTool({
storeName: 'myCacheStore', // optional: customize the store name
});
Storing Items
await cache.setItem('key', 'value', { ttl: 120 }); // TTL in seconds
Retrieving Items
const value = await cache.getItem('key');
if (value) {
console.log('Cached value:', value);
} else {
console.log('Item not found or expired');
}
Removing Items
await cache.removeItem('key');
Clearing Expired Items
await cache.clearExpiredItems();
Clearing All Items
await cache.clear();
Configuration
You can configure CacheManagementTool
by passing an options object to the constructor:
storeName
: Name of the localForage store. Defaults to'cacheStore'
.
Contributing
Contributions are welcome! Please feel free to submit a pull request.