# varcache

> A pure memory based cache for Javascript.

Latest version **0.0.2** (published 2014-04-22) · 0 weekly downloads

## Install

```sh
npm install varcache
pnpm add varcache
yarn add varcache
bun add varcache
```

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2014-04-22 |
| First published | 2014-04-20 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | * |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Stian Hanger |
| Maintainers | nagilum |
| Keywords | cache, memory |

## Links

- npm: https://www.npmjs.com/package/varcache
- Repository: https://github.com/nagilum/varcache
- Issues: https://github.com/nagilum/varcache/issues
- npm.io page: https://npm.io/package/varcache

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 0.0.2 (latest) — 2014-04-22
- 0.0.1 — 2014-04-20

## README

# varcache

A pure memory based cache for Node.js

## Installation

```
npm install varcache
```

## Usage

```js
var cache = require('varcache');

// Storing an item in the cache.
cache.set('test', 'Something quite awesome!');

// Getting an item from the cache.
var test = cache.get('test');

// Deleting an item from the cache.
cache.delete('test');

// Storing an item in the cache with expiration and callbacks.
cache.set(
	'test',
	'Semething even more awesome...',
	5000,
	function (key, data) {
		console.log('Just stored "' + key + '" with:');
		console.log(data);
	},
	function (key) {
		console.log('"' + key + '" just expired.');
	}
);
```

This should print this:

```
Something quite awesome!
Just stored "test" with:
Semething even more awesome...

... 5 seconds later ...

"test" just expired.
```

## Available functions

* [clear](#clear)
* [debug](#debug)
* [delete](#delete)
* [get](#get)
* [hitCount](#hitCount)
* [hitLog](#hitLog)
* [keys](#keys)
* [keysCount](#keysCount)
* [missCount](#missCount)
* [missLog](#missLog)
* [recordHits](#recordHits)
* [recordMisses](#recordMisses)
* [set](#set)

# Functions

## clear

Clears the entire cache hold of entries.

Available parameters:

* callback (function)

Simple call:

```js
cache.clear();
```

Can be extended to trigger a callback after the items are cleared.

```js
cache.clear(function () {
	console.log('Cache is not cleared.');
});
```

## debug

Enables, or disables, debug mode, which logs evething it does to the console.

Available parameters:

* value (boolean)

Simple call:

```js
cache.debug();
```

Can be extended to specify the boolean value to turn it on or off.

```js
cache.debug(false);
```

## delete

Deletes an entry by-key or several items if an array of keys are passed.

Available parameters:

* key/keys (string/array)
* callback (function)

Simple call:

```js
cache.delete('test-key-1');
```

Or with an array of keys:

```js
cache.delete([ 'test-key-2', 'test-key-3' ]);
```

Can be extended to trigger a callback for each key which is deleted.

```js
cache.delete('test-key-1', function (key) {
	console.log('Just deleted key: "' + key + '".');
});
```

## get

Gets the stored value for the given key in the cache.

Available parameters:

* key (string)
* callbackHit (function)
* callbackMiss (function)

Simple call:

```js
var value = cache.get('key');
```

Can be extended to trigger callbacks for both hit and miss.

```js
var value = cache.get(
	'key',
	function (key, data) {
		console.log('Found key: "' + key + '" with value:');
		console.log(data);
	},
	function (key) {
		console.log('No data cached for key: "' + key + '".');
	}
);
```

## hitCount

Returns the number of key-hits recorded, which is how many times data has been
requested from the cache. This requires that the recordHist() function has been
called.

Available parameters:

* callback (function)

Simple call:

```js
var count = cache.hitCount();
```

Can be extended to trigger a callback with the number.

```js
cache.hitCount(function (count) {
	console.log('Total hits for keys are: ' + count.toString());
});
```

## hitLog

Returns the log of hits, which returns the entire log for every hit against the
cache. This requires that the recordHist() function has been called.

Available parameters:

* callback (function)

Simple call:

```js
var hits = cache.hitLog();
```

Can be extended to trigger a callback with the log.

```js
cache.hitLog(function (hits) {
	console.log(hits);
});
```

## keys

Returns all stored keys, which is convenient for management

Available parameters:

* callback (function)

Simple call:

```js
var keys = cache.keys();
```

Can be extended to trigger a callback with the keys.

```js
cache.keys(function (keys) {
	console.log(keys);
});
```

## keysCount

Returns the number of keys stored.

Available parameters:

* callback (function)

Simple call:

```js
var count = cache.keysCount();
```

Can be extended to trigger a callback with the number.

```js
cache.keysCount(function (count) {
	console.log(count);
});
```

## missCount

Returns the number of key-misses recorded, which is how many times data has been
requested without existing in the cache. This requires that the recordMisses()
function has been called.

Available parameters:

* callback (function)

Simple call:

```js
var count = cache.missCount();
```

Can be extended to trigger a callback with the number.

```js
cache.missCount(function (count) {
	console.log('Total misses for keys are: ' + count.toString());
});
```

## missLog

Returns the log of misses, which returns the entire log for every miss against
the cache. This requires that the recordMisses() function has been called.

Available parameters:

* callback (function)

Simple call:

```js
var misses = cache.missLog();
```

Can be extended to trigger a callback with the log.

```js
cache.missLog(function (misses) {
	console.log(misses);
});
```

## recordHits

Enables, or disables, recording of hits in the cache. Default for this is set
to false.

Available parameters:

* value (boolean)

Simple call:

```js
cache.recordHits();
```

Can be extended to specify the boolean value to turn it on or off.

```js
cache.recordHits(false);
```

## recordMisses

Enables, or disables, recording of misses in the cache. Default for this is set
to false.

Available parameters:

* value (boolean)

Simple call:

```js
cache.recordMisses();
```

Can be extended to specify the boolean value to turn it on or off.

```js
cache.recordMisses(false);
```

## set

Creates, or updates, an item in the cache with all the fixins.

Available parameters:

* key (string)
* data (any)
* ttl (int)
* callbackSet (function)
* callbackExpires (function)

Simple call:

```js
cache.set('key', 'value');
```

This stores the "value" by the "key" forever. Can be extended to include how
many milliseconds the value is to be stored in the cache.

```js
cache.set('key', 'value', 5000);
```

This will store the data for 5 seconds. If you set 0, the data is stored
without an expiration. Can also be extended to trigger callbacks when the data
is set and when the data expires.

```js
cache.set(
	'key',
	'value',
	5000,
	function (record) {
		console.log('This is the newly created record, which contains some metadata:');
		console.log(record);
	},
	function (key) {
		console.log('The entry with key: "' + key + '" just expired.');
	}
);
```

---
_Source: https://npm.io/package/varcache · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
