Prevents + Commit
rfogel opened this issue · 3 comments
Hello. Im trying to make a cache system using aspect using something like this
export const Cacheable = () => beforeMethod(async (meta) => {
// fetch cache
meta.prevent()
meta.commit()
if (cache)
returns cache
else {
const result = await meta.method.call(meta.scope, ...meta.args)
return result
}
}
I'm obliged to use meta.commit() otherwise async operations does not work. But, using commit and prevent together does not seem to work, since the function is still been called.
I don't know if there's a conceptual error on my logic but is this supposed to work? If not not, what should I do?
Thanks in advanced
Hi, sorry for the late response.
What are you trying do to?
meta.prevent just skips main method from being called.
I see that u're doing an async action. Have u tried to assign meta.result = 1 or meta.result = meta.method.call(meta.scope, ...meta.args)
keep in mind that u don't need to call meta.prevent there.
For instance:
const cached = beforeMethod(meta => {
if(cache) {
this.prevent();
meta.result = cache;
}
// if cache does not exists main method will be invoked as normal
})
// ...
class YourClass {
@cached
someExpensiveAction() {
return this.service.someCall();
}
}
check this link https://github.com/k1r0s/kaop#get-started
It has useful info about the concepts required to make method memoization which are tied to caching too.
To clarify. meta.commit is required only if your call stack requires to wait in order to receive something. But sice u're using promises and either checking or storing cache is not performed by the main method u don't need to use meta.commit.