Exemplo n.º 1
0
        /// <summary>
        /// 将枚举转换为键值对集合
        /// </summary>
        /// <param name="enumType">枚举类型</param>
        /// <returns>key:枚举name,值:枚举value</returns>
        public async static Task<Dictionary<int, string>> EnumToDictionary(this Type enumType)
        {
            if (!enumType.IsEnum)
            {
                //传入的不是枚举
                throw new AggregateException();
            }

            Array enumValueArray = Enum.GetValues(enumType);

            string cacheKey = enumType.Name + "_EnumToDictionary_cache";
            AsyncThreadSafeObjectCache<Dictionary<int, string>> cache = new AsyncThreadSafeObjectCache<Dictionary<int, string>>(MemoryCache.Default, TimeSpan.FromSeconds(120));
            Dictionary<int, string> enumDict = await cache.GetAsync(cacheKey, () =>
            {
                enumDict = new Dictionary<int, string>();
                foreach (var enumValue in enumValueArray)
                {
                    int key = Convert.ToInt32(enumValue);//value值
                    string value = Enum.GetName(enumType, enumValue).InnerGetDescription(enumType);
                    enumDict.Add(key, value);
                }
                cache.Set(cacheKey, enumDict);
                return Task.FromResult(enumDict);
            });

            return enumDict;
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public SettingManager(ISettingDefinitionManager settingDefinitionManager)
        {
            _settingDefinitionManager = settingDefinitionManager;

            AbpSession   = NullAbpSession.Instance;
            SettingStore = NullSettingStore.Instance;                                                                                                                                     //Should be constructor injection? For that, ISettingStore must be registered!

            _applicationSettings = new Lazy <Dictionary <string, SettingInfo> >(() => AsyncHelper.RunSync(GetApplicationSettingsFromDatabase), true);                                     //TODO: Run async
            _tenantSettingCache  = new AsyncThreadSafeObjectCache <Dictionary <string, SettingInfo> >(new MemoryCache(GetType().FullName + ".TenantSettings"), TimeSpan.FromMinutes(60)); //TODO: Get constant from somewhere else.
            _userSettingCache    = new AsyncThreadSafeObjectCache <Dictionary <string, SettingInfo> >(new MemoryCache(GetType().FullName + ".UserSettings"), TimeSpan.FromMinutes(20));   //TODO: Get constant from somewhere else.
        }
        /// <inheritdoc/>
        public SettingManager(ISettingDefinitionManager settingDefinitionManager)
        {
            _settingDefinitionManager = settingDefinitionManager;

            AbpSession = NullAbpSession.Instance;
            SettingStore = DefaultConfigSettingStore.Instance;

            _applicationSettings = new Lazy<Dictionary<string, SettingInfo>>(() => AsyncHelper.RunSync(GetApplicationSettingsFromDatabase), true); //TODO: Run async
            _tenantSettingCache = new AsyncThreadSafeObjectCache<Dictionary<string, SettingInfo>>(new MemoryCache(GetType().FullName + ".TenantSettings"), TimeSpan.FromMinutes(60)); //TODO: Get constant from somewhere else.
            _userSettingCache = new AsyncThreadSafeObjectCache<Dictionary<string, SettingInfo>>(new MemoryCache(GetType().FullName + ".UserSettings"), TimeSpan.FromMinutes(20)); //TODO: Get constant from somewhere else.
        }
Exemplo n.º 4
0
        /// <inheritdoc/>
        public SettingManager(ISettingDefinitionManager settingDefinitionManager)
        {
            _settingDefinitionManager = settingDefinitionManager;

            AbpSession   = NullAbpSession <TTenantId, TUserId> .Instance;
            SettingStore = DefaultConfigSettingStore <TTenantId, TUserId> .Instance;

            _applicationSettings = new Lazy <Dictionary <string, SettingInfo <TTenantId, TUserId> > >(() => AsyncHelper.RunSync(GetApplicationSettingsFromDatabase), true);                                     //TODO: Run async
            _tenantSettingCache  = new AsyncThreadSafeObjectCache <Dictionary <string, SettingInfo <TTenantId, TUserId> > >(new MemoryCache(GetType().FullName + ".TenantSettings"), TimeSpan.FromMinutes(60)); //TODO: Get constant from somewhere else.
            _userSettingCache    = new AsyncThreadSafeObjectCache <Dictionary <string, SettingInfo <TTenantId, TUserId> > >(new MemoryCache(GetType().FullName + ".UserSettings"), TimeSpan.FromMinutes(20));   //TODO: Get constant from somewhere else.
        }
Exemplo n.º 5
0
        public async Task<GetCategoryOutput> GetAllByTaxonomy(TaxonomyEnum taxonomy)
        {
            string cacheKey = "GetAllByTaxonomy_"+Enum.GetName(typeof(TaxonomyEnum), taxonomy);
            //创建缓存对象
            AsyncThreadSafeObjectCache<GetCategoryOutput> cache =
                new AsyncThreadSafeObjectCache<GetCategoryOutput>(System.Runtime.Caching.MemoryCache.Default, TimeSpan.FromSeconds(120));
            GetCategoryOutput output = await cache.GetAsync(cacheKey, async () => 
            {
                var tagCategory = await _categoryRepository.GetAllListAsync(w => w.Taxonomy == taxonomy);
                output = new GetCategoryOutput
                {
                    CategoryCollection = Mapper.Map<List<CategoryDto>>(tagCategory)
                };
                //插入到缓存
                cache.Set(cacheKey, output);
                //返回数据
                return output;
            });

            return output;
        }
Exemplo n.º 6
0
/// <summary>
/// 获取前10篇文章
/// </summary>
/// <returns></returns>
public async Task<GetTopTenOutput> GetTopTen()
{
    //从缓存获取数据
    const string cacheKey = "GetTopTen";

    AsyncThreadSafeObjectCache<GetTopTenOutput> cache = new AsyncThreadSafeObjectCache<GetTopTenOutput>(MemoryCache.Default, TimeSpan.FromSeconds(120));
    GetTopTenOutput output = await cache.GetAsync(cacheKey, async () =>
   {
               //获取前10条数据
               var posts = await this._postRepository.GetEnumerableTopTen(10);

       output = new GetTopTenOutput
       {
           Items = posts.MapTo<IReadOnlyList<TopTenPostDto>>()
       };
       cache.Set(cacheKey, output);
       return output;
   });

    if (output == null)//缓存不存在数据
    {

    }

    return output;
}