Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/akashchouhan16/nodecache.js
đ Simple, Intuitive in-memory cache with TTL support.
https://github.com/akashchouhan16/nodecache.js
cache hacktoberfest node-cache nodejs npm
Last synced: 15 days ago
JSON representation
đ Simple, Intuitive in-memory cache with TTL support.
- Host: GitHub
- URL: https://github.com/akashchouhan16/nodecache.js
- Owner: akashchouhan16
- License: mit
- Created: 2023-05-16T10:51:53.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-02-11T14:50:38.000Z (9 months ago)
- Last Synced: 2024-09-18T08:37:04.578Z (about 2 months ago)
- Topics: cache, hacktoberfest, node-cache, nodejs, npm
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/nodecache.js
- Size: 183 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![nodecache.js](https://github.com/akashchouhan16/nodeCache.js/assets/56465610/3c72f526-73f2-4528-b01d-d188532b8231)
![Travis Build Status](https://img.shields.io/travis/com/akashchouhan16/nodecache.js?label=Travis+CI+build&color=F2ECAC)
![GitHub Workflow Status (with branch)](https://img.shields.io/github/actions/workflow/status/akashchouhan16/nodeCache.js/nodecache-unit-tests.yml?&label=%20Node.js%20CI)
[![Maintenance](https://img.shields.io/badge/Maintained%3F-Yes-1dbf73.svg)](https://github.com/akashchouhan16/nodeCache.js "nodeCache.js is actively Maintained")
[![made-for-Developers](https://img.shields.io/badge/Made%20for-Developers-238636.svg)](https://github.com/akashchouhan16/nodeCache.js "nodeCache.js")A simple and intuitive in-memory cache with TTL support for your JavaScript applications.
## âŠī¸ AboutDiscover an elegant and efficient in-memory cache module for Node.js. This package incorporates time-to-live (**ttl**) functionality, asynchronously evicting expired keys from the cache.
- Support for **up to 1 million keys** that are stored in a single object.
- Leverages **worker_threads**, to optimize cache expiration checks, freeing the main thread for other critical tasks.
## đ Installation
- You can install nodecache.js via npm:
```properties
npm install nodecache.js
```## đ Getting Started
To start using **nodecache.js** with your applications, import and create an instane as follows:
```js
const NodeCache = require("nodecache.js")let myCache = new NodeCache()
```#### **Working with custom params**
- Values in the cache are stored as `string` type by default. To prevent forced string types:
- ```js
let myCache = new NodeCache({
forceString: false // this will disable default type coercion.
})
```
- By default, there is no limit set on the keys for the cache. To limit the max number of keys for your cache instance:
- ```js
let myCache = new NodeCache({
maxKeys: 5000 // Default is -1 to disable the limit.
})
```
- Set a default TTL value for all set() calls with standard ttl configuration value.
- ```js
let myCache = new NodeCache({
stdTTL: 5 * 60 * 1000 // every key saved for ttl: 5mins
})
```
- Cache instance by default only logs errors. To Configure params for **cache logs**:
- Cache log params: **`mode`**, **`type`** and **`path`**
- ```js
let myCache = new NodeCache({
mode: "std"
// std debug mode for std output stream. Defaults to none.
})
```
- Allowed **`mode`** values include: `none`, `std`, `exp`
- ```js
let myCache = new NodeCache({
type: "dev env logs"
// sets a custom debugger msg type. Defaults to info.
})
```
**Console output:**
```shell
[đ Custom Err] 6/2/23, 4:51 PM: nodeCache.js initialized
```
- To configure cache log output stream to a specific file path: `wip`
- ```js
let myCache = new NodeCache({
path: "file://mycache.log"
// sets log output stream, default is set to none.
})
```
---## đŊ APIs
### Access a key: get()
- Accepts a valid key of type: **`number` or `string`**.
- If not found or the **key** has expired: Returns **`undefined`**.
- If found: returns the **`value`** of type: **`number`, `string`, or `object`**.
- ```js
let value = myCache.get("key")
```
### Access multiple keys: getM()
- Accepts an Array of valid keys. **`Array`**
- Returns an Array of **`objects`** corresponding to each key. **`Array<{value, ttl?}>`**
- ```js
let values = myCache.getM(["key1", "key2", ...])
for(let value of values) {
//access all the value objects. {value, ttl}
}
```
### Store a key: set()
- Accepts valid key, value and optional ttl.
- Allowed value types: **`number`, `string`, or `object`**.
- Allowed ttl type: **`number`** in milliseconds.
- Returns a **`boolean`**. True is set was success, else false.
- ```js
let isSet = myCache.set("key", "value", 1200) // key->value set for 1.2s
```
### Store multiple keys: setM()
- Accepts an Array of valid set() inputs. **`Array<{key, value, ttl?}>`**
- Returns an Array of boolean flags indicating set status. **`Array`**
- ```js
let isSetResponses = myCache.setM([{"k1", "v1", 2500}, {"k2", 15}...])
```### Retrieve TTL for a key: getTTL()
- Accepts a valid key: **`number` or `string`**.
- Returns **`undefined`** to key is missing, else the ttl value: **`number`**.
- ```js
let ttl = myCache.getTTL("key1")
```
### Update TTL for a key: setTTL()
- Accepts a valid key and ttl in ms: **`number`**.
- Returns **`boolean`** response. **true** if set was a success else **false**.
- ```js
let success = myCache.setTTL("key1", 12000) // key1 for a ttl: 12sec
```
### Refresh cache instance: refresh()
- Accepts void. Returns a Promise.
- Refresh runs on the main thread, looping over all the keys in the cache, and evicting the expired once.
- This is a **blocking code**. Recommendated when consistency in the cache is a high priority.
- ```js
const importantTask = async () => {
try {
await cache.refresh();
// code ...
} catch(err) { ... }
}
```
### Close the cache instance: close()
- Accepts **`void`** and returns **`void`**.
- It is **mandatory** to invoke **close()** method when cache is no longer needed.
- ```js
let myCache = new NodeCache({...options})
/**
* work with cache instance..
*/myCache.close(); // close worker thread, & free up memory.
```### Retrieve Stats on the cache: global()
- Returns an **object** with cacheHit, cacheMiss and keyCount.
- ```js
let stats = myCache.global()
// stats: { cacheHit, cacheMiss, keyCount }
```
### Flush Cache stats: flush()
- Returns **void**. To clear the current global stats for the cache instance.
- ```js
myCache.flush()
let stats = myCache.global() //stats: { 0, 0, 0}
```### âī¸ To Do (`wip`)
- Node.js Events: **on(`event_type`)**: Support for **initialize**, **close**, **get**, **set**, **delete**, **flush**, and **refresh**.
- Optimizations on existing ttl implementation and the cache eviction policy.
---## đ Contributing
If you're interested to contribute or brain storm ideas, Contributions are most welcomed!
Reach out to: **[[email protected]](mailto:[email protected] "Akash's Gmail")**
## đ License
Copyright (c) **[Akash Chouhan](https://github.com/akashchouhan16 "Akash Chouhan")**. All rights reserved. Released under the **[MIT License](https://github.com/akashchouhan16/nodeCache.js/blob/master/LICENSE "nodeCache.js License")**.