hapi icon indicating copy to clipboard operation
hapi copied to clipboard

Question: Is it OK to use _cache as general purpose cache?

Open paullang opened this issue 13 years ago • 4 comments

I need to do some general purpose caching from my Hapi based application. Since I already have Hapi configured with an open cache connection to Redis, I would prefer to just reuse that.

I confirmed the following will work from my request handler, but is this usage frowned upon since it is not a part of the public API?

request.server.pack._cache.set({segment: 'someKey', id: 'someId'}, 'someValue', ttl, function (err) {
...
});

paullang avatar Jul 06 '13 19:07 paullang

It's not. If you are using the plugin interface, you can use this:

https://github.com/spumko/hapi/blob/master/docs/Reference.md#plugincacheoptions

Otherwise, we can add this interface to the server itself.

Personally, I would just create plugins. They are awesome.

hueniverse avatar Jul 06 '13 20:07 hueniverse

Plugins are awesome and I'll be taking that route.

I created a fun little proof of concept plugin to teach myself how to create my own public plugins as well as how to call the cache client from within a plugin. I also learned a little about lab and server.inject in the process. https://npmjs.org/package/deadbox

I can see value in plugins when properly engineering an application and when sharing generic public plugins, but for private logic, wrapping a local node library can be an extra barrier to entry if somebody new to the framework is just trying to hack together something quickly. If you do add an interface for cache to the server, I suppose you'll need to prepend something to the key name to avoid key name collisions.

paullang avatar Jul 13 '13 06:07 paullang

Yeah, just use another prefix character that's supported by the DBs we use.

hueniverse avatar Jul 13 '13 06:07 hueniverse

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

lock[bot] avatar Jan 09 '20 01:01 lock[bot]