如何将缓存添加到我的MVC应用程序和WCF服务中

本文关键字:应用程序 WCF 服务 MVC 我的 缓存 添加 | 更新日期: 2023-09-27 18:22:01

我有一个MVC Web应用程序,它基本上查询产品列表的SQL存储过程,我有一层WCF服务层负责数据库查询,有一个按类别获取产品并将数据返回到MVC网格视图的调用。我通过将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);
        }
    }
}

如何将缓存添加到我的MVC应用程序和WCF服务中

要从WCF服务缓存数据,您应该首先有一个缓存层。样本代码:

using System.Runtime.Caching;
public class CacheManager
{
        private static MemoryCache _cache = MemoryCache.Default;
        public static void AddToCache<T>(string key, T value)
        {
            _cache[key] = value;
        }
        public static T GetFromCache<T>(string key)
        {
            return (T)_cache[key];
        }
        public static void RemoveFromCache(string key)
        {
            _cache.Remove(key);
        }
}

然后在数据层中使用它,例如:

public List<Top_100_Result> GetTopProductsByTypeName()
{
    var products = CacheManager.GetFromCache<List<Top_100_Result>>("TOP_100_RESULT");
    //Add to cache if not existed
    if (products == null)
    {
            using (EmbraceEntities ctx = new EmbraceEntities())
            {
                var productObjects = ctx.Top_100(null);
                products = new List<Top_100_Result>(productObjects.Distinct());
                CacheManager.AddToCache<List<Top_100_Result>>("TOP_100_RESULT", products);
            }
    }
   return products;
}

您还应该清除缓存,以便在缓存数据无效时立即刷新数据。

CacheManager.RemoveFromCache("TOP_100_RESULT");

有很多方法可以做到这一点。也许会是:(伪码)

在global.asax.cs

 public static Service1 MyService = new Service1();
 protected void Application_Start()
 {
     Task.CreateNew(()=>mySerivce.Init());

我会在任务中初始化您的服务。在init中,我会读取Entities()并在本地缓存它们。

    GetTopProductsByTypeName()
    {
          return new List<Top_100_Result>(productObjectsCache.Distinct());

那么当数据对象发生更改时,您需要一个更新方法。

 public ActionResult Index()
 {
    List<Top_100_Result> productType = WebHostApplication.MyService.GetTopProductsByTypeName();