【问题标题】:Twitter jQuery typeahead - How to remove the cacheTwitter jQuery typeahead - 如何删除缓存
【发布时间】:2014-02-24 20:50:05
【问题描述】:

我做了一个这样的查询...第一次,它运行了过滤器...很酷,它工作...

但现在有更多条目,而且似乎快用完了缓存。如何强制它停止使用缓存?

var countries = new Bloodhound({
    datumTokenizer: function (d) { return Bloodhound.tokenizers.whitespace(d.name); },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    limit: 10,
    prefetch: {
        url: Url + '/Country/JsonList',
        filter: function (list) {
            return $.map(list, function (country) { return { name: country.Name }; });
        }
    }
});

countries.initialize();

$('.countries.typeahead').typeahead(null, {
    displayKey: 'name',
    source: countries.ttAdapter()
});

【问题讨论】:

  • 奇怪地清除 Google Chrome 缓存并重新启动 Chrome 似乎也没有清除预取缓存

标签: jquery caching twitter-typeahead


【解决方案1】:

我认为这比公认的答案更好:

var countries = new Bloodhound({
    datumTokenizer: function (d) { return Bloodhound.tokenizers.whitespace(d.name); },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    limit: 10,
    prefetch: {
        url: Url + '/Country/JsonList',
        filter: function (list) {
            return $.map(list, function (country) { return { name: country.Name }; });
        },
        cache: false //NEW!
    }
});

countries.initialize();

$('.countries.typeahead').typeahead(null, {
    displayKey: 'name',
    source: countries.ttAdapter() //NOTE: .ttAdapter() is deprecated and will be removed in V1
});

【讨论】:

  • Doh,在发现它缓存了结果之前,我几乎失去了理智。
【解决方案2】:

ttl 属性添加到过滤器...并将其设置为 1 而不是 0。

var countries = new Bloodhound({
    datumTokenizer: function (d) { return Bloodhound.tokenizers.whitespace(d.name); },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    limit: 10,
    prefetch: {
        ttl: 1,
        url: Url + '/Country/JsonList',
        filter: function (list) {
            return $.map(list, function (country) { return { name: country.Name }; });
        }
    }
});

countries.initialize();

$('.countries.typeahead').typeahead(null, {
    displayKey: 'name',
    source: countries.ttAdapter()
});

https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md

【讨论】:

  • 您能否详细说明为什么“不是 0”?我正在研究预取大型数据集(很高兴在每次页面加载时都这样做)并且希望完全禁用缓存以避免遇到可能导致脚本崩溃的本地存储限制。
  • 不知道他们为什么说“1 不是 0”。 ttl 只是一个添加到now() 毫秒的数字。然后在读取时将结果与now() 进行比较以检查是否过期。 0 作品。来源:github.com/twitter/typeahead.js/blob/…
  • 它现在可能可以工作,也许这与旧版本的猎犬有关,也许是一个错误。谁知道呢!
  • "0" 表示不缓存。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-29
  • 2014-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多