You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@cacheable/node-cache

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cacheable/node-cache - npm Package Compare versions

Comparing version

to
1.5.7

2

dist/index.d.ts

@@ -137,3 +137,3 @@ import { Hookified } from 'hookified';

* @param {string | number} key
* @returns {any | undefined}
* @returns {T | undefined}
*/

@@ -140,0 +140,0 @@ take<T>(key: string | number): Promise<T | undefined>;

@@ -191,3 +191,3 @@ // src/index.ts

* @param {string | number} key
* @returns {any | undefined}
* @returns {T | undefined}
*/

@@ -194,0 +194,0 @@ async take(key) {

{
"name": "@cacheable/node-cache",
"version": "1.5.6",
"version": "1.5.7",
"description": "Simple and Maintained fast NodeJS internal caching",

@@ -34,2 +34,3 @@ "type": "module",

"devDependencies": {
"@types/eslint": "^9.6.1",
"@types/node": "^22.15.30",

@@ -36,0 +37,0 @@ "@vitest/coverage-v8": "^3.2.2",

@@ -16,2 +16,3 @@ [<img align="center" src="https://cacheable.org/symbol.svg" alt="Cacheable" />](https://github.com/jaredwray/cacheable)

* Fully Compatible with `node-cache` using `{NodeCache}`
* Faster than the original `node-cache` package 🚀
* Async/Await functionality with `{NodeCacheStore}`

@@ -21,10 +22,9 @@ * Storage Adapters via [Keyv](https://keyv.org) with `{NodeCacheStore}`

Note: `NodeCache` is ready and available for use. `NodeCacheStore` is in progress and will be available soon. Please do not use it until it is released.
# Table of Contents
* [Getting Started](#getting-started)
* [Basic Usage](#basic-usage)
* [Advanced Usage](#advanced-usage)
* [NodeCache Performance](#nodecache-performance)
* [NodeCache API](#nodecache-api)
* [NodeCacheStore](#nodecachestore)
* [API](#api)
* [NodeCacheStore API](#nodecachestore-api)
* [How to Contribute](#how-to-contribute)

@@ -55,3 +55,3 @@ * [License and Copyright](#license-and-copyright)

# NodeCache Not Default Export
The `NodeCache` is not the default export, so you need to import it like this:

@@ -66,74 +66,13 @@ ```javascript

# Advanced Usage
# NodeCache Performance
```javascript
import {NodeStorageCache} from '@cacheable/node-cache';
import {Keyv} from 'keyv';
import {KeyvRedis} from '@keyv/redis';
The performance is comparable if not faster to the original `node-cache` package, but with additional features and improvements.
const storage = new Keyv({store: new KeyvRedis('redis://user:pass@localhost:6379')});
const cache = new NodeStorageCache(storage);
| name | summary | ops/sec | time/op | margin | samples |
|-----------------------------------|:---------:|----------:|----------:|:--------:|----------:|
| Cacheable NodeCache - set / get | 🥇 | 117K | 9µs | ±1.01% | 111K |
| Node Cache - set / get | -4.6% | 112K | 9µs | ±1.31% | 106K |
// with storage you have the same functionality as the NodeCache but will be using async/await
await cache.set('foo', 'bar');
await cache.get('foo'); // 'bar'
# NodeCache API
// if you call getStats() this will now only be for the single instance of the adapter as it is in memory
cache.getStats(); // {hits: 1, misses: 1, keys: 1, ksize: 2, vsize: 3}
```
# NodeCacheStore
The `NodeCacheStore` is a class that extends the `NodeCache` and adds the ability to use storage adapters. This is based on the `cacheable` engine and allows you to do layer 1 and layer 2 caching. The storage adapters are based on the [Keyv](https://keyv.org) package. This allows you to use any of the storage adapters that are available.
```javascript
import {NodeCacheStore} from '@cacheable/node-cache';
const cache = new NodeCacheStore();
cache.set('foo', 'bar');
cache.get('foo'); // 'bar'
```
## NodeCacheStoreOptions
When initializing the cache you can pass in the options below:
```javascript
export type NodeCacheStoreOptions = {
ttl?: number; // The standard ttl as number in milliseconds for every generated cache element. 0 = unlimited
primary?: Keyv; // The primary storage adapter
secondary?: Keyv; // The secondary storage adapter
maxKeys?: number; // Default is 0 (unlimited). If this is set it will throw and error if you try to set more keys than the max.
stats?: boolean; // Default is true, if this is set to false it will not track stats
};
```
Note: the `ttl` is now in milliseconds and not seconds like `stdTTL` in `NodeCache`. You can learn more about using shorthand also in the [cacheable documentation](https://github.com/jaredwray/cacheable/blob/main/packages/cacheable/README.md#shorthand-for-time-to-live-ttl). as it is fulling supported. Here is an example:
```javascript
const cache = new NodeCacheStore({ttl: 60000 }); // 1 minute as it defaults to milliseconds
cache.set('foo', 'bar', '1h'); // 1 hour
cache.set('longfoo', 'bar', '1d'); // 1 day
```
## Node Cache Store API
* `set(key: string | number, value: any, ttl?: number): Promise<boolean>` - Set a key value pair with an optional ttl (in milliseconds). Will return true on success. If the ttl is not set it will default to 0 (no ttl)
* `mset(data: Array<NodeCacheItem>): Promise<boolean>` - Set multiple key value pairs at once
* `get<T>(key: string | number): Promise<T>` - Get a value from the cache by key
* `mget(keys: Array<string | number>): Promise<Record<string, unknown>>` - Get multiple values from the cache by keys
* `take<T>(key: string | number): Promise<T>` - Get a value from the cache by key and delete it
* `del(key: string | number): Promise<boolean>` - Delete a key
* `mdel(keys: Array<string | number>): Promise<boolean>` - Delete multiple keys
* `clear(): Promise<void>` - Clear the cache
* `setTtl(key: string | number, ttl: number): Promise<boolean>` - Set the ttl of a key
* `disconnect(): Promise<void>` - Disconnect the storage adapters
* `stats`: `NodeCacheStats` - Get the stats of the cache
* `ttl`: `number` | `string` - The standard ttl as number in seconds for every generated cache element. `< 0` or `undefined` = unlimited
* `primary`: `Keyv` - The primary storage adapter
* `secondary`: `Keyv` - The secondary storage adapter
* `maxKeys`: `number` - If this is set it will throw and error if you try to set more keys than the max
# API
## `constructor(options?: NodeCacheOptions)`

@@ -168,3 +107,3 @@

## `.set(key: string | number, value: any, ttl?: number): boolean`
## `set(key: string | number, value: any, ttl?: number): boolean`

@@ -177,3 +116,3 @@ Set a key value pair with an optional ttl (in seconds). Will return true on success. If the ttl is not set it will default to 0 (no ttl).

## `.mset(data: Array<NodeCacheItem>): boolean`
## `mset(data: Array<NodeCacheItem>): boolean`

@@ -196,3 +135,3 @@ Set multiple key value pairs at once. This will take an array of objects with the key, value, and optional ttl.

## `.get(key: string | number): any`
## `get<T>(key: string | number): T | undefined`

@@ -205,3 +144,3 @@ Get a value from the cache by key. If the key does not exist it will return `undefined`.

## `mget(keys: Array<string | number>): Record<string, unknown>`
## `mget<T>(keys: Array<string | number>): Record<string, T | undefined>`

@@ -218,3 +157,3 @@ Get multiple values from the cache by keys. This will return an object with the keys and values.

## `take(key: string | number): any`
## `take<T>(key: string | number): T | undefined`

@@ -243,3 +182,3 @@ Get a value from the cache by key and delete it. If the key does not exist it will return `undefined`.

## `.mdel(keys: Array<string | number>): number`
## `mdel(keys: Array<string | number>): number`

@@ -252,3 +191,3 @@ Delete multiple keys from the cache. Will return the number of deleted entries and never fail.

## `.ttl(key: string | number, ttl?: number): boolean`
## `ttl(key: string | number, ttl?: number): boolean`

@@ -278,3 +217,3 @@ Redefine the ttl of a key. Returns true if the key has been found and changed. Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.

## `keys(): Array<string>`
## `keys(): string[]`

@@ -284,3 +223,3 @@ Get all keys from the cache.

```javascript
cache.keys(); // ['foo', 'bar']
await cache.keys(); // ['foo', 'bar']
```

@@ -302,3 +241,3 @@

cache.flushAll();
cache.keys(); // []
await cache.keys(); // []
cache.getStats(); // {hits: 0, misses: 0, keys: 0, ksize: 0, vsize: 0}

@@ -312,16 +251,8 @@ ```

```javascript
cache.set('foo', 'bar');
await cache.set('foo', 'bar');
cache.flushStats();
cache.getStats(); // {hits: 0, misses: 0, keys: 0, ksize: 0, vsize: 0}
cache.keys(); // ['foo']
await cache.keys(); // ['foo']
```
## `close(): void`
this will stop the interval that is running for the `checkperiod` and `deleteOnExpire` options.
```javascript
cache.close();
```
## `on(event: string, callback: Function): void`

@@ -342,2 +273,71 @@

# NodeCacheStore
`NodeCacheStore` has a similar API to `NodeCache` but it is using `async / await` as it uses the `Keyv` storage adapters under the hood. This means that you can use all the storage adapters that are available in `Keyv` and it will work seamlessly with the `NodeCacheStore`. To learn more about the `Keyv` storage adapters you can check out the [Keyv documentation](https://keyv.org).
```javascript
import {NodeCacheStore} from '@cacheable/node-cache';
const cache = new NodeCacheStore();
await cache.set('foo', 'bar');
await cache.get('foo'); // 'bar'
```
Here is an example of how to use the `NodeCacheStore` with a primary and secondary storage adapter:
```javascript
import {NodeStorageCache} from '@cacheable/node-cache';
import {Keyv} from 'keyv';
import {KeyvRedis} from '@keyv/redis';
const primary = new Keyv(); // In-memory storage as primary
const secondary = new Keyv({store: new KeyvRedis('redis://user:pass@localhost:6379')});
const cache = new NodeStorageCache({primary, secondary});
// with storage you have the same functionality as the NodeCache but will be using async/await
await cache.set('foo', 'bar');
await cache.get('foo'); // 'bar'
// if you call getStats() this will now only be for the single instance of the adapter as it is in memory
cache.getStats(); // {hits: 1, misses: 1, keys: 1, ksize: 2, vsize: 3}
```
When initializing the cache you can pass in the options below:
```javascript
export type NodeCacheStoreOptions = {
ttl?: number; // The standard ttl as number in milliseconds for every generated cache element. 0 = unlimited
primary?: Keyv; // The primary storage adapter
secondary?: Keyv; // The secondary storage adapter
maxKeys?: number; // Default is 0 (unlimited). If this is set it will throw and error if you try to set more keys than the max.
stats?: boolean; // Default is true, if this is set to false it will not track stats
};
```
Note: the `ttl` is now in milliseconds and not seconds like `stdTTL` in `NodeCache`. You can learn more about using shorthand also in the [cacheable documentation](https://github.com/jaredwray/cacheable/blob/main/packages/cacheable/README.md#shorthand-for-time-to-live-ttl) as it is fully supported. Here is an example:
```javascript
const cache = new NodeCacheStore({ttl: 60000 }); // 1 minute as it defaults to milliseconds
await cache.set('foo', 'bar', '1h'); // 1 hour
await cache.set('longfoo', 'bar', '1d'); // 1 day
```
## NodeCacheStore API
* `set(key: string | number, value: any, ttl?: number): Promise<boolean>` - Set a key value pair with an optional ttl (in milliseconds). Will return true on success. If the ttl is not set it will default to 0 (no ttl)
* `mset(data: Array<NodeCacheItem>): Promise<boolean>` - Set multiple key value pairs at once
* `get<T>(key: string | number): Promise<T>` - Get a value from the cache by key
* `mget(keys: Array<string | number>): Promise<Record<string, unknown>>` - Get multiple values from the cache by keys
* `take<T>(key: string | number): Promise<T>` - Get a value from the cache by key and delete it
* `del(key: string | number): Promise<boolean>` - Delete a key
* `mdel(keys: Array<string | number>): Promise<boolean>` - Delete multiple keys
* `clear(): Promise<void>` - Clear the cache
* `setTtl(key: string | number, ttl: number): Promise<boolean>` - Set the ttl of a key
* `disconnect(): Promise<void>` - Disconnect the storage adapters
* `stats`: `NodeCacheStats` - Get the stats of the cache
* `ttl`: `number` | `string` - The standard ttl as number in seconds for every generated cache element. `< 0` or `undefined` = unlimited
* `primary`: `Keyv` - The primary storage adapter
* `secondary`: `Keyv` - The secondary storage adapter
* `maxKeys`: `number` - If this is set it will throw and error if you try to set more keys than the max
# How to Contribute

@@ -348,2 +348,2 @@

# License and Copyright
[MIT © Jared Wray](./LICENSE)
[MIT © Jared Wray](./LICENSE)

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet