简体   繁体   English

使用带有underscore.js的memoize函数

[英]using memoize function with underscore.js

I am trying to cache the result from an ajax call using memoize function from Underscore.js . 我试图使用Ajax调用缓存结果memoize从功能Underscore.js I am not sure of my implementation. 我不确定我的实施情况。 Also how to retrieve back the cached result data using the key. 还有如何使用密钥检索缓存的结果数据。 Below is my implementation: 以下是我的实施:

Javascript code: Javascript代码:

var cdata = $http
.get(HOST_URL + "/v1/report/states")
.success(function(data) {
    //put the result in the angularJs scope object. 
    $scope.states = data;
});

//store the result in the cache.
var cachedResult = _.memoize(
    function() {
        return cdata;
    }, "states");

Is my usage of memoize to store the result of ajax is correct. 我使用memoize来存储ajax的结果是否正确。 Also once it is put in cache, how to retrieve based on the key. 一旦将其放入缓存中,如何基于密钥进行检索。 ie 'states'. 即'国家'。

Let us understand how _.memoize works, it takes a function which needs to be memoized as first argument and caches the result of the function return for given parameter. 让我们理解_.memoize是如何工作的,它需要一个需要被记忆为第一个参数的函数,并缓存给定参数的函数返回结果。 Next time if the memoized function is invoked with same argument it will use cached result and the execution time for the function can be avoided. 下次如果使用相同的参数调用memoized函数,它将使用缓存的结果,并且可以避免函数的执行时间。 So it is very important to reduce the computation time. 因此减少计算时间非常重要。

As mentioned, the above fibonaci function it memoized works perfectly fine as the argument has a primitive type. 如上所述,它所记忆的上述斐波那契函数完全正常,因为参数具有原始类型。

The problem occurs when you have to memoize a function which accepts an object. 当您必须记住接受对象的函数时,会发生此问题。 To solve this, _.memoize accepts an optional argument hashFunction which will be used to hash the input. 为了解决这个问题, _.memoize接受一个可选的参数hashFunction ,它将用于散列输入。 This way you can uniquely identify your objects with your own hash functions. 这样,您可以使用自己的哈希函数唯一标识对象。

The default implementation of _.memoize (using the default hash function) returns the first argument as it is - in the case of JavaScript it will return [Object object] . _.memoize的默认实现(使用默认的哈希函数)返回第一个参数 - 在JavaScript的情况下,它将返回[Object object]

So for eg 所以对于例如

var fn = function (obj){ some computation here..}
var memoizedFn = _.memoize(fn);

memoizedFn({"id":"1"}) // we will get result, and result is cahced now

memoizedFn({"id":"2"}) // we will get cached result which is wrong

why default has function in _.memoize is function(x) {return x} 为什么默认在_.memoize中有函数是函数(x){return x}

the problem can be avoided by passing a hash function 传递哈希函数可以避免这个问题

_.memoize(fn, function(input){return JSON.stringify(input)});

This was a real help for me when I was using _.memoize for a function that was working on arrays arguments. 当我使用_.memoize作为一个处理数组参数的函数时,这对我来说是一个真正的帮助。

Hope this helps many people in their work. 希望这有助于许多人的工作。

_.memoize takes a function: _.memoize需要一个函数:

var fibonacci = _.memoize(function(n) {
  return n < 2 ? n: fibonacci(n - 1) + fibonacci(n - 2);
});

You should understand that this is just an extra wrapper function that makes function that you pass it as an argument smarter( Adds extra mapping object to it ). 您应该明白,这只是一个额外的包装函数,它使您将其作为参数更智能地传递给它的函数(向其添加额外的映射对象)。

In example above function that computes fibonacci number is wrapped around with _.memoize . 在上面的示例中,计算斐波纳契数的函数用_.memoize包裹。 So on every function call ( fibonacci(5) or fibonacci(55555) ) passed argument matched to return value so if you need to call one more time fibonacci(55555) it doesn't need to compute it again. 所以在每个函数调用( fibonacci(5)fibonacci(55555) )传递的参数匹配返回值,所以如果你需要再调用一次fibonacci(55555)它不需要再次计算它。 It just fetches that value from that mapping object that _.memoize provided internally. 它只是从内部提供的_.memoize映射对象中获取该值。

If you are using Angular.js's $http , you probably just want to pass {cache : true} as a second parameter to the get method. 如果您使用的是Angular.js的$http ,您可能只想将{cache : true}作为第二个参数传递给get方法。

To store values using key value pairs, you may want to use $cacheFactory , as described in other answers like here . 为了储存使用键值对的值,你可能需要使用$ cacheFactory ,像在其他的答案中描述这里 Basically: 基本上:

var cache = $cacheFactory('cacheId');
cache.put('states', 'value');
cache.get('states');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM