/// <summary> /// 设置缓存 /// </summary> /// <returns></returns> public static void setCache(string key, string content, double cacheTime, bool isWap) { if (cacheTime == 0) { return; } key = getKey(key, isWap); if (cacheTime == -1 || cacheTime >= 60) { string mapPath = Fetch.getMapPath(key); new System.Threading.Thread(new System.Threading.ThreadStart(() => { Fetch.writeFile(mapPath, content); })).Start(); } else if (cacheTime > 0) { TempCacheInfo cacheInfo = null; if (dictCach.ContainsKey(key)) { cacheInfo = dictCach[key]; } else { cacheInfo = new TempCacheInfo(); } cacheInfo.LimitDate = DateTime.Now.AddMinutes(cacheTime); cacheInfo.CacheContent = content; dictCach[key] = cacheInfo; } }
/// <summary> /// 获取缓存 /// </summary> /// <param name="key"></param> /// <returns></returns> public static string getCache(string key, int cacheTime, bool isWap) { key = getKey(key, isWap); if (cacheTime == -1)//文件缓存 { return(Fetch.getFile(Fetch.getMapPath(key))); } else if (cacheTime >= 60) { string tempPath = Fetch.getMapPath(key); DateTime dt = System.IO.File.GetLastWriteTime(tempPath); if (dt.AddMinutes(cacheTime) > DateTime.Now) { return(Fetch.getFile(tempPath)); } else { return(null); } } else if (cacheTime > 0) { if (dictCach.ContainsKey(key)) { TempCacheInfo cacheInfo = dictCach[key]; if (cacheInfo.LimitDate >= DateTime.Now) { return(cacheInfo.CacheContent); } else { dictCach.Remove(key); return(null); } } else { return(null); } } else { return(null); } }
/// <summary> /// 定时清除缓存委托方法 /// </summary> /// <param name="obj"></param> /// <param name="e"></param> private static void clearCache(object obj, ElapsedEventArgs e) { List <string> keys = new List <string>(); foreach (string key in dictCach.Keys) { keys.Add(key); } foreach (string key in keys) { TempCacheInfo cacheInfo = dictCach[key]; if (cacheInfo.LimitDate < DateTime.Now) { dictCach.Remove(key); } } }