###Cache Clearance

After you have familiarized yourself with the [Redis caching library](https://github.com/theorchard/redis-lib),  have [set up this repo](install.md), and have read up on redis-lib's [cache clearance](https://github.com/theorchard/redis-lib/blob/master/cache-clearance.md) you are ready to clear some cache entries.

The `RedisClass` creates and manages cache entries. The `Buster` class will handle deleting those entries. Both classed are packaged in the redis-lib. Buster sends messages from a given system (OA, ALW, Skynet) to an SQS queue, and, in the redis-api (i.e., this repo), another Buster will delete cache entries.

On the receiving end, in redis-api, a NodeJS script is doing the SQS management. Here's how you set everything up.

####0. Set up the listener

Assuming you have [NodeJS](http://nodejs.org/) installed and have [filled out your config file](install.md#8-configure) (`cache,` `loggly` and `aws` keys are of importance here) you can get the requisite node modules. From the `node` directory:

```
$ npm i
```

Then start the node listener:

```
$ node cache_listener.js -v
```

The -v flags is for verbose, as you might have guessed. It's optional, too, not surprisingly.

When developing in your secondary repo (oa, etc.) you can keep this listener running in the background. By following the Redis [cache clearance](https://github.com/theorchard/redis-lib/blob/master/cache-clearance.md) instructions, you can send a message to your SQS development queue; the Node listener will pick up the job and then run local php scripts to delete the relevant keys; and, finally, Node logs the results to your dev Loggly account. The whole process should happen in under a second.

####1. Generate Test Keys

If you would like some test keys to work against, you can navigate to redis-lib directory, rename the `/vendor/redis-lib/redis-lib/config/config.shadow.json` file to `config.json` and add your redis dev instance to the `make_keys` json object:


```javascript
//only an example
{
    "test": {
        "redis_host": "",
        "redis_port": null
    },
    "make_keys": {
        "redis_host": "127.0.0.1",
        "redis_port": 6380
    }
}
```

Then, from the `vendor/redis-lib/redis-lib/test/` directory:

```
$ php MakeKeys.php
```

This will populate your redis dev instance with 1000 dev keys.

####2. An example

In your application, you've deleted a record associated with vendor 1234.

```php
//fictional code only. has nothing to do with redis yet
$recordId = 1;
$vendorId = 1234;
$tag = 'foo';
if ($this->record->delete($recordId)) {
    $this->view->display('Record deleted');
}

```

However, that record is cached as part of a VAPI response. The cache keys you need to delete has been tagged with the vendor id (1234) and the cache tag 'foo.' In your application, you would simply add the following code:

```php
$buster = new \Buster($config); //for $config settings, see redis-lib docs
$recordId = 1;
$vendorId = 1234;
$tag = 'foo';
if ($this->record->delete($recordId)) {
    $buster->send($vendorId, array($tag)); //delete vendor id 1234 and tag 'foo'
    $this->view->display('Record deleted');
}
```

That's it.




