静态缓存问题 [英] Static Cache Problem
问题描述
我将我的xml数据缓存到数据集中,如下所示:
Hi,
I am caching my xml data into a data set as given below:
public static void LoadCache()
{
static DataSet ds = new DataSet();
ds.ReadXml(HttpContext.Current.Server.MapPath("myXML.xml"));
HttpRuntime.Cache.Insert("myCache", ds,null, Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration, CacheItemPriority.High, null);
}
//Method to Call Cache
public DataSet GetXMLData()
{
if (HttpRuntime.Cache["myCache"] == null)
{
LoadCache();
}
return (DataSet)HttpRuntime.Cache["myCache"];
}
现在我有一个数据集dsData在我的页面加载中从这个缓存中填充并执行以下操作:
Now I have a dataset dsData In my page load that is filled from this cache and I do the following:
DataView dv = dsData.Tables[0].DefaultView;
dv.RowFilter = "key=0";
DataTable dtTemp = dv.ToTable();
dtTemp.Columns.Add("sort"); //Works fine only once
上面的块只能工作一次,当我刷新页面时,它显示dtTemp中已经存在同名的列。
当我尝试调试我发现缓存数据集ds也包含相同的列。
我无法弄清楚为什么会发生这种情况。
The above chunk works only for once, when I refresh my page it shows Column with same name already exist in dtTemp.
Also when I tried Debugging I found Out that The Cache Dataset ds also contains same column.
I am unable to figure out why this happens.
推荐答案
我修改了我的第一个答案,因为它没有回答这个问题。
我在尝试下面的代码后仍然没有答案。
它可以正常工作我可以看到。
I revised my first answer as it did not answer the question.
I still don't have an answer, after trying the code below.
It works fine as far as I can see.
static DataSet ds = new DataSet();
public static void LoadCache()
{
DataTable dt = ds.Tables.Add();
dt.Columns.Add("key", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add(0, "Donald");
dt.Rows.Add(0, "Duck");
dt.Rows.Add(1, "Mickey");
dt.Rows.Add(1, "Mouse");
HttpRuntime.Cache.Insert("myCache", ds, null, Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration, CacheItemPriority.High, null);
}
public DataSet GetXMLData()
{
if (HttpRuntime.Cache["myCache"] == null)
{
LoadCache();
}
return (DataSet)HttpRuntime.Cache["myCache"];
}
public void GetData()
{
DataView dv = GetXMLData().Tables[0].DefaultView;
dv.RowFilter = "key=0";
DataTable dtTemp = dv.ToTable();
dtTemp.Columns.Add("sort");
}
private void SomeFunction()
{
// Call GetData several times.
GetData();
GetData();
GetData();
}
由于我不知道数据集的结构,我不能做更多的测试。
还有其他差异可能是一个因素。
As I don't know the structure of your dataset, I cannot do much more testing.
There are also other differences that might be a factor.
set
HttpRuntime.Cache [myCache添加一列后,] = null;
。
set
HttpRuntime.Cache["myCache"] = null;
after adding a column.
这篇关于静态缓存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!