【问题标题】:Difference between WebCache and MemoryCache in Caching [closed]缓存中WebCache和MemoryCache之间的区别[关闭]
【发布时间】:2016-03-23 12:55:35
【问题描述】:

我只是想在我的 Web API 中实现缓存。因为我已经在使用 System.Runtime.Caching 的 MemoryCache 的帮助下完成了缓存。

现在我知道我们也可以使用使用 System.Web.Helpers 的 WebCache 类进行缓存。

它们之间有什么区别吗?

【问题讨论】:

  • 并不是说这是一个重复的问题,但请查看这些以前提出的问题。 stackoverflow.com/a/14811900/1260204stackoverflow.com/a/11547814/1260204。他们都提到了关于如何在 web api 中实现自定义输出缓存的非常好的文章。
  • @Igor 感谢您提供的信息,现在我们在 Web API 中有很多缓存选项。像 CacheCow 并使用 System.Runtime.caching。但我的问题不同。
  • 这些之间有区别和相似之处。如果它重新打开,我会回答并解释。
  • @Luke 先谢谢了,我也在等。

标签: c# asp.net caching asp.net-web-api memorycache


【解决方案1】:

我总是选择 System.Runtime.Caching。可能是(没有检查) System.Web.Helper.WebCache 在内部指向同一个全局对象。但是,最好在接口上使用任何你想要的东西,所以做一个简单的缓存接口,以后你总是可以轻松切换。

【讨论】:

  • 同意。停止编码具体。我的回答详细说明了这个提示。我赞成,因为在这个答案中包含“使用界面”。
【解决方案2】:

虽然不是确切的答案,但我会将其抽象化,以便您选择以后要实施的答案。

我更喜欢 ObjectCache/Memory 缓存,因为有更多选项。但是....不要把自己画到角落里。

public interface IServerSideMyInformationCache
{
    void SetMyObject(string key, MyObject myobj);

    MyObject GetMyObject(string key);

    void RemoveMyObject(string key);
}

public class ServerSideMyInformationMemoryCache : IServerSideMyInformationCache
{
    public const string CacheKeyPrefix = "ServerSideMyInformationMemoryCachePrefixKey";

    public void SetMyObject(string key, MyObject myobj)
    {
        /* not shown...custom configuration to house the setting */
        CachingSettingsConfigurationSection settings = CachingSettingsConfigurationRetriever.GetCachingSettings();

        ObjectCache cache = MemoryCache.Default;
        CacheItemPolicy policy = new CacheItemPolicy { SlidingExpiration = new TimeSpan(0, settings.MyObjectCacheMinutes, 0), Priority = CacheItemPriority.NotRemovable };
        cache.Set(this.GetFullCacheKey(key), myobj, policy);
    }

    public MyObject GetMyObject(string key)
    {
        MyObject returnItem = null;
        ObjectCache cache = MemoryCache.Default;
        object value = cache.Get(this.GetFullCacheKey(key));
        if (null != value)
        {
            returnItem = value as MyObject;
        }

        return returnItem;
    }

    public void RemoveMyObject(string key)
    {
        string cacheKey = this.GetFullCacheKey(key);
        ObjectCache cache = MemoryCache.Default;
        if (null != cache)
        {
            if (cache.Contains(cacheKey))
            {
                cache.Remove(cacheKey);
            }
        }
    }

    private string GetFullCacheKey(string key)
    {
        string returnValue = CacheKeyPrefix + key;
        return returnValue;
    }
}   


public class ServerSideMyInformationSystemWebCachingCache : IServerSideMyInformationCache
{
    public const string CacheKeyPrefix = "ServerSideMyInformationSystemWebCachingCachePrefixKey";

    public void SetMyObject(string key, MyObject myobj)
    {
        string cacheKey = this.GetFullCacheKey(key);

        if (null != myobj)
        {
            if (null == System.Web.HttpRuntime.Cache[cacheKey])
            {
                /* not shown...custom configuration to house the setting */
                CachingSettingsConfigurationSection settings = CachingSettingsConfigurationRetriever.GetCachingSettings();

                System.Web.HttpRuntime.Cache.Insert(
                    cacheKey, 
                    myobj,
                    null, 
                    System.Web.Caching.Cache.NoAbsoluteExpiration,
                    new TimeSpan(0, settings.MyObjectCacheMinutes, 0),
                    System.Web.Caching.CacheItemPriority.NotRemovable, 
                    null);
            }
            else
            {
                System.Web.HttpRuntime.Cache[cacheKey] = myobj;
            }
        }
    }

    public MyObject GetMyObject(string key)
    {
        MyObject returnItem = null;
        string cacheKey = this.GetFullCacheKey(key);
        if (null != System.Web.HttpRuntime.Cache[cacheKey])
        {
            returnItem = System.Web.HttpRuntime.Cache[cacheKey] as MyObject;
        }

        return returnItem;
    }

    public void RemoveMyObject(string key)
    {
        string cacheKey = this.GetFullCacheKey(key);
        if (null != System.Web.HttpRuntime.Cache[cacheKey])
        {
            System.Web.HttpRuntime.Cache.Remove(cacheKey);
        }
    }

    private string GetFullCacheKey(string key)
    {
        string returnValue = CacheKeyPrefix + key;
        return returnValue;
    }
}   

/* the crappiest of factories, but shows the point */
public static class ServerSideMyInformationCacheFactory
{
    public static IServerSideMyInformationCache GetAIServerSideMyInformationCache()
    {
        return new ServerSideMyInformationMemoryCache();
        ////return new ServerSideMyInformationSystemWebCachingCache();
    }
}   

【讨论】:

    猜你喜欢
    • 2013-01-04
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    相关资源
    最近更新 更多