【发布时间】:2014-04-03 12:06:44
【问题描述】:
尽管我浏览了很多关于这个概念的文档,但我无法在这个问题上取得任何进展。我有一个 wcf 服务,它对 Sql Server 数据库进行一些查询。当我只缓存一个查询时,它按预期工作,但是当我尝试缓存两个查询时,第二个查询会引发异常The method has already been invoked。我是这个缓存的新手,所以让我知道我的缓存概念是否存在次要或主要的方法问题。下面是我正在尝试做的代码 sn-p:
var cachePolicy = new CacheItemPolicy();
var cache = MemoryCache.Default;
firstObject = cache["firstObject"] as string;
if ( firstObject == null )
using ( var command = new SqlCommand("SELECT * FROM someTable", connection) )
{
cachePolicy.ChangeMonitors.Add(new SqlChangeMonitor(new SqlDependency(command, null, 600)));
///... I get the data from database and set the firstObject
cache.Add("firstObject", firstObject , cachePolicy);
}
secondObject = cache["secondObject"] as string;
if ( secondObject == null )
using ( var command = new SqlCommand("SELECT * FROM someTable2", connection) )
{
cachePolicy.ChangeMonitors.Add(new SqlChangeMonitor(new SqlDependency(command, null, 600)));
///... I get the data from database and set the secondObject
cache.Add("secondObject", secondObject , cachePolicy);///---> problem occurs here
}
我也尝试使用cache.Set 方法,仍然是同样的异常。
【问题讨论】:
标签: c# sql wcf caching memorycache