Closed seanohollaren closed 8 years ago
@seanohollaren Firstly, sorry for replying that late, Github notifications failed for me, and it's today I noticed that issue.
resolvers
are purely about normalization of arguments, before they're provided into caching mechanism and underlying function. This unifies handling of some cases, e.g. if at some argument you expect a number, the default caching mechanism will treat '2'
and 2
differently (as by language they're different values), however if you indicate a Number
resolver for that argument, then it's not the case, as '2'
will be turned to 2
before it reaches caching handling.
Now concerning normalization of cache id, there's not documented option normalizer
(sorry for that, I'll try to address that shortly, and keep this issue open until that's the case).
Through that option you provide a function that's meant to resolve cache id (string) for given call. So in case you're describing it can be:
var memoizedFunction = memoizee(function (anObject, aBool) {
...
}, { normalizer: function (args) {
var anObject = args[0], aBool = args[1];
return anObject.id + aBool.toString();
});
Looks great, thanks!
I'm a huge fan of the built-in stats and cache management tools that memoizee offers, but right now I'm totally puzzled by how I'm supposed to use the resolver functionality.
Lodash and Underscore resolvers are very straight forward. For those, you provide a function which returns the key used for the cache map.
Like this:
const memoizedFunction = _.memoize(nonMemoizedFunction, (anObject, aBool) => anObject.id + aBool.toString() );
Memoizee's resolver functionality, however, is a lot less clear.
To me, it's not apparent from the documentation how to craft a resolver function and it seems that memoizee focuses on argument type as opposed to allowing for arbitrary key construction.
Is it possible to memoize based on a given object's property instead of attempting to serialize the entire object? What about combining that with a second parameter?
For example, given
foo(anObject, aBool)
, I'd like my memoization key to beanObject.id + bool
.Given how important resolvers are to a memoization library functioning as intended, I think the documentation would really benefit from being beefed up with useful examples as opposed to trivial ones.