just-cache
Version:
Simple in-memory cache manager and controller for Node JS with TTL and storage size limit
129 lines (77 loc) • 2.11 kB
Markdown
Just Cache
===========
Simple in-memory cache manager and controller for Node JS with TTL and storage size limit.
```js
const Just = require("just-cache");
const cache = new Just();
cache.set("message", "this text message");
cache.put("message", { foo: "bar" });
cache.set("message", true);
cache.get("message"); // { foo: "bar" };
```
Installation is easy with the [npm](https://www.npmjs.com) command
```bash
$ npm install just-cache --save
```
- Cache manager with basic and simples commands.
- Storage size info.
- High test coverage.
- Automatic storage limit management.
- Compatible with Typescript.
Set or update cache value.
Add a new cache. If the cache key already exists, will not set the value.
Return the value stored.
Return the stored cache size. (value in bytes).
Return the stored formated size string.
Clean all stored cache.
Check if contains stored key cache.
Remove existing cache by key.
Get all stored keys.
Count all stored keys.
Set TTL on storage cache
```js
const cache = new Just({
ttl: 10 // seconds
});
cache.set("message", "without me");
cache.set("another", "with me", 30 /* seconds */);
cache.get("message"); // 'with me'
// ... after 10 seconds:
cache.get("message"); // null
cache.has("message"); // false
cache.count(); // 1
cache.get("another"); // 'with me'
```
Set memory storage bytes limit.
If new values exceed the memory limit, the first key in the queue will be removed during the sending of the new value.
```js
const cache = new Just({
limit: 100 // in bytes
});
```
Check cache size:
```js
cache.set("message", "this");
cache.size(); // 8
cache.sizeText(); // '8 bytes'
```
- ***ttl*** - General ttl value. Case use set or put with TTL this option will be ignored.
- ***limit*** - Storage size limit in bytes.
- Actions callbacks.
- Info logger option.