【问题标题】:.Net Caching with system.runtime.caching.Net 缓存与 system.runtime.caching
【发布时间】:2012-11-14 19:07:05
【问题描述】:

我正在使用 .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;

    }


}

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

【问题讨论】:

标签: .net c#-4.0


【解决方案1】:

CacheEntryChangeMonitor用于监控other缓存入口,如你所见请使用master cache key监控slave。

【讨论】:

    猜你喜欢
    • 2011-12-12
    • 2016-07-20
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 2014-04-07
    • 2012-05-21
    相关资源
    最近更新 更多