2011-07-13 3 views
1

Je travaille avec .net 4.0 spécifiquement System.runtime.caching. Est-ce que quelqu'un a eu l'expérience ou des exemples de la façon de créer un changemonitor personnalisé? Ce que je veux faire est, quand mon programme commence, il a un nombre d'exécution de 1. L'utilisateur clique sur un bouton qui incrémente l'execount à 2 et charge les résultats db dans le cache. Si l'exécutable est incrémenté de 3, je veux invalider le cache et charger les nouveaux résultats dans le cache..NET Mise en cache avec system.runtime.caching

Quelqu'un at-il des suggestions sur la façon d'accomplir cela?

  • Je travaille sur cette question depuis un certain temps et est venu avec cela, mais je ne sais pas si je suis sur la bonne voie avec ce que je veux faire (mentionné ci-dessus) Principalement parce que ti ne fonctionne pas correctement .

J'ai 4 fichiers classe

de 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); 
    } 

} 

Puis 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; } 
    } 


} 

Et enfin ExecCount.cs

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; 

    } 


} 

Si un corps a des ides sur pourquoi cela ne fonctionne pas, d'une autre façon si pourrait être utilisé pour accomplir la même chose?

+0

Cette question pourrait contenir quelques informations utiles: http://stackoverflow.com/questions/6664297/caching-in-a-wcf-application/6664440#6664440 –

Répondre

2

CacheEntryChangeMonitor est utilisé pour surveiller autre entrée de cache, de votre manière s'il vous plaît utiliser master cache key pour surveiller l'esclave.

Questions connexes