我有一个 MVC Web 应用程序,它基本上查询一个产品列表的 SQL 存储过程,我有一个负责数据库查询的 WCF 服务层,有一个按类别获取产品并将数据返回到 MVC Grid-看法。我已经通过将 outputcache 设置为 3600 的持续时间来设法在应用程序级别进行缓存,这工作正常,但是只有在我对每个产品类别进行初始调用后才缓存数据,我怎样才能使其在启动时保持一致。另外我如何在 WCF 服务层缓存数据。请查看我的代码以了解我到目前为止所拥有的内容。我对MVC相当陌生,请您帮忙。
public class HomeController : Controller
{
[OutputCache(Duration = 3600, Location = OutputCacheLocation.Client, VaryByParam = "none")]
public ActionResult Index()
{
TopProductWCFService.TopProductServiceClient client = new TopProductWCFService.TopProductServiceClient();
List<Top_100_Result> productType = client.GetTopProductsByTypeName();
ViewBag.ProductType = new SelectList(productType.Select(x => x.Product_Type_Name).Distinct().OrderBy(x => x));
return View("Index", productType);
}
[OutputCache(Duration = 3600)]
public ActionResult ProductDescription(string ProductType)
{
TopProductWCFService.TopProductServiceClient client = new TopProductWCFService.TopProductServiceClient();
List<Top_100_Result> productDesctriptionList = client.GetTopProductsByCategory(ProductType).Where(x => x.Product_Type_Name == ProductType).ToList();//new List<Top_100_Result>();
return PartialView("_ProductDescription", productDesctriptionList);
}
}
public class Service1 : ITopProductService
{
//private const string CacheKey = "topProducts";
public List<Top_100_Result> GetTopProductsByTypeName()
{
using (EmbraceEntities ctx = new EmbraceEntities())
{
var productObjects = ctx.Top_100(null);
return new List<Top_100_Result>(productObjects.Distinct());
}
}
public List<Top_100_Result> GetTopProductsByCategory(string productCategory)
{
using (EmbraceEntities ctx = new EmbraceEntities())
{
var productsCategoryList = ctx.Top_100(productCategory);
return new List<Top_100_Result>(productsCategoryList);
}
}
}