Jquery twitter引导型typeahead缓存json结果

Jquery twitter引导型typeahead缓存json结果,jquery,json,twitter-bootstrap,Jquery,Json,Twitter Bootstrap,我正在我的应用程序中使用twitter引导库,这是一次很好的体验。现在我正在为autocomplete实现引导typeahad。 在这里,我在缓存结果以避免对服务器的许多请求方面遇到了问题。在网上做研究的时候,我发现这个库也被称为TwitterTypeahead。这似乎与引导程序上的非常不同。我错了吗 第一种选择似乎最适合预期的目标,但我有以下问题: 如何使用预回迁?这必须是预先存在的json文件,或者可能是pageload上对服务器的第一个请求 是否有使用预回迁和远程回迁的工作示例 仅供

我正在我的应用程序中使用twitter引导库,这是一次很好的体验。现在我正在为autocomplete实现引导typeahad。 在这里,我在缓存结果以避免对服务器的许多请求方面遇到了问题。在网上做研究的时候,我发现这个库也被称为TwitterTypeahead。这似乎与引导程序上的非常不同。我错了吗

第一种选择似乎最适合预期的目标,但我有以下问题:

  • 如何使用预回迁?这必须是预先存在的json文件,或者可能是pageload上对服务器的第一个请求

    是否有使用预回迁和远程回迁的工作示例

仅供参考:这是我使用TwitterBootTrapTypeahead(不是TwitterTypeahead)的实际代码

$searchbox.typeahead(
{
最小长度:2,
来源:功能(查询、流程){
$SearchID.val(“”);
人员=[];
map={};
$.getJSON(“App_Services/MyService.svc/GetData”,{max:100,条件:query},函数(数据){
$。每个(数据、功能(i、个人){
map[person.name]=个人;
persons.push(person.name);
});
过程(人);
});
},
更新程序:函数(项){
所选=映射[项目].id;
//警报(选定)
退货项目;
},
匹配器:功能(项目){
if(item.toLowerCase().indexOf(this.query.trim().toLowerCase())!=-1){
返回true;
}
},
分拣机:功能(项目){
returnitems.sort();
},
荧光灯:功能(项目){
var regex=new RegExp(“(“+this.query+”)”,“gi”);
返回“”+项。替换(正则表达式“$1”+”;
}
})

我只是想澄清一下。我试图做的是创建一个结果缓存,如果在缓存中找不到匹配项,则仅使用服务器。

为了避免在您的情况下向服务器发出许多请求,在服务器端输出json时,应该在http头中添加缓存

在PHP中,它喜欢:

$time = time();  
$interval = 3600 * 24 * 60; //cache for 60 days  
header('Last-Modified: '.gmdate('r',$time));  
header('Expires: '.gmdate('r',($time+$interval)));  
header('Cache-Control: max-age='.$interval);  
header('Content-type: application/json');
echo $json;
exit;

为了避免对数据库的大量查询,您应该使用memcache之类的工具缓存频繁查询的结果。

为了避免对服务器的大量请求,您应该在服务器端输出json时在http头中添加缓存

window.query_cache = {};
$searchbox.typeahead({
    minLength: 2,
    source: function(query, process) {
        if (query_cache[query]) {
            process(query_cache[query]); //If cached, use it !
            return;
        }
        $SearchID.val('');
        persons = [];
        map = {};
        $.getJSON("App_Services/MyService.svc/GetData", {
            max: 100,
            criteria: query
        }, function(data) {
            $.each(data, function(i, person) {
                map[person.name] = person;
                persons.push(person.name);
            });
            query_cache[query] = persons; //Add it if it doesn't exist.
            process(persons);
        });
    },
    updater: function(item) {
        selected = map[item].id;
        //alert(selected)
        return item;
    },
    matcher: function(item) {
        if (item.toLowerCase().indexOf(this.query.trim().toLowerCase()) != -1) {
            return true;
        }
    },
    sorter: function(items) {
        return items.sort();
    },
    highlighter: function(item) {
        var regex = new RegExp('(' + this.query + ')', 'gi');
        return '<div>' + item.replace(regex, "<strong>$1</strong>") + '</div>';
    }
})
在PHP中,它喜欢:

$time = time();  
$interval = 3600 * 24 * 60; //cache for 60 days  
header('Last-Modified: '.gmdate('r',$time));  
header('Expires: '.gmdate('r',($time+$interval)));  
header('Cache-Control: max-age='.$interval);  
header('Content-type: application/json');
echo $json;
exit;
为了避免对数据库进行大量查询,您应该使用memcache之类的方法缓存频繁查询的结果。

window.query\u cache={};
window.query_cache = {};
$searchbox.typeahead({
    minLength: 2,
    source: function(query, process) {
        if (query_cache[query]) {
            process(query_cache[query]); //If cached, use it !
            return;
        }
        $SearchID.val('');
        persons = [];
        map = {};
        $.getJSON("App_Services/MyService.svc/GetData", {
            max: 100,
            criteria: query
        }, function(data) {
            $.each(data, function(i, person) {
                map[person.name] = person;
                persons.push(person.name);
            });
            query_cache[query] = persons; //Add it if it doesn't exist.
            process(persons);
        });
    },
    updater: function(item) {
        selected = map[item].id;
        //alert(selected)
        return item;
    },
    matcher: function(item) {
        if (item.toLowerCase().indexOf(this.query.trim().toLowerCase()) != -1) {
            return true;
        }
    },
    sorter: function(items) {
        return items.sort();
    },
    highlighter: function(item) {
        var regex = new RegExp('(' + this.query + ')', 'gi');
        return '<div>' + item.replace(regex, "<strong>$1</strong>") + '</div>';
    }
})
$searchbox.typeahead({ 最小长度:2, 来源:功能(查询、流程){ if(查询\u缓存[query]){ 进程(query_cache[query]);//如果已缓存,请使用它! 返回; } $SearchID.val(“”); 人员=[]; map={}; $.getJSON(“App_Services/MyService.svc/GetData”{ 最高:100, 条件:查询 },函数(数据){ $。每个(数据、功能(i、个人){ map[person.name]=个人; persons.push(person.name); }); query\u cache[query]=persons;//如果不存在则添加。 过程(人); }); }, 更新程序:函数(项){ 所选=映射[项目].id; //警报(选定) 退货项目; }, 匹配器:功能(项目){ if(item.toLowerCase().indexOf(this.query.trim().toLowerCase())!=-1){ 返回true; } }, 分拣机:功能(项目){ returnitems.sort(); }, 荧光灯:功能(项目){ var regex=new RegExp(“(“+this.query+”)”,“gi”); 返回“”+项。替换(正则表达式“$1”+”; } })
这个代码是做什么的

  • 它定义了一个全局变量
    query\u cache
  • 然后它查找缓存的结果。若存在,它将处理这个问题,所以服务器上并没有负载
  • 如果它没有被缓存,它会缓存它,所以下次可以使用它
  • 解决你的问题
  • window.query\u cache={};
    $searchbox.typeahead({
    最小长度:2,
    来源:功能(查询、流程){
    if(查询\u缓存[query]){
    进程(query_cache[query]);//如果已缓存,请使用它!
    返回;
    }
    $SearchID.val(“”);
    人员=[];
    map={};
    $.getJSON(“App_Services/MyService.svc/GetData”{
    最高:100,
    条件:查询
    },函数(数据){
    $。每个(数据、功能(i、个人){
    map[person.name]=个人;
    persons.push(person.name);
    });
    query\u cache[query]=persons;//如果不存在则添加。
    过程(人);
    });
    },
    更新程序:函数(项){
    所选=映射[项目].id;
    //警报(选定)
    退货项目;
    },
    匹配器:功能(项目){
    if(item.toLowerCase().indexOf(this.query.trim().toLowerCase())!=-1){
    返回true;
    }
    },
    分拣机:功能(项目){
    returnitems.sort();
    },
    荧光灯:功能(项目){
    var regex=new RegExp(“(“+this.query+”)”,“gi”);
    返回“”+项。替换(正则表达式“$1”+”;
    }
    })
    
    这个代码是做什么的

  • 它定义了一个全局变量
    query\u cache
  • 然后它查找缓存的结果。若存在,它将处理这个问题,所以服务器上并没有负载
  • 如果它没有被缓存,它会缓存它,所以下次可以使用它
  • 解决你的问题

  • 谢谢你的回复。是C#中的一个应用程序。我对php不是很熟悉。我正在寻找一个解决方案,在打字代码水平。谢谢你的答复。是C#中的一个应用程序。我不是