예제 #1
0
 /// <summary>
 /// 获取值 item
 /// </summary>
 /// <typeparam name="T">泛型</typeparam>
 /// <param name="key">键</param>
 /// <returns></returns>
 public static T Get <T>(string key, int db = 0) where T : class, new()
 {
     try
     {
         db = db == 0 ? _db : db;
         if (string.IsNullOrEmpty(key))
         {
             return(new T());
         }
         else
         {
             return(BaseJson.JsonToModel <T>(Context.GetDatabase(db).StringGet(key)) ?? new T());
         }
     }
     catch (RedisException ex)
     {
         SaveLog <T>(ex, "Get<T>");
         return(new T());
     }
 }
예제 #2
0
 /// <summary>
 /// 获取值 item asy
 /// </summary>
 /// <typeparam name="T">泛型</typeparam>
 /// <param name="key">键</param>
 /// <returns></returns>
 public static async Task <T> GetAsy <T>(string key, int db = 0) where T : class, new()
 {
     try
     {
         db = db == 0 ? _db : db;
         if (string.IsNullOrEmpty(key))
         {
             return(new T());
         }
         else
         {
             return(BaseJson.JsonToModel <T>(await Context.GetDatabase(db).StringGetAsync(key).ConfigureAwait(false)) ?? new T());
         }
     }
     catch (RedisException ex)
     {
         SaveLog <T>(ex, "Get<T>");
         return(new T());
     }
 }
예제 #3
0
        public static void InstanceMapResource(string dbKey = null, string dbFile = "db.json", string mapFile = "map.json")
        {
            var projectName = Assembly.GetCallingAssembly().GetName().Name;
            var config      = DataConfig.Get(dbKey, projectName, dbFile);
            var db          = new DataContext(dbKey);
            var assembly    = Assembly.Load(projectName);
            var map         = new MapConfigModel();

            using (var resource = assembly.GetManifestResourceStream(string.Format("{0}.{1}", projectName, mapFile)))
            {
                if (resource != null)
                {
                    using (var reader = new StreamReader(resource))
                    {
                        var content = reader.ReadToEnd();
                        map.Path = BaseJson.JsonToModel <List <string> >(BaseJson.JsonToDic(BaseJson.ModelToJson(BaseJson.JsonToDic(content).GetValue(AppSettingKey.Map))).GetValue("Path").ToStr());
                    }
                }
                else
                {
                    map = BaseConfig.GetValue <MapConfigModel>(AppSettingKey.Map, mapFile);
                }
            }

            if (map.Path == null)
            {
                return;
            }

            map.Path.ForEach(a =>
            {
                using (var resource = assembly.GetManifestResourceStream(string.Format("{0}.{1}", projectName, a.Replace("/", "."))))
                {
                    var xml = "";
                    if (resource != null)
                    {
                        using (var reader = new StreamReader(resource))
                        {
                            xml = reader.ReadToEnd();
                        }
                    }
                    var info = new FileInfo(a);
                    var key  = BaseSymmetric.Generate(info.FullName);
                    if (!DbCache.Exists(config.CacheType, key))
                    {
                        var temp       = new MapXmlModel();
                        temp.LastWrite = info.LastWriteTime;
                        temp.FileKey   = MapXml.ReadXml(info.FullName, config, info.Name.ToLower().Replace(".xml", ""), xml);
                        temp.FileName  = info.FullName;
                        if (MapXml.SaveXml(dbKey, key, info, config, db))
                        {
                            DbCache.Set <MapXmlModel>(config.CacheType, key, temp);
                        }
                    }
                    else if ((DbCache.Get <MapXmlModel>(config.CacheType, key).LastWrite - info.LastWriteTime).Milliseconds != 0)
                    {
                        DbCache.Get <MapXmlModel>(config.CacheType, key).FileKey.ForEach(f => { DbCache.Remove(config.CacheType, f); });

                        var model       = new MapXmlModel();
                        model.LastWrite = info.LastWriteTime;
                        model.FileKey   = MapXml.ReadXml(info.FullName, config, info.Name.ToLower().Replace(".xml", ""), xml);
                        model.FileName  = info.FullName;
                        if (MapXml.SaveXml(dbKey, key, info, config, db))
                        {
                            DbCache.Set <MapXmlModel>(config.CacheType, key, model);
                        }
                    }
                }
            });
        }