idealley / feathers-hooks-rediscache

Set of caching hooks and routes for feathersjs.
MIT License
38 stars 12 forks source link

custom cache key #59

Closed SassNinja closed 5 years ago

SassNinja commented 5 years ago

I'm missing a way to specify a custom cacheKey. Right now the hook does create the key itself using the hook params etc. The problem is I do a lot of query manipulation in my hooks what increase the key's length.

My current setup looks similar to:

return {
    before: {
        find: [lorem(), ipsum(), dolor(), redisBeforeHook()]
    }
    after: {
        find: [lorem(), ipsum(), dolor(), redisAfterHook()]
    }
};

As you can see I've placed both redis hooks at the end of the chain because the cache key wouldn't match otherwise. But I'd prefer to place the redisBeforeHook at the beginning what only works if the cache key is created at the beginning of the after hook chain (before all the query manipulation).

Therefore I need a way to override the default cache key. I'm thinking about adding a custom hook at the beginning of the after hook chain that writes params.cacheKey. But then the redis hook has to check if this param is present and use it instead of the default one.

@idealley would you accept a PR that adds this feature?

idealley commented 5 years ago

Hello, yes please send me a PR with tests if possible. I am sorry these days I am not really reactive. I am changing job and the handover is quite long.

SassNinja commented 5 years ago

I've submitted a PR with tests!

Unfortunately 1 test fails (Cache clearing http routes) if running npm test but it's not related to my changes or my tests. Also fails in master branch.

@idealley ~~are you aware of this? If you like I can try to find out what's up. Irrespective of it I'd appreciate if you take a look at my PR.~~

Ignore my npm test problem – some other project was running on port 3030. After exiting it all tests were passing.