我已经阅读了大量关于MVC应用程序中的页面缓存和部分页面缓存的信息。然而,我想知道你将如何缓存数据。

在我的场景中,我将使用LINQ到实体(实体框架)。在第一次调用GetNames(或任何方法)时,我想从数据库中获取数据。我想将结果保存在缓存中,并在第二次调用时使用缓存版本(如果存在)。

谁能举例说明这将如何工作,这应该在哪里实现(模型?),如果它将工作。

我在传统的ASP中见过这种情况。NET应用程序,通常用于非常静态的数据。


当前回答

HttpContext.Current.Cache.Insert("subjectlist", subjectlist);

其他回答

参考系统。并使用System.Web.Caching.Cache

    public string[] GetNames()
    {
      string[] names = Cache["names"] as string[];
      if(names == null) //not in cache
      {
        names = DB.GetNames();
        Cache["names"] = names;
      }
      return names;
    }

有点简化了,但我想应该可以。这不是MVC特定的,我一直使用这种方法缓存数据。

我使用两个类。第一个是缓存核心对象:

public class Cacher<TValue>
    where TValue : class
{
    #region Properties
    private Func<TValue> _init;
    public string Key { get; private set; }
    public TValue Value
    {
        get
        {
            var item = HttpRuntime.Cache.Get(Key) as TValue;
            if (item == null)
            {
                item = _init();
                HttpContext.Current.Cache.Insert(Key, item);
            }
            return item;
        }
    }
    #endregion

    #region Constructor
    public Cacher(string key, Func<TValue> init)
    {
        Key = key;
        _init = init;
    }
    #endregion

    #region Methods
    public void Refresh()
    {
        HttpRuntime.Cache.Remove(Key);
    }
    #endregion
}

第二个是缓存对象列表:

public static class Caches
{
    static Caches()
    {
        Languages = new Cacher<IEnumerable<Language>>("Languages", () =>
                                                          {
                                                              using (var context = new WordsContext())
                                                              {
                                                                  return context.Languages.ToList();
                                                              }
                                                          });
    }
    public static Cacher<IEnumerable<Language>> Languages { get; private set; }
}

扩展@Hrvoje Hudo的回答…

代码:

using System;
using System.Runtime.Caching;

public class InMemoryCache : ICacheService
{
    public TValue Get<TValue>(string cacheKey, int durationInMinutes, Func<TValue> getItemCallback) where TValue : class
    {
        TValue item = MemoryCache.Default.Get(cacheKey) as TValue;
        if (item == null)
        {
            item = getItemCallback();
            MemoryCache.Default.Add(cacheKey, item, DateTime.Now.AddMinutes(durationInMinutes));
        }
        return item;
    }

    public TValue Get<TValue, TId>(string cacheKeyFormat, TId id, int durationInMinutes, Func<TId, TValue> getItemCallback) where TValue : class
    {
        string cacheKey = string.Format(cacheKeyFormat, id);
        TValue item = MemoryCache.Default.Get(cacheKey) as TValue;
        if (item == null)
        {
            item = getItemCallback(id);
            MemoryCache.Default.Add(cacheKey, item, DateTime.Now.AddMinutes(durationInMinutes));
        }
        return item;
    }
}

interface ICacheService
{
    TValue Get<TValue>(string cacheKey, Func<TValue> getItemCallback) where TValue : class;
    TValue Get<TValue, TId>(string cacheKeyFormat, TId id, Func<TId, TValue> getItemCallback) where TValue : class;
}

例子

单项缓存(当每个项都基于其ID进行缓存时,因为为该项类型缓存整个目录将过于密集)。

Product product = cache.Get("product_{0}", productId, 10, productData.getProductById);

缓存所有东西

IEnumerable<Categories> categories = cache.Get("categories", 20, categoryData.getCategories);

何必TId

第二个帮助器特别好,因为大多数数据键都不是复合的。如果经常使用复合键,可以添加其他方法。通过这种方式,您可以避免进行各种字符串连接或字符串。格式以获取要传递给缓存助手的键。它还使传递数据访问方法更容易,因为您不必将ID传递到包装器方法中……对于大多数用例来说,整个过程变得非常简洁和一致。

下面是我使用的一个漂亮而简单的缓存助手类/服务:

using System.Runtime.Caching;  

public class InMemoryCache: ICacheService
{
    public T GetOrSet<T>(string cacheKey, Func<T> getItemCallback) where T : class
    {
        T item = MemoryCache.Default.Get(cacheKey) as T;
        if (item == null)
        {
            item = getItemCallback();
            MemoryCache.Default.Add(cacheKey, item, DateTime.Now.AddMinutes(10));
        }
        return item;
    }
}

interface ICacheService
{
    T GetOrSet<T>(string cacheKey, Func<T> getItemCallback) where T : class;
}

用法:

cacheProvider.GetOrSet("cache key", (delegate method if cache is empty));

缓存提供程序将检查缓存中是否有名称为“缓存id”的东西,如果没有,它将调用委托方法来获取数据并将其存储在缓存中。

例子:

var products=cacheService.GetOrSet("catalog.products", ()=>productRepository.GetAll())

我用过这种方法,对我很有效。 https://msdn.microsoft.com/en-us/library/system.web.caching.cache.add (v = vs.110) . aspx system.web. cache. cache.add的参数信息。

public string GetInfo()
{
     string name = string.Empty;
     if(System.Web.HttpContext.Current.Cache["KeyName"] == null)
     {
         name = GetNameMethod();
         System.Web.HttpContext.Current.Cache.Add("KeyName", name, null, DateTime.Noew.AddMinutes(5), Cache.NoSlidingExpiration, CacheitemPriority.AboveNormal, null);
     }
     else
     {
         name = System.Web.HttpContext.Current.Cache["KeyName"] as string;
     }

      return name;

}