add storage option for Pivotal Cloud Foundry Redis tile ('p-redis') service

This commit is contained in:
O'Keefe, Gerard (Gerry) 2019-03-03 13:00:52 -07:00
parent f3838ab4a8
commit 902fc8b824
4 changed files with 100 additions and 5 deletions

View File

@ -33,9 +33,7 @@
},
"storage": {
"type": "memcached",
"host": "127.0.0.1",
"port": 11211,
"type": "p-redis",
"expire": 2592000
},

View File

@ -0,0 +1,83 @@
var redis = require('redis');
var winston = require('winston');
// For storing in Pivotal Cloud Foundry p-redis
// options[type] = p-redis
// options[expire] - The time to live for each key set (default never)
var RedisDocumentStore = function(options, client) {
this.expire = options.expire;
if (client) {
winston.info('using predefined redis client');
RedisDocumentStore.client = client;
} else if (!RedisDocumentStore.client) {
winston.info('configuring redis');
RedisDocumentStore.connect(options);
}
};
// Create a connection according to VCAP_SERVICES
RedisDocumentStore.connect = function() {
var vcapServices = JSON.parse(process.env.VCAP_SERVICES);
var p_redis = vcapServices['p-redis'][0];
var index = 0;
RedisDocumentStore.client = redis.createClient(p_redis.credentials.port, p_redis.credentials.host);
RedisDocumentStore.client.auth(p_redis.credentials.password);
RedisDocumentStore.client.on('error', function(err) {
winston.error('redis disconnected', err);
});
RedisDocumentStore.client.select(index, function(err) {
if (err) {
winston.error(
'error connecting to redis index ' + index,
{ error: err }
);
process.exit(1);
}
else {
winston.info('connected to redis on ' + p_redis.credentials.host + ':' + p_redis.credentials.port + '/' + index);
}
});
};
// Save file in a key
RedisDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
var _this = this;
RedisDocumentStore.client.set(key, data, function(err) {
if (err) {
callback(false);
}
else {
if (!skipExpire) {
_this.setExpiration(key);
}
callback(true);
}
});
};
// Expire a key in expire time if set
RedisDocumentStore.prototype.setExpiration = function(key) {
if (this.expire) {
RedisDocumentStore.client.expire(key, this.expire, function(err) {
if (err) {
winston.error('failed to set expiry on key: ' + key);
}
});
}
};
// Get a file from a key
RedisDocumentStore.prototype.get = function(key, callback, skipExpire) {
var _this = this;
RedisDocumentStore.client.get(key, function(err, reply) {
if (!err && !skipExpire) {
_this.setExpiration(key);
}
callback(err ? false : reply);
});
};
module.exports = RedisDocumentStore;

15
manifest.yml Normal file
View File

@ -0,0 +1,15 @@
# cf push -f manifest-dev.yml
---
applications:
- name: hastebin
buildpacks:
- https://github.com/cloudfoundry/nodejs-buildpack.git#v1.6.3
disk_quota: 128M
env:
OPTIMIZE_MEMORY: "true"
instances: 1
memory: 128M
services:
- hastebin-redis
stack: cflinuxfs2

View File

@ -30,8 +30,7 @@
},
"bundledDependencies": [],
"engines": {
"node": "8.1.4",
"npm": "5.2.0"
"node": "8.1.4"
},
"bin": {
"haste-server": "./server.js"