【发布时间】:2018-03-08 21:36:56
【问题描述】:
我在用户控制页面中有一个 AJAX 自动完成搜索,它在 document.ready 上被调用。我们对 Web 服务进行 AJAX 调用,该服务从数据库中获取数据(大约 90,000 个),将数据插入缓存,将数据返回到 JavaScript 并添加到数组中。
它首先从数据库中获取数据,然后将数据插入缓存后,每次从缓存中获取数据。当我们在文本框上键入内容时,它会将文本框的文本与数组匹配并显示列表。要从存储过程中获取 90,000 个项目,在本地服务器中需要 2 秒。
但在实时服务器上,从存储过程中获取数据大约需要 40 秒。同样,从缓存中获取数据也需要同样的时间。如何减少时间并提高性能?
AJAX 调用:
var locationSearchListData = [];
var termTemplate = "<span class='ui-autocomplete-term'>%s</span>";
var postData = '{cultureId : "DE"}';
// Ajax call to run webservice's methods.
$.ajax({
url: "/asmx/SearchLocations.asmx/GetAutoCompleteSearchLocations",
type: "POST",
data: postData,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (responseData) {
if (responseData != undefined && responseData != null && responseData.d.length > 0) {
for (i = 0; i < responseData.d.length; i++) {
// Add resopnse data in location search lis, this list is used as a source for autocomplete textbox.
locationSearchListData.push({
label: responseData.d[i].locationData,
latitude: responseData.d[i].latitude,
longitude: responseData.d[i].longitude
});
}
}
网络服务:
[ScriptMethod]
[WebMethod(Description = "Provides instant search suggestions")]
public List<GeoLocationObject> GetAutoCompleteSearchLocations(string cultureId)
{
SqlDatabase database = new SqlDatabase(WebConfigurationManager.ConnectionStrings["MasterDB"].ConnectionString);
string databaseName = WebConfigurationManager.AppSettings["databaseName"];
// Key to identify location search data in cache
string cacheKey = "auto_complete_result";
// List to store locations
List<GeoLocationObject> lstGeolocationObject = new List<GeoLocationObject>();
// If location data is present in cache then return data from cache.
if (Context.Cache[cacheKey] is List<GeoLocationObject>)
{
return Context.Cache[cacheKey] as List<GeoLocationObject>;
}
else // If data is not present in cache, get data from db and add into cache.
{
// Call method GetAutoCompleteSearchLocations of LocationManager to get list of geo location object.
lstGeolocationObject = LocationManager.GetAutoCompleteSearchLocations(database, cultureId);
// Checking if lstGeolocationObject is not null
// If its not null then adding the lstGeolocationObject in the cache
if (lstGeolocationObject.Count > 0)
{
// Add locationdata in cache.
// Removed sqlcache dependency.
Context.Cache.Insert(cacheKey,
lstGeolocationObject,
null,
Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable,
null);
}
// Return geolocation data list
return lstGeolocationObject;
}
} // GetAutoCompleteSearchLocations
【问题讨论】:
-
与其写一个pad,为什么不把代码放在有问题的地方呢?如果您真的需要帮助,我们会更好......
-
请检查代码。
-
我的建议是:不要提前下载所有 9000 条记录,这会浪费大量带宽,因为在页面的生命周期中可能只使用了一小部分。而是将自动完成设置为直接向服务器发出请求,传入搜索词,然后只需查询数据库以查找与该特定项目的匹配项。然后,您应该每次都会收到一个很小的请求和响应,因此希望时间延迟会小得多。
-
请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
标签: javascript asp.net ajax web-services caching