/// <summary> /// 获取外部程序集的缓存 /// </summary> /// <param name="info"></param> /// <param name="type"></param> /// <param name="connectionString"></param> /// <returns></returns> private static ICacheAdaper GetAssemblyCache(DBInfo info, string type, string connectionString) { Assembly _cacheAssembly = null; try { _cacheAssembly = Assembly.Load("Buffalo.QueryCache"); } catch (Exception ex) { throw new MissingMemberException("找不到类Buffalo.QueryCache,请保证项目已经引用了Buffalo.QueryCache.dll"); } if (_cacheAssembly == null) { throw new MissingMemberException("找不到类Buffalo.QueryCache,请保证项目已经引用了Buffalo.QueryCache.dll"); } Type loaderType = _cacheAssembly.GetType("Buffalo.QueryCache.CacheLoader", false, false); if (loaderType == null) { throw new MissingMemberException("找不到类Buffalo.QueryCache.CacheLoader,请保证Buffalo.QueryCache.dll的完整性"); } MethodInfo mInfo = loaderType.GetMethod("GetCache"); if (loaderType == null) { throw new MissingMethodException("找不到方法GetCache,请保证Buffalo.QueryCache.dll的完整性"); } ICacheAdaper cache = mInfo.Invoke(null, new object[] { info, type, connectionString }) as ICacheAdaper; return(cache); }
/// <summary> /// 根据类型创建缓存适配器 /// </summary> /// <param name="type">类型</param> /// <param name="connectionString">连接字符串</param> /// <returns></returns> public static ICacheAdaper GetCache(DBInfo info, string type, string connectionString) { if (string.IsNullOrEmpty(type)) { return(null); } string dtype = type.Trim(); if (dtype.Equals("system", StringComparison.CurrentCultureIgnoreCase))//内存 { return(new MemoryAdaper(info)); } ICacheAdaper cache = GetAssemblyCache(info, dtype, connectionString); if (cache != null) { return(cache); } throw new NotSupportedException("不支持:" + type + " 的缓存类型,当前只支持system、memcached、redis类型的缓存"); }
/// <summary> /// 获取当前配置文件的数据库信息 /// </summary> /// <param name="doc"></param> /// <returns></returns> public static DBInfo GetDBInfo(XmlDocument doc) { string dbType = null; string connectionString = null; string name = null; //string output = null; string[] attNames = null; ICacheAdaper ica = null; string cacheType = null; string cacheConn = null; //Dictionary<string, string> extendDatabaseConnection = new Dictionary<string,string>(); if (doc == null) { throw new Exception("找不到配置文件"); } bool isAlltable = false; XmlNodeList lstConfig = doc.GetElementsByTagName("config"); if (lstConfig.Count > 0) { XmlNode node = lstConfig[0]; foreach (XmlAttribute att in node.Attributes) { if (att.Name.Equals("dbType", StringComparison.CurrentCultureIgnoreCase)) { dbType = att.InnerText; } //else if (att.Name.Equals("output", StringComparison.CurrentCultureIgnoreCase)) //{ // output = att.InnerText; //} else if (att.Name.Equals("name", StringComparison.CurrentCultureIgnoreCase)) { name = att.InnerText; } else if (att.Name.Equals("connectionString", StringComparison.CurrentCultureIgnoreCase)) { connectionString = att.InnerText; } else if (att.Name.Equals("appnamespace", StringComparison.CurrentCultureIgnoreCase)) { string names = att.InnerText; if (!string.IsNullOrEmpty(names)) { attNames = names.Split(new char[] { '|' }); } for (int i = 0; i < attNames.Length; i++) { string attName = attNames[i]; if (!attName.EndsWith(".")) { attNames[i] = attName + "."; } } } else if (att.Name.Equals("cache", StringComparison.CurrentCultureIgnoreCase)) { cacheType = att.InnerText; } else if (att.Name.Equals("cacheConnString", StringComparison.CurrentCultureIgnoreCase)) { cacheConn = att.InnerText; } else if (att.Name.Equals("allCache", StringComparison.CurrentCultureIgnoreCase)) { isAlltable = att.InnerText == "1"; } } } else { throw new Exception("配置文件没有config节点"); } DBInfo info = new DBInfo(name, connectionString, dbType); ica = QueryCache.GetCache(info, cacheType, cacheConn); info.SetQueryCache(ica, isAlltable); info.DataaccessNamespace = attNames; return(info); }
/// <summary> /// 设置查询缓存 /// </summary> /// <param name="ica"></param> internal void SetQueryCache(ICacheAdaper ica, bool isAlltable) { _cache.InitCache(ica, isAlltable); }
/// <summary> /// 初始化缓存 /// </summary> /// <param name="cache">缓存类</param> /// <param name="isAllTableCache">是否所有表都进行缓存</param> public void InitCache(ICacheAdaper cache, bool isAllTableCache) { _cache = cache; _isAllTableCache = isAllTableCache; }