2016-03-23 5 views
3

こんにちは私は自分のWeb APIでキャッシングを実装しようとしています。 System.Runtime.Cachingを使用するMemoryCacheの助けを借りてキャッシュを既に行っています。今私は、System.Web.Helperを使用するWebCacheクラスでもキャッシュできることを知りました。それらの間に違いはありますか?どちらが良いでしょうか?説明してください。前もって感謝します。キャッシングにおけるWebCacheとMemoryCacheの違い

+0

これは重複した質問ですが、これまでに質問したことがあります。 http://stackoverflow.com/a/14811900/1260204およびhttp://stackoverflow.com/a/11547814/1260204。彼らは両方とも、Web APIでカスタム出力キャッシュを実装する方法に関するかなり良い記事を参照しています。 – Igor

+0

@Igor情報をお寄せいただきありがとうございます。今度は、Web APIのキャッシュには非常に多くのオプションがあります。 CacheCowと同様にSystem.Runtime.cachingを使用します。しかし、私の質問は異なっていました。 –

答えて

1

私はいつもSystem.Runtime.Cachingに行きます。 System.Web.Helper.WebCacheが内部的に同じグローバルオブジェクトを指している(チェックしていない)可能性があります。ただし、インターフェイス上で必要なものを使用することをお勧めします。したがって、簡単なキャッシュ処理を行い、後でいつでも簡単に切り替えることができます。

+0

合意。コンクリートへのコーディングをやめてください。私の答えはこのヒントを詳述しています。私はこの回答に「インターフェイスを使用する」ということを含めることでアップ投票しています。 – granadaCoder

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(); 
    } 
} 
関連する問題