Twitter jQuery typeahead - 如何移除缓存

7
我进行了这样的查询...第一次,它运行了筛选器...酷,而且还起作用了...
但现在有更多的条目,似乎正在使用缓存。我该如何强制它停止使用缓存?
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似乎也无法清除预取缓存。 - Matthew Lock
2个回答

23

我认为这比被接受的答案更好:

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
});

2
差点就要疯了,终于想明白它缓存了结果。 - Pavel

8
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”吗?我正在考虑预取大型数据集(每次页面加载都可以这样做),并希望完全禁用缓存,以避免撞到本地存储限制,可能会导致脚本崩溃。 - IvanR
不确定为什么他们说“1而不是0”。ttl只是一个数字,它被添加到now()毫秒数中。然后将结果与读取时的now()进行比较以检查是否过期。0也可以使用。来源:https://github.com/twitter/typeahead.js/blob/f835e162ec4551479114fd245c907ae032de692a/src/bloodhound/persistent_storage.js - danneu
现在可能可以工作了,也许这与旧版本的 Blood Hound 有关,也许是一个 bug。谁知道呢! - Jimmyt1988
"0" 表示不缓存。 - Sudheera

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接