我已经实现了一个缓存,可以在包含值到期时更新它们。
我的代码是:
class MyCache
{
private static readonly MemoryCache Cache = MemoryCache.Default;
private CacheItemPolicy _errorpolicy;
private CacheItemPolicy _warnPolicy;
private CacheEntryRemovedCallback _warnCacheEntryRemovedCallback;
private CacheEntryRemovedCallback _errorCacheEntryRemovedCallback;
public void AddErrors(string key, object errors)
{
_errorCacheEntryRemovedCallback = ErrorItemRemovedCallback;
_errorpolicy = new CacheItemPolicy
{
AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(7.00),
RemovedCallback = _errorCacheEntryRemovedCallback
};
Cache.Set(key, errors, _errorpolicy);
}
public void AddWarnings(string key, object warnings)
{
_warnCacheEntryRemovedCallback = WarningCacheItemRemovedCallback;
_warnPolicy = new CacheItemPolicy
{
AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(4.00),
RemovedCallback = _warnCacheEntryRemovedCallback
};
Cache.Set(key, warnings, _warnPolicy);
}
private void ErrorItemRemovedCallback(CacheEntryRemovedArguments arguments)
{
Console.WriteLine("{0} Item removed: {1}", DateTime.UtcNow, arguments.CacheItem.Key);
Cache.Set(arguments.CacheItem.Key, new List<string> { "error1", "error2", "error3" }, _errorpolicy);
}
private void WarningCacheItemRemovedCallback(CacheEntryRemovedArguments arguments)
{
Console.WriteLine("{0} Item removed: {1}", DateTime.UtcNow, arguments.CacheItem.Key);
Cache.Set(arguments.CacheItem.Key, new List<string> { "warn1", "warn2", "warn3" }, _warnPolicy);
}
}
但是出了点问题。
以上内容在提示中写道:
04-05-2014 21:20:00 Item removed: warn
04-05-2014 21:20:00 Item removed: error
04-05-2014 21:20:20 Item removed: error
04-05-2014 21:20:20 Item removed: warn
04-05-2014 21:20:40 Item removed: warn
04-05-2014 21:20:40 Item removed: error
04-05-2014 21:20:40 Item removed: error
04-05-2014 21:20:40 Item removed: warn
......我本来期待像
这样的事情04-05-2014 21:20:04 Item removed: warn
04-05-2014 21:20:07 Item removed: error
04-05-2014 21:20:08 Item removed: warn
04-05-2014 21:20:12 Item removed: warn
04-05-2014 21:20:14 Item removed: error
...
我错过了什么?
更新2014-05-14:
我已将字段级变量更改为局部变量 - 但存在相同的行为。另请注意,从缓存中删除的顺序会改变 - 首先是“errors-then-warns” - 但下次是“warns-then-errors”。
另请注意,第一次从缓存中删除是在21秒之后 - 之后每隔20秒就会发生一次。
14-05-2014 13:00:59 Adding errors to cache
14-05-2014 13:00:59 Adding warnings to cache
Press 'q' to quit
14-05-2014 13:01:20 Item removed: warn
14-05-2014 13:01:20 Item removed: error
14-05-2014 13:01:40 Item removed: error
14-05-2014 13:01:40 Item removed: warn
14-05-2014 13:02:00 Item removed: warn
14-05-2014 13:02:00 Item removed: error
14-05-2014 13:02:20 Item removed: error
14-05-2014 13:02:20 Item removed: warn
14-05-2014 13:02:40 Item removed: warn
14-05-2014 13:02:40 Item removed: error
我的全部(新代码)在这里:
namespace DemoCache
{
internal class Program
{
private static readonly List<string> ErrorList = new List<string> {"error1", "error2", "error3"};
private static readonly List<string> WarningList = new List<string> {"warn1", "warn2", "warn3"};
static void Main()
{
var myCache = new MyCache();
Console.WriteLine("{0} Adding errors to cache ", DateTime.UtcNow);
myCache.AddErrors("error", ErrorList);
Console.WriteLine("{0} Adding warnings to cache", DateTime.UtcNow);
myCache.AddWarnings("warn", WarningList);
Console.WriteLine("Press 'q' to quit");
var keepRunning = true;
do
{
var key = Console.ReadKey(true);
switch (Char.ToLower(key.KeyChar))
{
case 'q':
keepRunning = false;
break;
}
} while (keepRunning);
}
}
class MyCache
{
private static readonly MemoryCache Cache = new MemoryCache("name");
//private CacheItemPolicy _errorpolicy;
//private CacheItemPolicy _warnPolicy;
//private CacheEntryRemovedCallback _warnCacheEntryRemovedCallback;
//private CacheEntryRemovedCallback _errorCacheEntryRemovedCallback;
public void AddErrors(string key, object errors)
{
CacheEntryRemovedCallback errorCacheEntryRemovedCallback = ErrorItemRemovedCallback;
var errorpolicy = new CacheItemPolicy
{
AbsoluteExpiration = DateTime.Now.AddSeconds(7.00),
RemovedCallback = errorCacheEntryRemovedCallback
};
Cache.Set(key, errors, errorpolicy);
}
public void AddWarnings(string key, object warnings)
{
CacheEntryRemovedCallback warnCacheEntryRemovedCallback = WarningCacheItemRemovedCallback;
var warnPolicy = new CacheItemPolicy
{
AbsoluteExpiration = DateTime.Now.AddSeconds(11.00),
RemovedCallback = warnCacheEntryRemovedCallback
};
Cache.Set(key, warnings, warnPolicy);
}
private void ErrorItemRemovedCallback(CacheEntryRemovedArguments arguments)
{
Console.WriteLine("{0} Item removed: {1}", DateTime.UtcNow, arguments.CacheItem.Key);
AddErrors(arguments.CacheItem.Key, new List<string> { "error1", "error2", "error3" });
//Cache.Set(arguments.CacheItem.Key, , _errorpolicy);
}
private void WarningCacheItemRemovedCallback(CacheEntryRemovedArguments arguments)
{
Console.WriteLine("{0} Item removed: {1}", DateTime.UtcNow, arguments.CacheItem.Key);
AddWarnings(arguments.CacheItem.Key, new List<string> { "warn1", "warn2", "warn3" });
//Cache.Set(arguments.CacheItem.Key, new List<string> { "warn1", "warn2", "warn3" }, _warnPolicy);
}
}
}