private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (!applyMemCached) { //检查并移除相应的缓存项 instance = CachesFileMonitor.CheckAndRemoveCache(instance); } }
/// <summary> /// 单体模式返回当前类的实例 /// </summary> /// <returns></returns> public static ShopCache GetCacheService() { if (instance == null) { lock (lockHelper) { if (instance == null) { instance = applyMemCached ? new ShopCache() : CachesFileMonitor.CheckAndRemoveCache(new ShopCache()); } } } return(instance); }
/// <summary> /// 单体模式返回当前类的实例 /// </summary> /// <returns></returns> public static ShopCache GetCacheService() { if (instance == null) { lock (lockHelper) { if (instance == null) { instance = applyMemCached ? new ShopCache() : CachesFileMonitor.CheckAndRemoveCache(new ShopCache()); } } } return instance; }
/// <summary> /// 检查和移除缓存 /// </summary> /// <param name="instance"></param> /// <returns></returns> public static ShopCache CheckAndRemoveCache(ShopCache instance) // { //当程序运行中cache.config发生变化时则对缓存对象做删除的操作 cachefilenewchange = System.IO.File.GetLastWriteTime(path); if (cachefileoldchange != cachefilenewchange) { lock (cachelockHelper) { if (cachefileoldchange != cachefilenewchange) { //当有要清除的项时 DataSet dsSrc = new DataSet(); dsSrc.ReadXml(path); foreach (DataRow dr in dsSrc.Tables[0].Rows) { if (dr["xpath"].ToString().Trim() != "") { DateTime removedatetime = DateTime.Now; try { removedatetime = Convert.ToDateTime(dr["removedatetime"].ToString().Trim()); } catch { ; } if (removedatetime > cachefilenewchange.AddSeconds(-2)) { string xpath = dr["xpath"].ToString().Trim(); instance.RemoveObject(xpath, false); } } } cachefileoldchange = cachefilenewchange; dsSrc.Dispose(); } } } return(instance); }
/// <summary> /// 检查和移除缓存 /// </summary> /// <param name="instance"></param> /// <returns></returns> public static ShopCache CheckAndRemoveCache(ShopCache instance) // { //当程序运行中cache.config发生变化时则对缓存对象做删除的操作 cachefilenewchange = System.IO.File.GetLastWriteTime(path); if (cachefileoldchange != cachefilenewchange) { lock (cachelockHelper) { if (cachefileoldchange != cachefilenewchange) { //当有要清除的项时 DataSet dsSrc = new DataSet(); dsSrc.ReadXml(path); foreach (DataRow dr in dsSrc.Tables[0].Rows) { if (dr["xpath"].ToString().Trim() != "") { DateTime removedatetime = DateTime.Now; try { removedatetime = Convert.ToDateTime(dr["removedatetime"].ToString().Trim()); } catch { ; } if (removedatetime > cachefilenewchange.AddSeconds(-2)) { string xpath = dr["xpath"].ToString().Trim(); instance.RemoveObject(xpath, false); } } } cachefileoldchange = cachefilenewchange; dsSrc.Dispose(); } } } return instance; }