Typeahead Bloodhoud 在 TTL 超时后不会清除缓存



我对Typeahead的引擎Bloodhound进行了以下设置。尽管我已经将TTL设置为15秒,但刷新不会发生。我等待的时间比这长得多,并进行了新的搜索,但服务器上的hint方法没有执行。

var users = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.whitespace,
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  prefetch: {
    url: "/Worker/HintUser",
    cache: true,
    ttl: 15000
  },
  remote: {
    wildcard: '',
    rateLimitWait: 500,
    url: "/Worker/HintUser",
    transform: function (response) {
      return $.map(response.results, function (element) {
        return { value: element }
      });
    }
  }
});
$("#remoteFetch").typeahead(
{ hint: true, highlight: true, minLength: 1 },
{ name: 'users', source: users });

缓存是否存在已知问题?当我将缓存设置为false时,我使它按预期工作。但随后,它呼吁每次搜索都要打破从本地开始的点。

我该怎么做才能让Bloodhound在几秒钟内缓存这些值,然后将其释放到遗忘状态,获取新的、新鲜的值?

这可能很晚了,但我想好了如何解决这个问题。显然,在typeahead js文件中,作者明确表示要使用现在生效的strict。因此,当使用数字或毫秒时,需要对PersistentStorage.isExpired函数进行一些修改。同样重要的是要记住,ttl是通过将您设置的任何时间添加到new Date().getTime()返回的值中来设置的,该值以毫秒表示

// This is the original 
            isExpired: function(key) {
                var ttl = decode(this.ls.getItem(this._ttlKey(key)));
                return _.isNumber(ttl) && now() > ttl ? true : false;
            }
// And this is my modification
            isExpired: function( key ) {
                var endTime = parseInt( decode(
                    this.ls.getItem(
                        this._ttlKey( key )
                    ), 10 )
                );
                var currentTime = parseInt( now(), 10 ),
                    expired = ( ( _.isNumber( endTime ) ) && ( endTime < currentTime ) ) ? true : false;
                return expired;
            }

最新更新