Skip to content

Commit f47dd18

Browse files
committed
Initial commit
0 parents  commit f47dd18

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

Cache.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Caching interface for Hapi
3+
*/
4+
const client = require('redis');
5+
6+
class Cache () {
7+
8+
/**
9+
* Set up general cache
10+
* @return {void}
11+
*/
12+
constructor (config) {
13+
this.config = config;
14+
this.cacheClient = client.createClient(config.redisUrl);
15+
}
16+
17+
/**
18+
* Set a value to cache
19+
* @param {String} key Key to store value to
20+
* @param {String} value Value to store
21+
* @param {Number} expiresIn Milliseconds after which the cached value gets deleted
22+
* @param {Function} callback Callback
23+
* @return {void}
24+
*/
25+
set (key, value, expiresIn = this.config.expiresIn, callback = () => {}) {
26+
value = JSON.stringify(value);
27+
28+
cacheClient.set(key, value, 'PX', expiresIn, (error) => {
29+
if (error) {
30+
throw error;
31+
}
32+
33+
callback && callback();
34+
});
35+
}
36+
37+
/**
38+
* Get a specific value from the store
39+
* @param {String} key Key to get
40+
* @param {Function} callback Function to fire after cached value was retrieved
41+
* @return {void}
42+
*/
43+
get (key, callback) {
44+
45+
cacheClient.get(key, (error, value) => {
46+
try {
47+
value = JSON.parse(value);
48+
} catch (error) {}
49+
50+
callback && callback(error, value);
51+
});
52+
}
53+
}
54+
55+
module.exports = Cache;

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# HAPI INIT - Redis Cache plugin
2+
3+
This plugin makes it easy to Cache any sort of data in Redis.
4+
5+
## Usage
6+
7+
Install via npm:
8+
9+
$ npm install --save hapi-init-redis
10+
11+
### How to use
12+
13+
const Cache = require('hapi-init-redis');
14+
const cache = new Cache({
15+
redisUrl:
16+
});
17+
18+
// Setting an object in cache
19+
cache.set('foo', 'bar', 1000 * 60 * 60, () => {
20+
console.log('Data set in Cache');
21+
});
22+
23+
// Getting the object from Cache
24+
cache.get('foo', (error, data) => {
25+
console.log('Data found:', data);
26+
});

package.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "hapi-init-redis",
3+
"version": "1.0.0",
4+
"description": "Hapi Caching Interface with redis",
5+
"main": "Cache.js",
6+
"author": "Hans Christian Reinl <[email protected]>",
7+
"license": "MIT",
8+
"dependencies": {
9+
"redis": "^2.7.1"
10+
}
11+
}

yarn.lock

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
double-ended-queue@^2.1.0-0:
6+
version "2.1.0-0"
7+
resolved "https://registry.yarnpkg.com/double-ended-queue/-/double-ended-queue-2.1.0-0.tgz#103d3527fd31528f40188130c841efdd78264e5c"
8+
9+
redis-commands@^1.2.0:
10+
version "1.3.1"
11+
resolved "https://registry.yarnpkg.com/redis-commands/-/redis-commands-1.3.1.tgz#81d826f45fa9c8b2011f4cd7a0fe597d241d442b"
12+
13+
redis-parser@^2.5.0:
14+
version "2.6.0"
15+
resolved "https://registry.yarnpkg.com/redis-parser/-/redis-parser-2.6.0.tgz#52ed09dacac108f1a631c07e9b69941e7a19504b"
16+
17+
redis@^2.7.1:
18+
version "2.7.1"
19+
resolved "https://registry.yarnpkg.com/redis/-/redis-2.7.1.tgz#7d56f7875b98b20410b71539f1d878ed58ebf46a"
20+
dependencies:
21+
double-ended-queue "^2.1.0-0"
22+
redis-commands "^1.2.0"
23+
redis-parser "^2.5.0"

0 commit comments

Comments
 (0)