Web Services C#中的结果字符串或LINQ查询的缓存方法



缓存以下代码的最佳方法是什么。因此,从数据库中读取数据并每隔一次从缓存中读取?

这将在一天中从多个域中访问数千次。我也希望在需要时清除缓存,也许是图片。

    // GETAll api/category
    [WebMethod(CacheDuration = 600)]
    public string GetAllCategories()
    {
        using (var db = new nopMass())
        {
            var cats = db.Categories
                        .Where(x => x.ParentCategoryId == 1
                                    && x.Deleted == false
                                    && x.Published == true)
                        .OrderBy(c => c.ParentCategoryId)
                        .ThenBy(c => c.DisplayOrder)
                        .AsEnumerable()
                        .Select(cat => new Category
                        {
                            Id = cat.Id,
                            Name = cat.Name,
                            Description = cat.Description,
                            MetaKeywords = cat.MetaKeywords,
                            MetaDescription = cat.MetaDescription,
                            MetaTitle = cat.MetaTitle,
                            PictureId = cat.PictureId,
                            DisplayOrder = cat.DisplayOrder,
                            CreatedOnUtc = cat.CreatedOnUtc,
                            Product_Category_Mapping = cat.Product_Category_Mapping,
                            ParentCategoryId = cat.ParentCategoryId,
                        })
                        .ToList();
            string XML = "";
            XML += "<Collection>";
            foreach (var item in cats)
            {
                XML += "<Category>";
                XML += "<Id>";
                XML += item.Id.ToString();
                XML += "</Id>";
                XML += "<Name>";
                XML += item.Name;
                XML += "</Name>";
                XML += "<Description>";
                XML += item.Description;
                XML += "</Description>";
                XML += "<MetaKeywords>";
                XML += item.MetaKeywords;
                XML += "</MetaKeywords>";
                XML += "<MetaDescription>";
                XML += item.MetaDescription;
                XML += "</MetaDescription>";
                XML += "<MetaTitle>";
                XML += item.MetaTitle;
                XML += "</MetaTitle>";
                XML += "<PictureUrl>";
                try
                {
                    XML += GetPictureUrl(item.PictureId);
                }
                catch { }
                XML += "</PictureUrl>";
                XML += "<DisplayOrder>";
                XML += item.DisplayOrder.ToString();
                XML += "</DisplayOrder>";
                XML += "<CreatedOnUtc>";
                XML += item.CreatedOnUtc.ToString();
                XML += "</CreatedOnUtc>";
                XML += "<ParentCategoryId>";
                XML += item.ParentCategoryId.ToString();
                XML += "</ParentCategoryId>";
                XML += "</Category>";
            }
            XML += "</Collection>";
            return XML;
        }
    }

您应该阅读:httpruntime.cache

首先,您写下这样的缓存:

HttpRuntime.Cache.Insert(cacheKey, yourListOfObjects, null, DateTime.Now.AddMinutes(10), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);

此代码将您的数据保存10分钟。

要从缓存中读取您可以使用此方法:

public object ReadFromCache(string cacheKey)
    {
            if (HttpRuntime.Cache[cacheKey] != null)
            {
                var cache = HttpRuntime.Cache[cacheKey];
                if (cache != null)
                    return cache;
            }
            return null;
    }

基于您的代码,您可以从高速缓存中读取:

List<Categories> cats = (List<Categories>)ReadFromCache(cachekey);
if(cats == null)
{
   //your code to fetch
}

要清除可以简单的缓存:

foreach (System.Collections.DictionaryEntry cacheItem in HttpRuntime.Cache)
                {
                    HttpRuntime.Cache.Remove(cacheItem.Key.ToString());
                }

希望这会有所帮助。

注意:我没有测试过,但应该起作用

相关内容

  • 没有找到相关文章

最新更新