コード例 #1
0
        /// <summary>
        /// 获取或设置缓存对象。
        /// </summary>
        /// <typeparam name="TCache">当前缓存对象类型。</typeparam>
        /// <param name="key">缓存唯一键。</param>
        /// <param name="dependency">缓存依赖项。</param>
        /// <param name="action">获取和配置缓存实例。</param>
        /// <returns>返回当前缓存对象。</returns>
        public TCache GetOrCreate <TCache>(object key, IStorageCacheDependency dependency, Func <IStorageContext, TCache> action)
        {
            var cache = GetCache(key, dependency);

            if (cache == null)
            {
                var cacheKey = GetCacheKey(key);
                var context  = new StorageContext(cacheKey);
                var instance = action(context);
                cache = new StorageCache
                {
                    CacheKey    = cacheKey,
                    Dependency  = dependency?.ToString(),
                    ExpiredDate = context.ExpiredDate
                };
                _db.Create(cache);
                StorageHelper.SaveText(GetFilePath(cacheKey, true), instance.ToJsonString());
                return(instance);
            }
            var path = GetFilePath(cache.CacheKey);

            if (!File.Exists(path))//缓存文件不存在
            {
                Remove(cache.CacheKey);
                return(GetOrCreate(key, dependency, action));
            }
            return(Cores.FromJsonString <TCache>(StorageHelper.ReadText(path)));
        }
コード例 #2
0
        /// <summary>
        /// 获取或设置缓存对象。
        /// </summary>
        /// <typeparam name="TCache">当前缓存对象类型。</typeparam>
        /// <param name="key">缓存唯一键。</param>
        /// <param name="dependency">缓存依赖项。</param>
        /// <param name="action">获取和配置缓存实例。</param>
        /// <returns>返回当前缓存对象。</returns>
        public virtual async Task <TCache> GetOrCreateAsync <TCache>(object key, IStorageCacheDependency dependency, Func <IStorageContext, Task <TCache> > action)
        {
            var cache = await GetCacheAsync(key, dependency);

            if (cache == null)
            {
                var cacheKey = GetCacheKey(key);
                var context  = new StorageContext(cacheKey);
                var instance = await action(context);

                cache = new StorageCache
                {
                    CacheKey    = cacheKey,
                    Dependency  = dependency?.ToString(),
                    ExpiredDate = context.ExpiredDate
                };
                await _db.CreateAsync(cache);

                await StorageHelper.SaveTextAsync(GetFilePath(cacheKey, true), instance.ToJsonString());

                return(instance);
            }
            var path = GetFilePath(cache.CacheKey);

            if (!File.Exists(path))//缓存文件不存在
            {
                await RemoveAsync(cache.CacheKey);

                return(await GetOrCreateAsync(key, dependency, action));
            }
            return(Cores.FromJsonString <TCache>(await StorageHelper.ReadTextAsync(path)));
        }
コード例 #3
0
ファイル: Argument.cs プロジェクト: ljin518yi/aspnetcore
        internal Argument(string arguments)
        {
            if (string.IsNullOrWhiteSpace(arguments))
            {
                return;
            }
            var data = Cores.FromJsonString <Dictionary <string, object> >(arguments);

            foreach (var o in data)
            {
                _arguments[o.Key] = o.Value;
            }
        }
コード例 #4
0
 /// <summary>
 /// 获取网站配置实例。
 /// </summary>
 /// <typeparam name="TSiteSettings">网站配置类型。</typeparam>
 /// <param name="key">配置唯一键。</param>
 /// <returns>返回网站配置实例。</returns>
 public virtual Task <TSiteSettings> GetSettingsAsync <TSiteSettings>(string key) where TSiteSettings : class, new()
 {
     return(Cache.GetOrCreateAsync(key, async entry =>
     {
         entry.SetDefaultAbsoluteExpiration();
         var settings = await Context.FindAsync(x => x.SettingKey == key);
         if (settings?.SettingValue == null)
         {
             return new TSiteSettings();
         }
         return Cores.FromJsonString <TSiteSettings>(settings.SettingValue);
     }));
 }
コード例 #5
0
 /// <summary>
 /// 获取网站配置实例。
 /// </summary>
 /// <typeparam name="TSiteSettings">网站配置类型。</typeparam>
 /// <param name="key">配置唯一键。</param>
 /// <returns>返回网站配置实例。</returns>
 public virtual TSiteSettings GetSettings <TSiteSettings>(string key) where TSiteSettings : class, new()
 {
     return(Cache.GetOrCreate(key, entry =>
     {
         entry.SetDefaultAbsoluteExpiration();
         var settings = Context.Find(x => x.SettingKey == key)?.SettingValue;
         if (settings == null)
         {
             return new TSiteSettings();
         }
         return Cores.FromJsonString <TSiteSettings>(settings);
     }));
 }
コード例 #6
0
ファイル: AppDataManager.cs プロジェクト: onetcore/gentings
        /// <summary>
        /// 加载配置。
        /// </summary>
        /// <typeparam name="TModel">配置类型。</typeparam>
        /// <param name="name">名称,不包含文件扩展名。</param>
        /// <param name="minutes">缓存分钟数。</param>
        /// <returns>返回配置实例。</returns>
        public virtual async Task <TModel> LoadDataAsync <TModel>(string name, int minutes = -1)
        {
            if (minutes <= 0)
            {
                return(Cores.FromJsonString <TModel>(await LoadFileAsync(name)));
            }

            return(await _cache.GetOrCreateAsync(GetCacheKey(name), async ctx =>
            {
                ctx.SetAbsoluteExpiration(TimeSpan.FromMinutes(minutes));
                return Cores.FromJsonString <TModel>(await LoadFileAsync(name));
            }));
        }
コード例 #7
0
ファイル: AppDataManager.cs プロジェクト: onetcore/gentings
        /// <summary>
        /// 加载配置。
        /// </summary>
        /// <typeparam name="TModel">配置类型。</typeparam>
        /// <param name="name">名称,不包含文件扩展名。</param>
        /// <param name="minutes">缓存分钟数。</param>
        /// <returns>返回配置实例。</returns>
        public virtual TModel LoadData <TModel>(string name, int minutes = -1)
        {
            if (minutes <= 0)
            {
                return(Cores.FromJsonString <TModel>(LoadFile(name)));
            }

            return(_cache.GetOrCreate(GetCacheKey(name), ctx =>
            {
                ctx.SetAbsoluteExpiration(TimeSpan.FromMinutes(minutes));
                return Cores.FromJsonString <TModel>(LoadFile(name));
            }));
        }
コード例 #8
0
        /// <summary>
        /// 将当前对象转换为<typeparamref name="TSite"/>类型实例。
        /// </summary>
        /// <typeparam name="TSite">网站类型实例。</typeparam>
        /// <returns>返回当前网站实例对象。</returns>
        public TSite AsSite <TSite>() where TSite : Site, new()
        {
            var site = Cores.FromJsonString <TSite>(SettingValue) ?? new TSite();

            site.Description = Description;
            site.Disabled    = Disabled;
            site.Id          = Id;
            site.SiteKey     = SiteKey;
            site.SiteName    = SiteName;
            site.ShortName   = ShortName ?? SiteName;
            site.CreatedDate = CreatedDate;
            site.UserId      = UserId;
            return(site);
        }
コード例 #9
0
        /// <summary>
        /// 获取网站配置实例。
        /// </summary>
        /// <typeparam name="TSiteSettings">网站配置类型。</typeparam>
        /// <param name="userId">用户Id。</param>
        /// <param name="key">配置唯一键。</param>
        /// <returns>返回网站配置实例。</returns>
        public virtual TSiteSettings GetSettings <TSiteSettings>(int userId, string key) where TSiteSettings : class, new()
        {
            return(Cache.GetOrCreate(GetCacheKey(userId, key), entry =>
            {
                entry.SetDefaultAbsoluteExpiration();
                var settings = Context.Find(x => x.UserId == userId && x.SettingKey == key)?.SettingValue;
                if (settings == null)//用户配置未获取实例,则返回系统设定的配置
                {
                    return _settingsManager.GetSettings <TSiteSettings>(key);
                }

                return Cores.FromJsonString <TSiteSettings>(settings);
            }));
        }
コード例 #10
0
 /// <summary>
 /// 发送数据。
 /// </summary>
 /// <typeparam name="TResult">返回的结果类型。</typeparam>
 /// <param name="api">API地址。</param>
 /// <returns>返回发送结果。</returns>
 protected virtual Task <ServiceDataResult <TResult> > GetAsync <TResult>(string api)
 {
     return(CatchExecuteAsync(async() =>
     {
         var response = await Client.GetAsync(api);
         response.EnsureSuccessStatusCode();
         if (response.IsSuccessStatusCode)
         {
             var result = await response.Content.ReadAsStringAsync();
             return Cores.FromJsonString <ServiceDataResult <TResult> >(result);
         }
         //如果不成功,则返回状态码
         return HandleFailuredAsync <ServiceDataResult <TResult> >(response.StatusCode);
     }));
 }
コード例 #11
0
 /// <summary>
 /// 发送数据。
 /// </summary>
 /// <param name="api">API地址。</param>
 /// <param name="data">数据实例。</param>
 /// <returns>返回发送结果。</returns>
 protected virtual Task <ServiceResult> PostAsync(string api, object data = null)
 {
     return(CatchExecuteAsync(async() =>
     {
         var response = await Client.PostAsync(api, new StringContent(data.ToJsonString() ?? "{}"));
         response.EnsureSuccessStatusCode();
         if (response.IsSuccessStatusCode)
         {
             var result = await response.Content.ReadAsStringAsync();
             return Cores.FromJsonString <ServiceResult>(result);
         }
         //如果不成功,则返回状态码
         return HandleFailuredAsync <ServiceResult>(response.StatusCode);
     }));
 }
コード例 #12
0
ファイル: ClientBase.cs プロジェクト: ljin518yi/aspnetcore
        private async Task <TResult> ExecuteAsync <TResult>(string url, Func <HttpClient, string, Task <string> > action, int times)
            where TResult : ClientResult, new()
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("x-client", ClientName);
                client.DefaultRequestHeaders.Add("x-appid", AppId);
                if (Token != null)
                {
                    client.DefaultRequestHeaders.Add("x-token", Token);
                }
                string result = null;
                try
                {
                    result = await action(client, url);

                    if (string.IsNullOrEmpty(result))
                    {
                        return new TResult {
                                   Code = (int)ErrorCode.Failured, Msg = Resources.ErrorCode_Failured
                        }
                    }
                    ;
                    return(Cores.FromJsonString <TResult>(result));
                }
                catch (Exception exception)
                {
                    if (times < 3)
                    {
                        await Task.Delay(Delay * 1000);

                        return(await ExecuteAsync <TResult>(url, action, times + 1));
                    }
                    _logger.LogError(exception, @"请求API失败:{1}
--------------------------------------------------
地址:{2}
--------------------------------------------------
结果:{3}
==================================================
错误:{4}", exception.Message, url, result, exception.StackTrace);
                }
            }
            return(new TResult {
                Code = (int)ErrorCode.UnknownError, Msg = Resources.ErrorCode_UnknownError
            });
        }
コード例 #13
0
ファイル: EpubManager.cs プロジェクト: 346662108/aspnetcore
        /// <summary>
        /// 加载电子书解压后得物理文件夹。
        /// </summary>
        /// <param name="bookId">电子书Id。</param>
        /// <returns>返回电子书组成配置文件实例。</returns>
        public IEpubFile Create(string bookId)
        {
            var path     = GetEpubPath(bookId);
            var jsonPath = Path.Combine(path, "epub.json");

            if (File.Exists(jsonPath))
            {
                var file = Cores.FromJsonString <EpubFile>(StorageHelper.ReadText(jsonPath));
                if (file != null)
                {
                    return(new EpubFileEntry(file, path));
                }
            }
            var instance = new EpubFile {
                BookId = bookId
            };

            StorageHelper.SaveText(jsonPath, instance.ToJsonString());
            return(new EpubFileEntry(instance, path));
        }
コード例 #14
0
        /// <summary>
        /// 获取网站配置实例。
        /// </summary>
        /// <typeparam name="TSiteSettings">网站配置类型。</typeparam>
        /// <param name="siteId">网站Id。</param>
        /// <param name="key">配置唯一键。</param>
        /// <returns>返回网站配置实例。</returns>
        public virtual TSiteSettings GetSettings <TSiteSettings>(int siteId, string key) where TSiteSettings : ISite, new()
        {
            return(Cache.GetOrCreate(GetCacheKey(siteId, key), entry =>
            {
                entry.SetDefaultAbsoluteExpiration();
                var settings = Context.Find(x => x.SiteId == siteId && x.SettingKey == key)?.SettingValue;
                if (settings == null)
                {
                    return new TSiteSettings {
                        SiteId = siteId
                    }
                }
                ;

                var result = Cores.FromJsonString <TSiteSettings>(settings);
                if (result == null)
                {
                    result = new TSiteSettings();
                }
                result.SiteId = siteId;
                return result;
            }));
        }
コード例 #15
0
        /// <summary>
        /// 获取网站配置实例。
        /// </summary>
        /// <typeparam name="TSiteSettings">网站配置类型。</typeparam>
        /// <param name="siteId">网站Id。</param>
        /// <param name="key">配置唯一键。</param>
        /// <returns>返回网站配置实例。</returns>
        public virtual Task <TSiteSettings> GetSettingsAsync <TSiteSettings>(int siteId, string key) where TSiteSettings : ISite, new()
        {
            return(Cache.GetOrCreateAsync(GetCacheKey(siteId, key), async entry =>
            {
                entry.SetDefaultAbsoluteExpiration();
                var settings = await Context.FindAsync(x => x.SettingKey == key);
                if (settings?.SettingValue == null)
                {
                    return new TSiteSettings {
                        SiteId = siteId
                    }
                }
                ;

                var result = Cores.FromJsonString <TSiteSettings>(settings.SettingValue);
                if (result == null)
                {
                    result = new TSiteSettings();
                }
                result.SiteId = siteId;
                return result;
            }));
        }
コード例 #16
0
        /// <summary>
        /// 获取注册码。
        /// </summary>
        /// <returns>返回注册码实例。</returns>
        public async Task <Registration> GetRegistrationAsync()
        {
            var registions = await _context.FetchAsync();

            if (registions.Any())
            {
                try
                {
                    var code = registions.First().Registration;
                    code = Cores.Decrypto(code.Trim());
                    return(Cores.FromJsonString <Registration>(code));
                }
                catch
                {
                    // ignored
                }
            }

            var registration = new Registration();

            await SaveRegistrationAsync(registration);

            return(registration);
        }