[英]In a C# Web Method is it possible to store data retrieved via Entity Framework for subsequent calls?
我有一个Web方法,可以通过EF6从数据库访问大约8个表。 每当用户更改表单上的选项时,都会调用web方法,以便表单的其他输入反映基于当前选择的可能内容。
表格本身很少会更改,但是每个用户的Web方法可能会被点击10至12次。
我的网络方法的简化版本是:
[WebMethod]
public string GetProducts() {
string output = "";
using (var context = new MyEntity.MyEntities())
{
foreach (MyEntity.MyProduct p in context.MyProducts)
{
/// Perform some logic here to decide if we want the product
output += p.MyProductName + "_";
}
}
if (output != "")
output = output.Substring(0, output.Length - 1);
return output;
}
在这个简化的示例中,是否可以以asmx可以用于后续调用的方式缓存一次product表,然后仅使用该表而不是继续访问数据库?
您可以使用WebMethodAttribute.CacheDuration来缓存Web方法的结果。
启用缓存后,请求和响应至少在缓存期间会保留在服务器的内存中,因此,如果您期望请求或响应很大或者期望请求变化很大,则必须谨慎。
[WebMethod(CacheDuration = 180)]
public string GetProducts() {
有两个问题可能会影响ASP.NET 2.0 Web服务应用程序中的输出缓存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.