/// <summary> /// 读取文件内容 /// </summary> /// <param name="encoding">指定编码时(会跳过编码自动检测)</param> /// <returns></returns> public static string ReadAllText(string fileName, int cacheMinutes, Encoding encoding) { try { string key = "IOHelper_" + fileName.GetHashCode(); if (cache.Contains(key)) { return(cache.Get <string>(key)); } if (!File.Exists(fileName)) { return(string.Empty); } Byte[] buff = null; lock (GetLockObj(fileName.Length)) { if (!File.Exists(fileName))//多线程情况处理 { return(string.Empty); } buff = File.ReadAllBytes(fileName); string result = BytesToText(buff, encoding); if (cacheMinutes > 0) { cache.Set(key, result, cacheMinutes); } return(result); } } catch (Exception err) { Log.WriteLogToTxt(err); } return(string.Empty); }
private static string ReadAllText(string fileName, int cacheMinutes, Encoding encoding, int tryCount) { try { string key = "IOHelper_" + fileName.GetHashCode(); if (cache.Contains(key)) { return(cache.Get <string>(key)); } if (!File.Exists(fileName)) { return(string.Empty); } Byte[] buff = null; lock (GetLockObj(fileName.Length)) { if (!File.Exists(fileName))//多线程情况处理 { return(string.Empty); } try { buff = File.ReadAllBytes(fileName); } catch (Exception err) { if (tryCount > 0) { tryCount--; Thread.Sleep(500 + (3 - tryCount) * 500); ReadAllText(fileName, cacheMinutes, encoding, tryCount); } else { Error.Throw(err.Message); } } string result = BytesToText(buff, encoding); if (cacheMinutes > 0) { cache.Set(key, result, cacheMinutes); } return(result); } } catch (Exception err) { Log.WriteLogToTxt(err); } return(string.Empty); }
private static bool IsObjInHtml(string objName, string path) { if (string.IsNullOrEmpty(objName)) { return(false); } objName = objName.Trim('_', ' '); CacheManage cache = CacheManage.LocalInstance; string has = path.GetHashCode().ToString(); string html = string.Empty; if (cache.Contains(has)) { html = cache.Get <string>(has); } else if (File.Exists(path)) { html = File.ReadAllText(path); cache.Set(has, html, 0, path); } if (!string.IsNullOrEmpty(html)) { //检测文件中是否有对应的关键字: if (html.Contains(objName + "\"") || html.Contains(objName + "'")) { return(true); } } return(false); }
public static bool IsKeyInHtml(string objName) { CacheManage cache = CacheManage.LocalInstance; string path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.UrlReferrer.LocalPath); string has = path.GetHashCode().ToString(); string html = string.Empty; if (cache.Contains(has)) { html = cache.Get <string>(has); } else if (File.Exists(path)) { html = File.ReadAllText(path); cache.Set(has, html, 0, path); } if (!string.IsNullOrEmpty(html)) { //检测文件中是否有对应的关键字: if (html.Contains("\"" + objName + "\"") || html.Contains("'" + objName + "'")) { return(true); } } return(false); }
private IAop GetFromConfig() { IAop aop = null; string aopApp = AppConfig.Aop; if (!string.IsNullOrEmpty(aopApp)) { string key = "OuterAop_Instance"; if (_Cache.Contains(key)) { aop = _Cache.Get(key) as IAop; } else { #region AOP加载 string[] aopItem = aopApp.Split(','); if (aopItem.Length == 2)//完整类名,程序集(dll)名称 { if (!_IsLoadCompleted) { try { lock (lockObj) { if (_IsLoadCompleted) { return(GetFromConfig());//重新去缓存里拿。 } _IsLoadCompleted = true; System.Reflection.Assembly ass = System.Reflection.Assembly.Load(aopItem[1]); if (ass != null) { object instance = ass.CreateInstance(aopItem[0]); if (instance != null) { _Cache.Set(key, instance, 1440, AppConst.AssemblyPath + aopItem[1].Replace(".dll", "") + ".dll"); aop = instance as IAop; aop.OnLoad(); } } } } catch (Exception err) { string errMsg = err.Message + "--Web.config need add a config item,for example:<add key=\"Aop\" value=\"Web.Aop.AopAction,Aop\" />(value format : ClassFullName,AssemblyName) "; Error.Throw(errMsg); } } } #endregion } } if (aop != null) { IAop cloneAop = aop.Clone(); return(cloneAop == null ? aop : cloneAop); } return(null); }
private IAop GetFromConfig() { IAop aop = null; string aopApp = AppConfig.Aop; if (!string.IsNullOrEmpty(aopApp)) { if (_Cache.Contains("Aop_Instance")) { aop = _Cache.Get("Aop_Instance") as IAop; } else { #region AOP加载 string[] aopItem = aopApp.Split(','); if (aopItem.Length == 2)//完整类名,程序集(dll)名称 { try { System.Reflection.Assembly ass = System.Reflection.Assembly.Load(aopItem[1]); if (ass != null) { object instance = ass.CreateInstance(aopItem[0]); if (instance != null) { _Cache.Add("Aop_Instance", instance, AppConst.RunFolderPath + aopItem[1].Replace(".dll", "") + ".dll", 1440); aop = instance as IAop; if (!_CallOnLoad) { lock (lockObj) { if (!_CallOnLoad) { _CallOnLoad = true; aop.OnLoad(); } } } return(aop); } } } catch (Exception err) { string errMsg = err.Message + "--Web.config need add a config item,for example:<add key=\"Aop\" value=\"Web.Aop.AopAction,Aop\" />(value format:namespace.Classname,Assembly name) "; Error.Throw(errMsg); } } #endregion } } if (aop != null) { return(aop.Clone()); } return(null); }
/// <summary> /// 从缓存中加载html /// </summary> /// <param name="key"></param> /// <returns></returns> public bool LoadFromCache(string key) { if (theCache.Contains(key))//缓存中存在对应值是key的对象 { if (_IsNoClone) { _XmlDocument = theCache.Get(key) as XmlDocument; } else { _XmlDocument = GetCloneFrom(theCache.Get(key) as XmlDocument); } _IsLoadFromCache = true; return(true); } else { return(false); } }
MDataRow GetUser() { string key = "WF_User_" + UserID; if (cache.Contains(key)) { return(cache.Get <MDataRow>(key)); } else { using (MAction action = new MAction(U_AriesEnum.Sys_User)) { if (action.Fill(UserID)) { cache.Set(key, action.Data, 1); return(action.Data); } } } return(null); }
static void Main(string[] args) { //说明:如果你确定缓存一定是在本机,使用:CacheManage cache= CacheManage.LocalInstance //如果只是缓存一般数据,将来有可能启用分布式时,用:CacheManage cache = CacheManage.Instance; //比如框架对一些表架构的元数据的缓存,用的是本机(速度快):CacheManage.LocalInstance //而框架对于自动缓存(表的数据),用的是:CacheManage.Instance (将来随便分布式启用分散到各缓存服务器) AppConfig.Cache.MemCacheServers = "127.0.0.1:11211";//配置启用MemCache,127.0.0.1:11212 CacheManage cache = CacheManage.Instance; if (!cache.Contains("a1")) { cache.Set("a1", "a1", 0.1); } cache.Set("a2", "a2", 0.5);//存在则更新,不存在则添加。 cache.Set("a3", "a3", 2.2); cache.Set("a0", "a0"); cache.Set("table", cache.CacheInfo); Console.WriteLine(cache.Get <string>("a0")); Console.WriteLine(cache.Get <string>("a1")); Console.WriteLine(cache.Get <string>("a2")); Console.WriteLine(cache.Get <string>("a3")); MDataTable table = cache.Get <MDataTable>("table"); if (table != null) { Console.WriteLine(table.Rows.Count); } if (cache.CacheType == CacheType.LocalCache)//只能拿到本机的信息 { Console.WriteLine("缓存数:" + table.Rows.Count); Console.WriteLine("总内存(M):" + GC.GetTotalMemory(false) / 1024); // 感觉拿到的值不太靠谱。 } cache.Remove("a0"); //单个移除 cache.Clear(); //清除所有缓存 Console.Read(); }
internal static bool Save(string fileName, string text, bool isAppend, bool writeLogOnError, Encoding encode, int tryCount) { try { //System.Text.Encoding.UTF8 string folder = Path.GetDirectoryName(fileName); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } string key = "IOHelper_Save_" + fileName.GetHashCode(); if (cache.Contains(key)) { encode = cache.Get <Encoding>(key); } else if (File.Exists(fileName)) { TextEncodingDetect detect = new TextEncodingDetect(); Encoding detectEncode = detect.GetEncoding(File.ReadAllBytes(fileName), encode); if (detectEncode != Encoding.ASCII) { encode = detectEncode; } cache.Set(key, encode, 60); } lock (GetLockObj(fileName.Length)) { try { using (StreamWriter writer = new StreamWriter(fileName, isAppend, encode)) { if (!isAppend && fileName.EndsWith(".txt")) { //写入bom头 } writer.Write(text); } } catch (Exception err) { if (tryCount > 0) { tryCount--; Thread.Sleep(500 + (3 - tryCount) * 500); Save(fileName, text, isAppend, writeLogOnError, encode, tryCount); } else { Error.Throw(err.Message); } } } return(true); } catch (Exception err) { if (tryCount == 3) // 避免死循环。 { if (writeLogOnError) { Log.Write(err, LogType.Error); } else { Error.Throw("IOHelper.Save() : " + err.Message); } } } return(false); }