我只是想在我的Web API中实现缓存。因为我已经在使用System.Runtime.Caching的MemoryCache的帮助下完成了缓存。
现在我开始知道我们可以使用System.Web.Helpers对WebCache类进行缓存。
它们之间有什么区别吗?
答案 0 :(得分:1)
我总是使用System.Runtime.Caching。可能(没有检查)System.Web.Helper.WebCache在内部指向同一个全局对象。但是,最好在界面上使用任何你想要的东西,所以做一个简单的缓存intrerface,以后你总是可以轻松切换。
答案 1 :(得分:1)
虽然不是确切的答案,但我会抽象它,所以你可以选择你想要实现的那个。
我更喜欢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();
}
}