【问题标题】:How to add caching to my MVC Application and WCF Service如何将缓存添加到我的 MVC 应用程序和 WCF 服务
【发布时间】:2015-11-16 11:43:51
【问题描述】:

我有一个 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);
        }
    }
}

【问题讨论】:

标签: c# asp.net-mvc wcf caching sqlcachedependency


【解决方案1】:

要缓存来自 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");

【讨论】:

    【解决方案2】:

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

    在 global.asax.cs

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

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

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

    那么当数据对象改变时你需要一个更新方法。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多