Asp.net MVC學習日記十六(資料Cache)

來源:互聯網
上載者:User

1、建立Models/Product.cs

  public class Product
    {
        public string Name { get; set; }
        public decimal Price { get; set; }
    }

2、建立Models/ProductService.cs

  public class ProductService
    {
        public List<Product> GetProducts()
        {
            List<Product> result = Builder<Product>
            .CreateListOfSize(100)
            .Build()
            .ToList();
            return result;
        }
    }

3、Models/CacheWrapper.cs

public class CacheWrapper
    {
        public void PutProducts(List<Product> products)
        {
            Dictionary<string, object> itemsToCache = new Dictionary<string, object>();
            foreach (Product product in products)
            {
                itemsToCache.Add(product.Name, product);
            }
            AddCacheItems(itemsToCache);
        }
        public List<Product> GetProducts(string[] productNames)
        {
            List<Product> results = new List<Product>();
            List<object> cacheItems = GetCacheItems(productNames);
            foreach (object cacheItem in cacheItems)
            {
                results.Add(cacheItem as Product);
            }
            return results.OrderBy(p => p.Name).ToList();
        }

 

        private List<object> GetCacheItems(string[] keys)
        {
            IDictionaryEnumerator theCache = HttpContext.Current.Cache.GetEnumerator();
            List<object> results = new List<object>();
            while (theCache.MoveNext())
            {
                if (keys.Contains(theCache.Key))
                    results.Add(theCache.Value);
            }
            return results;
        }
        private object GetCachItem(string key)
        {
            if (HttpContext.Current.Cache[key] != null)
                return HttpContext.Current.Cache[key];
            return null;
        }
        private void AddCacheItems(Dictionary<string, object> items)
        {
            foreach (KeyValuePair<string, object> item in items)
            {
                AddCacheItem(item.Key, item.Value);
            }
        }
        private void AddCacheItem(string key, object item)
        {
            HttpContext.Current.Cache.Add(key, item, null, //dependencies
            DateTime.MaxValue, //absolute expiration
            new TimeSpan(0, 0, 2, 0), //sliding expiration
            CacheItemPriority.Default, //priority
            null); //callback
        }
    }

 

4、Global.asax

     protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);

            PreLoadProductCatalog();
        }

        private void PreLoadProductCatalog()
        {
            List<Product> products = new ProductService().GetProducts();
            new CacheWrapper().PutProducts(products);
        }

 

5、Controllers/HomeController.cs

  public ActionResult CachedProducts()
        {
            List<Product> products = new CacheWrapper().GetProducts(new[] {"Name1", "Name5", "Name98", "Name39", "Name88", "Name34"});
            return View(products);
        }

6、添加CachedProducts的強型別視圖,且視圖內容為List

 

ok啦

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.