Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fwh1990/cache-bucket
Light Cache for nodeJs and browserJs with TTL.
https://github.com/fwh1990/cache-bucket
browser cache localstorage memory nodejs sessionstorage
Last synced: about 4 hours ago
JSON representation
Light Cache for nodeJs and browserJs with TTL.
- Host: GitHub
- URL: https://github.com/fwh1990/cache-bucket
- Owner: fwh1990
- License: mit
- Created: 2018-10-18T08:59:10.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-11-02T04:45:50.000Z (about 6 years ago)
- Last Synced: 2024-10-29T15:14:58.939Z (17 days ago)
- Topics: browser, cache, localstorage, memory, nodejs, sessionstorage
- Language: TypeScript
- Homepage:
- Size: 49.8 KB
- Stars: 14
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Introduction
Cache your data with TTL. Using this package in node.js and browser.# Installation
By npm
```bash
npm install cache-bucket
```
Or by yarn
```bash
yarn add cache-bucket
```# Support
## Node.js
**FileCache** and **MemoryCache** is enable.Notice that CacheFile will touch a file as storage. The default file path is: ./.filecache
```js
// Based on file
import {cache} from 'cache-bucket/file-cache';// Based on memory
import {cache} from 'cache-bucket/memory-cache';
```
## Browser
**LocalCache** and **SessionCache** and **MemoryCache** is enable.Notice that LocalCache is based on localStorage, and SessionCache is based on sessionStorage.
```js
// Based on memory
import {cache} from 'cache-bucket/memory-cache';// Based on localStorage
import {cache} from 'cache-bucket/local-cache';// Based on sessionStorage
import {cache} from 'cache-bucket/session-cache';
```# Methods
### get(key: string, defaultValue?: any) => any
Get your cache by key.If cache is empty, defaultValue will be used. If parameter defaultValue is missing, method will respond `null`.
```js
cache.get('foo'); // null
cache.get('foo', 'default-bar'); // default-bar
```### set(key: string, value: any, duration?: number) => void
Set cache data.The parameter `value` type can be **string**, **number**, **object**, **array**. Cache data will expired after millSeconds when you provide duration.
```js
cache.set('foo', 'bar');
cache.set('obj', {pkg: 'cache-bucket'});// Expired after 3 second.
cache.set('array', ['cache', 'bucket'], 3000);
```### getOrSet(key: string, onEmpty: () => any, duration?: number) => any
Get cache data.
When cache data is missing, method will set data immediately. And then return it.
```js
cache.getOrSet('foo', () => {
return 'bar';
}); // bar
```### add(key: string, value: any, duration?: number) => boolean
Set cache data when key is not exist.```js
cache.add('foo', 'bar'); // true
cache.add('foo', 'new-bar'); // false
```### remove(key: string) => void
Delete a cache data.```js
cache.remove('foo');
```### clearExpired() => void
Clear all expired cache data
```js
cache.clearExpired();
```### clearAll() => void
Clear all data.
```js
cache.clearAll();
```# Advanced
### Multiply instances.
```js
import {MemoryCache} from 'cache-bucket/memory-cache';const cache = new MemoryCache();
cache.set('foo', 'bar');
cache.get('foo'); // bar
``````js
import {FileCache} from 'cache-bucket/file-cache';const cache = new FileCache('./.new-cachefile');
cache.set('foo', 'bar');
cache.get('foo'); // bar
```### Config
You can choose the file you want to put data when Using **FileCache**. Just creating config file `cache-bucekt.json` in project's root directory.
```json
{
"defaultFilePath": "./.custom-cache-file"
}
```