https://github.com/arb/tacky
Server-side response caching for hapi.
https://github.com/arb/tacky
Last synced: about 1 year ago
JSON representation
Server-side response caching for hapi.
- Host: GitHub
- URL: https://github.com/arb/tacky
- Owner: arb
- License: mit
- Created: 2015-04-30T14:22:18.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2018-04-11T00:00:00.000Z (about 8 years ago)
- Last Synced: 2025-03-17T19:52:27.622Z (about 1 year ago)
- Language: JavaScript
- Size: 84 KB
- Stars: 32
- Watchers: 1
- Forks: 5
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README

Logo design by chris.ruppert@gmail.com
Server-side response caching plugin for [hapi](http://hapijs.com/)
[](https://www.npmjs.org/package/tacky)
[](https://travis-ci.org/arb/tacky)
[](https://github.com/continuationlabs/belly-button)
tacky adds a new handler named `cache` that can be used on any route that is a `GET` method. tacky will try to serve a value from the server cache first if present. If the value is not in the server cache, it will call `hydrate()`, reply with the result and then cache the value in the server cache for subsequent requests. tacky stores values in a hapi server cache provision. It does *not* just set the response cache headers.
## Usage
See the [API Reference](https://github.com/arb/tacky/blob/master/API.md)
### Example
_copied from examples/default.js_
```js
const Assert = require('assert');
const Http = require('http');
const Hapi = require('hapi');
const Tacky = require('tacky');
const server = new Hapi.Server();
server.connection({ port: 9001 });
server.register({ register: Tacky }, (err) => {
Assert.ifError(err);
server.route({
method: 'get',
path: '/',
config: {
handler: {
cache: {
hydrate: (request, callback) => {
Http.get('http://www.google.com', (res) => {
const buffers = [];
res.on('data', (chunk) => {
buffers.push(chunk);
});
res.on('end', () => {
callback(null, buffers.join().toString());
});
});
}
}
}
}
});
server.start(() => { console.log('Server started at ' + server.info.uri); });
});
```
When the first request comes in to "/", the `hydrate` method is called. We are getting the Google home page and after 1000 milliseconds, we are calling back with the result. If you make a second request to "/", you should notice the delay isn't there and the response is almost instantaneous. The original response has been cached and sent back to the client. If you are testing with a browser, you should notice that the cache header decrements on each request. tacky will set the client cache header ("cache-control:max-age=3566, must-revalidate") based on the ttl options. The cache header ttl will be randomized so that the server isn't slammed by multiple requests at the same time. The goal is to stagger the cache header expiration across different clients.