https://github.com/yss/memo-cache.js
A simple in-memory cache for node and browser, based on Map.
https://github.com/yss/memo-cache.js
cache map memory node
Last synced: 3 months ago
JSON representation
A simple in-memory cache for node and browser, based on Map.
- Host: GitHub
- URL: https://github.com/yss/memo-cache.js
- Owner: yss
- License: mit
- Created: 2017-08-04T09:04:25.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-08-07T10:44:05.000Z (almost 8 years ago)
- Last Synced: 2025-02-17T06:16:37.105Z (4 months ago)
- Topics: cache, map, memory, node
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cache.js
This is a very very very simple memory cache which inherit from Map.
Use it just like the same with use Map.
Importantly, it can use in browser, and also in node environment.## Install
```sh
npm install memo-cache.js --save
```## Usage
```js
const MemoCache = require('memo-cache.js');const memoCache = new MemoCache();
const value = Promise.resolve('value');
// set a cache with expired after 60s;
memoCache.set('key', value, 60);// get a cache value
const val = memoCache.get('key');// delete a cache
memoCache.delete('key');// check if exists
memoCache.has('key'); // return false// change default ttl
memoCache.setTtl(10);
// change one key's ttl
memoCache.setTtl(10, 'key');
```Also you can custom it with you own advanced features, by just inherit from it!
```js
class CustomCache extend MemoCache {
// do want you want to do
}
```## Class
`MemoCache(ttl)`
ttl is the short for time to live. default is 5s.
Almost methods is the same with Map, but `set` and `setTtl`:
* set(key, value, ttl=5)
Only add one argument `ttl` when compare with `Map.prototype.set`.
This `ttl` argument is used to set the expired time of this key. and the unit of `ttl` is second.
* setTtl(ttl[, key])
Set the default ttl or one key's ttl.
All the Map method see:
## Compatibility
**In Node:* use it anyway.**
**In Browser:**see caniusemap
## Test
```sh
npm test
```