//这是读取配置文件(BaseConfig.config)的方法
//通过序列化和反序列进行读写
public static BaseConfig getModel()
{
WebConfig = HttpContext.Current.Cache["SystemBaseConfig"] as BaseConfig;
if (WebConfig == null)
{
string path = Lib.GetMapPath(ConfigurationManager.AppSettings["WebBaseConfig"].ToString());
//创建缓存依赖项
CacheDependency dependency = new CacheDependency(path);
HttpContext.Current.Cache.Add("SystemBaseConfig", (BaseConfig)SerializationHelper.Load(typeof(BaseConfig), path), dependency, Cache.NoAbsoluteExpiration, new TimeSpan(0, 30, 0), CacheItemPriority.Default, null);
WebConfig = HttpContext.Current.Cache["SystemBaseConfig"] as BaseConfig;
}
return WebConfig;
}
// 这是进行保存的
private static object lockHelper = new object(); public void Save(BaseConfig model,string path)
{
lock (lockHelper)
{
SerializationHelper.Save(model, path);
}
}//调用保存的方法是
new BLLBaseConfig().Save(model, Server.MapPath(ConfigurationManager.AppSettings["WebBaseConfig"].ToString()));
现在的问题是:保存时能正确写入.config文件,读取时并不会读取更新后的文件。
查了MSDN上关于CacheDependency dependency = new CacheDependency(path);的解释,说是当里面的对象被修改时会自动的移除该项。
那这说法应该是当我保存时HttpContext.Current.Cache["SystemBaseConfig"]这个缓存就为空了,可我刷新时还是读取缓存中的。
不知是不是哪里写错了。
解决方案 »
- 英文面试题,HELP
- 急 An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005
- 二级下拉用哪个Ajax控件比较简便
- 请问怎么在GridView1_RowEditing中获取当前行某个字段的值
- 怎么将常用的数据放在cache中,然后调用
- 如何让按钮控件优先于正则表达式
- 无法将带 [] 的索引应用于“bool”类型的表达式?
- 实现方法求助
- 有关session的简单问题!!
- sql server 数据库有一百万条信息 要取其中的一条 要花多长时间 谁能告诉我呀!
- GridView绑定XML数据源的问题
- 求ASP.NET 流行以及免费的开源CMS系统
这段代码是从dtcms里面看到的,它在保存时并没有重新赋值到cache中。
如果保存后要重新赋值的话,那
CacheDependency dependency = new CacheDependency(path);
这一句的意思就不是当里面的内容被更改时,会自动更新了。
我上面的代码哪里出错了呢,依赖缓存最重要的就是设置这里吧 CacheDependency dependency = new CacheDependency(path);
HttpContext.Current.Cache.Add("SystemBaseConfig", (BaseConfig)SerializationHelper.Load(typeof(BaseConfig), path), dependency, Cache.NoAbsoluteExpiration, new TimeSpan(0, 30, 0), CacheItemPriority.Default, null);
它必须维护其即时与准确性.
其内容一旦被修改,
则意味着Cache数据出错,
系统会自动清除错误数据,
避免出现更严重的业务问题.因此修改后的缓存对象必须重新赋值给Cache,
让系统明白这是有意为之.