/// <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 void Set(string cacheType, string key, string value, int Hours = 8640) { if (string.Compare(cacheType, CacheType.Web, true) == 0) { BaseCache.Set(key, value, Hours); } else if (string.Compare(cacheType, CacheType.Redis, true) == 0) { IRedis.SetAsy(key, value, Hours); } }
/// <summary> /// 设置缓存 /// </summary> public static void Set <T>(string cacheType, string key, T value, int Hours = 8640) where T : class, new() { if (string.Compare(cacheType, CacheType.Web, true) == 0) { BaseCache.Set <T>(key, value, Hours); } else if (string.Compare(cacheType, CacheType.Redis, true) == 0) { IRedis.SetAsy <T>(key, value, Hours); } }
/// <summary> /// 设置缓存 /// </summary> public static void Set <T>(string cacheType, string key, T value, int Hours = 8640) where T : class, new() { if (cacheType.ToLower() == CacheType.Web) { BaseCache.Set <T>(key, value, Hours); } else if (cacheType.ToLower() == CacheType.Redis) { IRedis.SetAsy <T>(key, value, Hours); } }
/// <summary> /// 设置缓存 /// </summary> public static void Set(string cacheType, string key, string value, int Hours = 8640) { if (cacheType.ToLower() == CacheType.Web) { BaseCache.Set(key, value, Hours); } else if (cacheType.ToLower() == CacheType.Redis) { IRedis.SetAsy(key, value, Hours); } }
// 构建函数 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> /// <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); }
public IActionResult Login(LoginModel item) { var isSuccess = false; using (var db = new DataContext(App.DbKey.Api)) { var info = FastRead.Query <ApiGatewayLogin>(a => a.UserName.ToLower() == item.Code.ToLower()).ToDic(db); isSuccess = BaseSymmetric.Generate(item.Pwd).ToLower() == info.GetValue("UserPwd").ToStr().ToLower(); info.Add("ip", App.Ip.Get(HttpContext)); if (isSuccess) { BaseCache.Set <Dictionary <string, object> >(App.Cache.UserInfo, info); return(Json(new { success = isSuccess, url = "/home/index" })); } else { return(Json(new { success = isSuccess, msg = "密码不正确" })); } } }
/// <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())); } }