日期:2014-05-18 浏览次数:20464 次
_cache = Context.Cache; _path = Context.Server.MapPath ("Stocks.xml"); //xml文件路径 DataSet ds = new DataSet (); ds.ReadXml (_path); //加xml文件转化到ds中 //插入缓存 //参数意思: // Cache.Insert 方法 (String, Object, CacheDependency, DateTime, TimeSpan, //CacheItemPriority, CacheItemRemovedCallback) //向 Cache 对象中插入对象,后者具有依赖项、过期和优先级策略以及一个委托(可用于在从 Cache 移除插//入项时通知应用程序)。 _cache.Insert ("Stocks", ds, new CacheDependency (_path), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback (RefreshDataSet));
------解决方案--------------------
<%@ Import Namespace="System.Data" %>
<script language="C#" runat="server">
static Cache _cache;
static string _path;
void Application_Start ()
{
_cache = Context.Cache; //缓存
_path = Context.Server.MapPath ("Stocks.xml"); //XML地址
DataSet ds = new DataSet ();
ds.ReadXml (_path); //读取XML
_cache.Insert ("Stocks", ds, new CacheDependency (_path),
Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
CacheItemPriority.Default,
new CacheItemRemovedCallback (RefreshDataSet)); //设置缓存依赖项等相关信息
}
//回调函数
static void RefreshDataSet (String key, Object item,
CacheItemRemovedReason reason)
{
DataSet ds = new DataSet ();
ds.ReadXml (_path);
_cache.Insert ("Stocks", ds, new CacheDependency (_path),
Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
CacheItemPriority.Default,
new CacheItemRemovedCallback (RefreshDataSet));
}
</script>
------解决方案--------------------
楼上回答不错
------解决方案--------------------
用函数RefreshDataSet()来进行xml的缓存、读写;
------解决方案--------------------
读取XML数据到dataset,添加到缓存
再CacheDependency跟踪缓存依赖项
使用RefreshDataSet 更新缓存