使用system.runtime.caching进行.Net缓存

时间:2011-07-13 13:32:00

标签: .net c#-4.0

我正在使用.net 4.0特别是System.runtime.caching。 有没有人有过如何创建自定义更改监视器的经验或示例?

我想要做的是,当我的程序启动时,它的execount为1.用户单击一个按钮,将execount增加到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; }
    }


}

最后是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;

    }


}

如果任何一个机构对于为什么这不起作用有任何想法,如果有另一种方法可以用来完成同样的事情?

1 个答案:

答案 0 :(得分:2)

CacheEntryChangeMonitor 用于监控其他缓存条目,请您使用master cache key来监控奴隶。