Пример #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;
        }
Пример #2
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;
        }
Пример #3
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;
}