mirusresearch / catbox-disk Goto Github PK
View Code? Open in Web Editor NEWDisk storage adapter for catbox
License: Other
Disk storage adapter for catbox
License: Other
Firstly, thanks for this great addition to catbox. We've been toying with writing our own, but stumbled across this implementation. ๐
Everything appears to work really well, though the cache ttl on a cache.get
appears to always return NaN. A really quick script I fumbled together below should replicate the issue.
var catbox = require('catbox'),
catboxDisk = require('catbox-disk');
var cache = new catbox.Client(catboxDisk, {
partition: 'test',
cleanEvery: 360000,
cachePath: './cache/'
});
cache.start(function(err){
if(err){
return console.error(err);
}
var key = {
id: 'test-key',
segment: 'test',
};
cache.set(key, {
some: 'data',
yes: 123
}, 3000, function(err){
if(err){ return console.error('Error setting cache').debug(err); }
setInterval(function(){
cache.get(key, function(err, data){
console.log('cache results', err, data);
});
}, 1000);
});
});
Looking in the .json file generated, the TTL seems to be set appropriately, but my results print
cache results null { item: { some: 'data', yes: 123 },
stored: 2017-04-26T21:37:12.486Z,
ttl: NaN }
before then being cleared. It appears the TTL is working as expected, but simply never being returned correctly.
Let me know if you need any further info.
I am using the following code (combining the Hapi caching tutorial with the catbox-disk
documentation) to get started with catbox-disk
before I graduate to something more advanced
const Hapi = require('hapi');
const Disk = require('catbox-disk');
const server = new Hapi.Server({
cache : [{
name : 'diskCache',
engine : Disk,
cachePath : '/some/existing/dir',
cleanEvery: 3600000,
partition : 'cache'
}]
});
const start = async () => {
const add = async (a, b) => {
await Hoek.wait(1000); // Simulate some slow I/O
return Number(a) + Number(b);
};
const sumCache = server.cache({
cache: 'diskCache',
expiresIn: 10 * 1000,
generateFunc: async (id) => {
return await add(id.a, id.b);
},
generateTimeout: 2000
});
server.route({
path: '/add/{a}/{b}',
method: 'GET',
handler: async function (request, h) {
const { a, b } = request.params;
const id = `${a}:${b}`;
return await sumCache.get({ id, a, b });
}
});
await server.start();
console.log('Server running at:', server.info.uri);
};
start();
I get the following error
/Users/punkish/Projects/catbox/node_modules/hapi/lib/config.js:19
throw new Error(Invalid ${type} options ${message.length ? '(' + message.join(' ') + ')' : ''} ${result.error.annotate()}
);
^
Error: Invalid cache options [
{ [1]
"name": "diskCache",
"cachePath": "/Users/punkish/Projects/catbox/diskCache",
"cleanEvery": 3600000,
"partition": "cache",
"engine" [2]: function Disk(options) {\n\n Hoek.assert(\n this.constructor === internals.Connection,\n 'Disk cache client must be instantiated using new'\n );\n\n const settings = Hoek.applyToDefaults(internals.defaults, options);\n\n Hoek.assert(settings.cachePath, 'Missing cachePath value');\n Hoek.assert(\n settings.cleanEvery === parseInt(settings.cleanEvery, 10),\n 'cleanEvery is not an integer'\n );\n\n this.settings = Object.assign({}, internals.defaults, options);\n this.isConnected = false;\n this.cacheCleanerTimeout = null;\n return this;\n}
}
]
[1] "0" must be a Function
[2] "engine" must be an object
at Object.exports.apply (/Users/punkish/Projects/catbox/node_modules/hapi/lib/config.js:19:15)
at module.exports.internals.Core._createCache (/Users/punkish/Projects/catbox/node_modules/hapi/lib/core.js:172:26)
at module.exports.internals.Core._initializeCache (/Users/punkish/Projects/catbox/node_modules/hapi/lib/core.js:140:18)
at new module.exports.internals.Core (/Users/punkish/Projects/catbox/node_modules/hapi/lib/core.js:107:14)
at new module.exports (/Users/punkish/Projects/catbox/node_modules/hapi/lib/server.js:22:18)
at Object. (/Users/punkish/Projects/catbox/index.js:6:16)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
What am I missing?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.