Skip to content

Commit

Permalink
Remove string catbox strategy support. For #1703
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Jun 9, 2014
1 parent fe9d5c6 commit cf74bb2
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 48 deletions.
10 changes: 5 additions & 5 deletions docs/Reference.md
Expand Up @@ -145,11 +145,11 @@ When creating a server instance, the following options configure the server's be
should use `plugins[name]`. Note the difference between `server.settings.app` which is used to store configuration value
and `server.app` which is meant for storing run-time state.

- <a name="server.config.cache"></a>`cache` - determines the type of server-side cache used. Every server includes a default
cache for storing and application state. By default, a simple memory-based cache is created which has a limited capacity.
**hapi** uses [**catbox** module documentation](https://github.com/spumko/catbox#client) as its cache implementation which
includes support for Redis, MongoDB, Memcached, and Riak. Caching is only utilized if methods and plugins explicitly store
their state in the cache. The server cache configuration only defines the store itself. `cache` can be assigned:
- <a name="server.config.cache"></a>`cache` - sets up server-side caching. Every server includes a default cache for storing
application state. By default, a simple memory-based cache is created which has limited capacity and capabilities. **hapi**
uses [**catbox**](https://github.com/spumko/catbox) for its cache which includes support for Redis, MongoDB, Memcached, and
Riak. Caching is only utilized if methods and plugins explicitly store their state in the cache. The server cache
configuration only defines the storage container itself. `cache` can be assigned:
- a string with the cache engine module name (e.g. `'catbox-memory'`, `'catbox-redis'`).
- a configuration object with the following options:
- `engine` -
Expand Down
4 changes: 1 addition & 3 deletions examples/cache.js
Expand Up @@ -20,9 +20,7 @@ internals.profile = function (request, reply) {
internals.main = function () {

var config = {
cache: {
engine: 'catbox-memory'
}
cache: require('catbox-memory')
};

var server = new Hapi.Server(8000, config);
Expand Down
11 changes: 0 additions & 11 deletions lib/defaults.js
Expand Up @@ -97,10 +97,6 @@ exports.server = {

labels: [], // Server pack labels

// Cache

cache: null, // Always created (null defaults to exports.cache)

// Optional components

cors: false, // CORS headers on responses and OPTIONS requests (defaults: exports.cors): false -> null, true -> defaults, {} -> override defaults
Expand Down Expand Up @@ -152,13 +148,6 @@ exports.security = {
};


// Server caching

exports.cache = { // Primary cache configuration (see catbox)
engine: 'catbox-memory'
};


// State management

exports.state = {
Expand Down
22 changes: 18 additions & 4 deletions lib/pack.js
Expand Up @@ -4,6 +4,7 @@ var Path = require('path');
var Events = require('events');
var Async = require('async');
var Catbox = require('catbox');
var CatboxMemory = require('catbox-memory');
var Hoek = require('hoek');
var Kilt = require('kilt');
var Server = require('./server');
Expand Down Expand Up @@ -48,7 +49,7 @@ exports = module.exports = internals.Pack = function (options) {
}

if (!this._caches._default) {
this._createCache(Defaults.cache); // Defaults to memory-based
this._createCache({ engine: CatboxMemory }); // Defaults to memory-based
}

this._ext = new Ext(['onPreStart']);
Expand All @@ -59,7 +60,7 @@ exports = module.exports = internals.Pack = function (options) {

internals.Pack.prototype._createCache = function (options) {

if (typeof options !== 'object') {
if (typeof options === 'function') {
options = { engine: options };
}

Expand All @@ -77,7 +78,7 @@ internals.Pack.prototype._createCache = function (options) {
delete settings.engine;
delete settings.shared;

client = new Catbox.Client(require(options.engine), settings);
client = new Catbox.Client(options.engine, settings);
}

this._caches[name] = {
Expand Down Expand Up @@ -684,7 +685,20 @@ internals.Pack.compose = function (manifest /*, [options], callback */) {
Hoek.assert(options, 'Invalid options');
Hoek.assert(typeof callback === 'function', 'Invalid callback');

var pack = new internals.Pack(manifest.pack);
var packSettings = manifest.pack;
if (packSettings.cache &&
(typeof packSettings.cache === 'string' || typeof packSettings.cache.engine === 'string')) {

packSettings = Hoek.clone(packSettings);
if (typeof packSettings.cache === 'string') {
packSettings.cache = require(packSettings.cache);
}
else {
packSettings.cache.engine = require(packSettings.cache.engine);
}
}

var pack = new internals.Pack(packSettings);

// Load servers

Expand Down
30 changes: 17 additions & 13 deletions lib/schema.js
Expand Up @@ -16,18 +16,19 @@ exports.assert = function (type, options, message) {
};


internals.cache = Joi.object({
name: Joi.string().invalid('_default'),
engine: Joi.alternatives([
Joi.string(),
Joi.object(),
Joi.func()
])
.required(),
partition: Joi.string(),
shared: Joi.boolean()
})
.unknown();
internals.cache = Joi.alternatives([
Joi.func(),
Joi.object({
name: Joi.string().invalid('_default'),
engine: Joi.alternatives([
Joi.object(),
Joi.func()
])
.required(),
partition: Joi.string(),
shared: Joi.boolean()
}).unknown()
]);


internals.viewBase = Joi.object({
Expand Down Expand Up @@ -81,7 +82,10 @@ internals.labels = Joi.alternatives([

internals.server = Joi.object({
app: Joi.object().allow(null),
cache: Joi.alternatives(Joi.string(), internals.cache, Joi.array().includes(internals.cache)).allow(null),
cache: Joi.alternatives([
internals.cache,
Joi.array().includes(internals.cache).min(1)
]).allow(null),
cors: Joi.object({
origin: Joi.array(),
isOriginExposed: Joi.boolean(),
Expand Down
4 changes: 2 additions & 2 deletions test/cache.js
Expand Up @@ -23,7 +23,7 @@ describe('Cache', function () {

it('returns max-age value when route uses default cache rules', function (done) {

var server = new Hapi.Server(0, { cache: [{ engine: 'catbox-memory', name: 'secondary' }] });
var server = new Hapi.Server(0, { cache: [{ engine: require('catbox-memory'), name: 'secondary' }] });

server.method('profile', function (id, next) {

Expand Down Expand Up @@ -116,7 +116,7 @@ describe('Cache', function () {

it('caches using non default cache', function (done) {

var server = new Hapi.Server(0, { cache: { name: 'primary', engine: 'catbox-memory' } });
var server = new Hapi.Server(0, { cache: { name: 'primary', engine: require('catbox-memory') } });
var defaults = server.cache('a', { expiresIn: 2000 });
var primary = server.cache('a', { expiresIn: 2000, cache: 'primary' });

Expand Down
10 changes: 5 additions & 5 deletions test/methods.js
Expand Up @@ -382,7 +382,7 @@ describe('Method', function () {

var fn = function () {

var server = new Hapi.Server({ cache: 'catbox-memory' });
var server = new Hapi.Server({ cache: require('catbox-memory') });
server.method('user', function () { }, { cache: { x: 'y' } });
};
expect(fn).to.throw(Error);
Expand Down Expand Up @@ -446,7 +446,7 @@ describe('Method', function () {

it('returns a valid result when calling a method using the cache', function (done) {

var server = new Hapi.Server(0, { cache: 'catbox-memory' });
var server = new Hapi.Server(0, { cache: require('catbox-memory') });

var gen = 0;
server.method('user', function (id, next) { return next(null, { id: id, gen: ++gen }); }, { cache: { expiresIn: 2000 } });
Expand All @@ -470,7 +470,7 @@ describe('Method', function () {

it('supports empty key method', function (done) {

var server = new Hapi.Server(0, { cache: 'catbox-memory' });
var server = new Hapi.Server(0, { cache: require('catbox-memory') });

var gen = 0;
var terms = 'I agree to give my house';
Expand All @@ -494,7 +494,7 @@ describe('Method', function () {

it('returns valid results when calling a method (with different keys) using the cache', function (done) {

var server = new Hapi.Server(0, { cache: 'catbox-memory' });
var server = new Hapi.Server(0, { cache: require('catbox-memory') });
var gen = 0;
server.method('user', function (id, next) { return next(null, { id: id, gen: ++gen }); }, { cache: { expiresIn: 2000 } });
server.start(function () {
Expand All @@ -517,7 +517,7 @@ describe('Method', function () {

it('returns new object (not cached) when second key generation fails when using the cache', function (done) {

var server = new Hapi.Server(0, { cache: 'catbox-memory' });
var server = new Hapi.Server(0, { cache: require('catbox-memory') });
var id1 = Math.random();
var gen = 0;
var method = function (id, next) {
Expand Down
2 changes: 1 addition & 1 deletion test/pack.js
Expand Up @@ -178,7 +178,7 @@ describe('Pack', function () {
expect(function () {

var pack = new Hapi.Pack();
pack.server({ cache: 'catbox-memory', labels: ['a', 'b', 'c'] });
pack.server({ cache: require('catbox-memory'), labels: ['a', 'b', 'c'] });
}).to.throw('Cannot configure server cache in a pack member');
done();
});
Expand Down
2 changes: 1 addition & 1 deletion test/schema.js
Expand Up @@ -73,7 +73,7 @@ describe('Schema', function () {

expect(function () {

var server = new Hapi.Server({ cache: 'catbox-memory' });
var server = new Hapi.Server({ cache: require('catbox-memory') });
}).to.not.throw();
done();
});
Expand Down
6 changes: 3 additions & 3 deletions test/server.js
Expand Up @@ -232,7 +232,7 @@ describe('Server', function () {

it('provisions a server cache with custom partition', function (done) {

var server = new Hapi.Server(0, { cache: { engine: 'catbox-memory', partition: 'hapi-test-other' } });
var server = new Hapi.Server(0, { cache: { engine: require('catbox-memory'), partition: 'hapi-test-other' } });
var cache = server.cache('test', { expiresIn: 1000 });
server.start(function () {

Expand Down Expand Up @@ -460,7 +460,7 @@ describe('Server', function () {

it('reuses the same cache segment', function (done) {

var server = new Hapi.Server({ cache: { engine: 'catbox-memory', shared: true } });
var server = new Hapi.Server({ cache: { engine: require('catbox-memory'), shared: true } });
expect(function () {

var a1 = server.cache('a', { expiresIn: 1000 });
Expand Down Expand Up @@ -586,7 +586,7 @@ describe('Server', function () {

var fn = function () {

var server = new Hapi.Server({ cache: 'catbox-memory', something: false });
var server = new Hapi.Server({ cache: require('catbox-memory'), something: false });
};

expect(fn).throws(Error);
Expand Down

0 comments on commit cf74bb2

Please sign in to comment.