2011-07-13 13 views
1

私は特に.net 4.0でSystem.runtime.cachingを使って作業しています。 誰かがチェンジモメンターを作成する方法の経験や例がありましたか?.Netキャッシングwith system.runtime.caching

私のやりたいことは、プログラムの起動時に1の実行があることです。ユーザーは、実行を2に増やし、dbの結果をキャッシュにロードするボタンをクリックします。 execountが3に増えたら、キャッシュを無効にして、新しい結果をキャッシュにロードして使用します。

誰でもこれを達成するための提案はありますか?

  • 私はしばらくの間、これに取り組んで、これを思い付いたが、私は、私は(上記の)何をしたいのかを正しい軌道に乗っていた場合、TIが正しく動作していない主な理由私にはわからないされています。

私は4クラスファイル

Cache.csあります

public static class Cache { 

    public static void Add(string key, object obj, CacheItemPolicy policy) { 
     ObjectCache cache = MemoryCache.Default; 
     cache.Add(key, obj, policy); 
    } 

    public static void AddExecCount(string key, object obj) { 
     Add(key, obj, new ExecCountCacheItemPolicy()); 
    } 

    public static object Get(string key) { 
     ObjectCache cache = MemoryCache.Default; 
     if (cache.Contains(key) == true) 
      return cache.Get(key); 
     else 
      return null; 
    } 

    public static void Clear() { 
     Material.MaterialFamilyList.ClearCache(); 
     Material.UsageMaterialDropList.ClearCache(); 
    } 

    /// <summary> 
    /// Clears the in-memory cache so the list matching the provided key is reloaded on next request. 
    /// </summary> 
    /// <param name="key"></param> 
    public static void Clear(string key) { 
     ObjectCache cache = MemoryCache.Default; 
     if (cache.Contains(key) == true) 
      cache.Remove(key); 
    } 

} 

その後ExecCountCacheitemPolicy.cs

public class ExecCountCacheItemPolicy : CacheItemPolicy { 

    public ExecCountCacheItemPolicy() { 
     this.ChangeMonitors.Add(new ExecCountChangeMonitor()); 
     //ExecCount.Increment(); 
    } 

} 

ExecCountChangeMonitor.cs

public class ExecCountChangeMonitor : CacheEntryChangeMonitor { 

    private ReadOnlyCollection<String> _cacheKeys; 
    private string _uniqueId; 
    private string _regionName; 
    private DateTimeOffset _lastModified; 

    public ExecCountChangeMonitor() { 

     _uniqueId = "ExecCountChangeMonitor"; 
     _regionName = ""; 
     _lastModified = DateTime.Now; 
     var keys = new List<string>(); 
     keys.Add(ExecCount.CACHE_KEY); 
     _cacheKeys = new ReadOnlyCollection<String>(keys); 

     InitializationComplete(); 
    } 

    public override string UniqueId { 
     get { return _uniqueId; } 
    } 

    public override ReadOnlyCollection<string> CacheKeys { 
     get { return _cacheKeys; } 
    } 

    public override DateTimeOffset LastModified { 
     get { return _lastModified; } 
    } 

    protected override void Dispose(bool disposing) { 
     base.Dispose(); 
    } 

    public override string RegionName { 
     get { return _regionName; } 
    } 


} 
を3210

そして最後にすべてのボディは、これは別の方法で同じことを達成するために使用することができる場合の、機能しない理由上の任意のIDEを持っている場合

public const string CACHE_KEY = "ExecCount"; 

    public static int Value { 
     get { 
      ObjectCache cache = MemoryCache.Default; 
      if (cache.Contains(CACHE_KEY)) { 
       return (int)cache.Get(CACHE_KEY); 
      } else { 
       return 0; 
      } 
     } 
    } 

    public static int Increment() { 

     CacheItem item; 
     ObjectCache cache = MemoryCache.Default; 

     if (cache.Contains(CACHE_KEY)) { 
      item = cache.GetCacheItem(CACHE_KEY); 
     } else { 
      item = new CacheItem(CACHE_KEY, 0, ""); 
      cache.Add(item, new CacheItemPolicy()); 
     } 

     item.Value = (int)item.Value + 1; 

     return (int)item.Value; 

    } 


} 

をExecCount.cs?

+0

この質問には、いくつかの有益な情報が含まれています:http://stackoverflow.com/questions/6664297/caching-in-a-wcf-application/6664440#6664440 –

答えて

2

CacheEntryChangeMonitorはスレーブを監視するためにmaster cache keyを使用してくださいあなたの方法では、キャッシュエントリを監視するために使用されます。