# existence-cache

> A wrapper to expose a simple set/check function for AWS ElastiCache or an in-memory cache

Latest version **1.0.2** (published 2017-03-20) · Apache-2.0 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install existence-cache
pnpm add existence-cache
yarn add existence-cache
bun add existence-cache
```

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2017-03-20 |
| First published | 2017-03-01 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Gary Crye |
| Maintainers | garygsc |

## Links

- npm: https://www.npmjs.com/package/existence-cache
- Repository: https://github.com/byu-oit-appdev/existence-cache
- Homepage: https://github.com/byu-oit-appdev/existence-cache#readme
- Issues: https://github.com/byu-oit-appdev/existence-cache/issues
- npm.io page: https://npm.io/package/existence-cache

## Dependencies (3)

- [redis](https://npm.io/package/redis.md) ^2.6.5
- [bluebird](https://npm.io/package/bluebird.md) ^3.4.7
- [node-cache](https://npm.io/package/node-cache.md) ^4.1.1

## Recent versions

- 1.0.2 (latest) — 2017-03-20
- 1.0.1 — 2017-03-01
- 1.0.0 — 2017-03-01

## README

# existence-cache

An NPM package that uses either ElastiCache or local memory to set and check for the existence of keys

### Installation
```
npm install existence-cache
```

### Usage
You set up configuration when requiring the module.

The parameters it accepts being: `(elasticache_endpoint, elasticache_port, horribleErrorFunc)`, where:
 - `elasticache_endpoint` (Optional) - Endpoint for a Redis-based ElastiCache cluster
 - `elasticache_port` (Optional) - Port for the cluster, defaults to 6379
 - `horribleErrorFunc` (Optional) - Function to call if it cannot connect to the cache (ElastiCache or in local memory)
    - Will run `horribleErrorFunc(message, error)` upon a horrible error if a function is provided.

If `elasticache_endpoint` is not specified, this will default to caching using local memory.

Require it like this:
```
var cache = require('existence-cache')('somecluster.abcde.0001.usw2.cache.amazonaws.com', 6000, postSNSAlert);
```
or just like:
```
var cache = require('existence-cache');
```

Then, use it like this:
```
cache.set("key", 20);
cache.check("key");
```

Where `cache.set` behaves as a synchronous function and its parameters are
 - `key` - A unique string
 - `ttl` - (Optional) Time to live, in seconds.
    - `0` will permanently set the key, negative values will delete it.
    - Defaults to permanently setting the key.
    
And `cache.check` behaves as an asynchronous function and has the parameter: 
 - `key` - A unique string to check if it exists (has been set in the cache and not expired)
 
### Example
```js
var cache = require('existence-cache')(null, null, console.error);
cache.check("15")
    .then(function(){
        console.log("Setting the key in the cache.");
        return Promise.resolve(cache.set("15", 1));
    })
    .then(function(){
        console.log("Made it here!");
        return Promise.resolve(cache.check("15"));
    })
    .then(function(){
        console.log("This code doesn't run, since 15 was in the cache.");
    })
    .catch(console.log);
```

###Some Notes about ElastiCache
The ElastiCache functionality only works with Redis-based ElastiCache Clusters.

ElastiCache is [only accessible from an EC2 instance](http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/WhatIs.Accessing.html) (whether you start that instance directly, or through Elastic Beanstalk).

When setting up your clusters, be sure they are in the same VPC as your EC2 instance(s) and that the security group has the appropriate inbound rule to allow access to ElastiCache.

For further reference, see [Amazon's documentation](http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/GettingStarted.AuthorizeAccess.html).

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