/// <summary> /// 动态加载数据库工厂 /// </summary> /// <param name="providerInvariantName"></param> /// <param name="dbType"></param> /// <returns></returns> public static DbProviderFactory GetFactory(string dbKey) { try { var config = GetConfig(dbKey); if (BaseCache.Exists(config.ProviderName)) { return(BaseCache.Get <object>(config.ProviderName) as DbProviderFactory); } else { var assembly = AppDomain.CurrentDomain.GetAssemblies().ToList().Find(a => a.FullName.Split(',')[0] == config.ProviderName); if (assembly == null) { assembly = Assembly.Load(config.ProviderName); } var type = assembly.GetType(config.FactoryClient, false); object instance = null; if (type != null) { instance = Activator.CreateInstance(type); } BaseCache.Set <object>(config.ProviderName, instance); return(instance as DbProviderFactory); } } catch { return(null); } }
/// <summary> /// 是否存在 /// </summary> public static bool Exists(string cacheType, string key) { if (string.Compare(cacheType, CacheType.Web, true) == 0) { return(BaseCache.Exists(key)); } else if (string.Compare(cacheType, CacheType.Redis, true) == 0) { return(IRedis.ExistsAsy(key).Result); } return(false); }
/// <summary> /// 是否存在 /// </summary> public static bool Exists(string cacheType, string key) { if (cacheType.ToLower() == CacheType.Web) { return(BaseCache.Exists(key)); } else if (cacheType.ToLower() == CacheType.Redis) { return(IRedis.ExistsAsy(key).Result); } return(false); }
// 构建函数 static DynamicGet() { var key = string.Format("DynamicGet<T>.{0}.{1}", typeof(T)?.Namespace, typeof(T).Name); if (!BaseCache.Exists(key)) { GetValueDelegate = GenerateGetValue(); BaseCache.Set <object>(key, GetValueDelegate); } else { GetValueDelegate = BaseCache.Get <object>(key) as Func <object, string, object>; } }
/// <summary> /// 动态加载数据库工厂 /// </summary> /// <param name="providerInvariantName"></param> /// <param name="dbType"></param> /// <returns></returns> public static DbProviderFactory GetFactory(string dbKey, bool IsResource = false, string dbFile = "db.json") { try { var config = new ConfigModel(); if (IsResource) { var projectName = Assembly.GetCallingAssembly().GetName().Name; config = DataConfig.Get(dbKey, projectName, dbFile); } else { config = DataConfig.Get(dbKey); } if (BaseCache.Exists(config.ProviderName)) { return(BaseCache.Get <object>(config.ProviderName) as DbProviderFactory); } else { var assembly = AppDomain.CurrentDomain.GetAssemblies().ToList().Find(a => a.FullName.Split(',')[0] == config.ProviderName); if (assembly == null) { assembly = Assembly.Load(config.ProviderName); } var type = assembly.GetType(config.FactoryClient, false); object instance = null; if (type != null) { instance = Activator.CreateInstance(type); } BaseCache.Set <object>(config.ProviderName, instance); return(instance as DbProviderFactory); } } catch (Exception ex) { BaseLog.SaveLog(ex.Message + ex.StackTrace, "GetFactory"); return(null); } }
/// <summary> /// 请求过滤器 /// </summary> /// <param name="filterContext"></param> public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); var controller = filterContext.Controller as Controller; #region model验证处理 if (!controller.ModelState.IsValid) { var item = controller.ViewData.ModelState.Values.ToList().Find(a => a.Errors.Count > 0); var error = item.Errors.Where(a => !string.IsNullOrEmpty(a.ErrorMessage)).Take(1).SingleOrDefault().ErrorMessage; filterContext.Result = new JsonResult(new { success = false, msg = error }); return; } #endregion foreach (var item in controller.ControllerContext.ActionDescriptor.FilterDescriptors.ToList()) { if (item.Filter is AllowAnonymousFilter) { return; } } #region 登陆验证 if (!BaseCache.Exists(App.Cache.UserInfo)) { filterContext.Result = new RedirectToActionResult("login", "Home", "default"); return; } else { var dic = BaseCache.Get <Dictionary <string, object> >(App.Cache.UserInfo); if (dic.GetValue("ip").ToStr() != App.Ip.Get(filterContext.HttpContext)) { filterContext.Result = new RedirectToActionResult("login", "Home", "default"); } } #endregion }
/// <summary> /// 泛型缓存属性成员 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public static List <PropertyInfo> GetPropertyInfo <T>(bool IsCache = true) { var key = string.Format("{0}.{1}", typeof(T).Namespace, typeof(T).Name); if (IsCache) { if (BaseCache.Exists(key)) { return(BaseCache.Get <List <PropertyInfo> >(key)); } else { var info = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).ToList(); BaseCache.Set <List <PropertyInfo> >(key, info); return(info); } } else { BaseCache.Remove(key); return(typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).ToList()); } }
/// <summary> /// 泛型缓存属性成员 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public static List <PropertyInfo> PropertyInfo <T>(bool IsCache = true) { var key = string.Format("{0}.to.{1}", typeof(T).Namespace, typeof(T).Name); if (IsCache) { if (BaseCache.Exists(key)) { return(BaseCache.Get <List <PropertyInfo> >(key)); } else { var info = typeof(T).GetProperties().ToList(); BaseCache.Set <List <PropertyInfo> >(key, info); return(info); } } else { return(typeof(T).GetProperties().ToList()); } }
/// <summary> /// 获取配置文件 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key">键名</param> /// <returns></returns> public static List <T> GetListValue <T>(string key, string fileName = "appsettings.json", bool isCache = true) where T : class, new() { var cacheKey = string.Format("json.{0}.{1}", key, fileName); if (isCache && BaseCache.Exists(cacheKey)) { return(BaseCache.Get <List <T> >(cacheKey)); } var build = new ConfigurationBuilder(); build.SetBasePath(Directory.GetCurrentDirectory()); build.AddJsonFile(fileName, optional: true, reloadOnChange: true); var config = build.Build(); var list = new ServiceCollection().AddOptions().Configure <List <T> >(config.GetSection(key)).BuildServiceProvider().GetService <IOptions <List <T> > >().Value; if (isCache) { BaseCache.Set <List <T> >(cacheKey, list); } return(list); }
/// <summary> /// 获取配置实体 /// </summary> /// <param name="key"></param> /// <returns></returns> private static ConfigModel GetConfig(string key = null) { var list = new List <ConfigModel>(); var cacheKey = key == null ? "FastApi.Config" : string.Format("FastApi.Config.{0}", key); if (BaseCache.Exists(cacheKey)) { list = BaseCache.Get <List <ConfigModel> >(cacheKey); } else { list = BaseConfig.GetListValue <ConfigModel>("DataConfig", "db.json"); BaseCache.Set <List <ConfigModel> >(cacheKey, list); } if (string.IsNullOrEmpty(key)) { return(list[0]); } else { return(list.Find(a => a.Key.ToLower() == key.ToLower())); } }