示例#1
0
        private async Task <List <RelationDto> > GetAllRelations()
        {
            var cahceValue = await _cache.GetAsync(EasyCachingConsts.MenuRelationCacheKey, async() =>
            {
                var allRelations = await _relationRepository.GetAll().ToListAsync();
                return(_mapper.Map <List <RelationDto> >(allRelations));
            }, TimeSpan.FromSeconds(EasyCachingConsts.OneYear));

            return(cahceValue.Value);
        }
示例#2
0
        private async Task <List <DeptDto> > GetAll()
        {
            var tempcache = await _cache.GetAsync <List <DeptDto> >(EasyCachingConsts.DetpListCacheKey);

            var cahceValue = await _cache.GetAsync(EasyCachingConsts.DetpListCacheKey, async() =>
            {
                var allDepts = await _deptRepository.GetAll().ToListAsync();
                return(_mapper.Map <List <DeptDto> >(allDepts));
            }, TimeSpan.FromSeconds(EasyCachingConsts.OneYear));

            return(cahceValue.Value);
        }
        public async Task <string> GetAsync(int type = 1)
        {
            if (type == 1)
            {
                await _provider.RemoveAsync("demo");

                return("removed");
            }
            else if (type == 2)
            {
                await _provider.SetAsync("demo", "123", TimeSpan.FromMinutes(1));

                return("seted");
            }
            else if (type == 3)
            {
                var res = await _provider.GetAsync("demo", async() => await Task.FromResult("456"), TimeSpan.FromMinutes(1));

                return($"cached value : {res}");
            }
            else
            {
                return("error");
            }
        }
        public async Task <string> GetAsync(string str)
        {
            var method = str.ToLower();

            switch (method)
            {
            case "get":
                var res = await _provider.GetAsync("demo", async() => await Task.FromResult("3-456"), TimeSpan.FromHours(1));

                return($"cached value : {res}");

            case "set":
                await _provider.SetAsync("demo", "3-123", TimeSpan.FromHours(1));

                return("seted");

            case "remove":
                await _provider.RemoveAsync("demo");

                return("removed");

            default:
                return("default");
            }
        }
示例#5
0
        public async Task <bool> ExistPermissions(RolePermissionsCheckInputDto inputDto)
        {
            bool result = false;

            var cahceValue = await _cache.GetAsync(EasyCachingConsts.MenuCodesCacheKey, async() =>
            {
                var allMenus = await _relationRepository.GetAll()
                               .Where(x => x.Menu.Status == true)
                               .Select(x => new RoleMenuCodesDto {
                    RoleId = x.RoleId, Code = x.Menu.Code
                })
                               .ToArrayAsync();
                return(allMenus.Distinct().ToArray());
            }, TimeSpan.FromSeconds(EasyCachingConsts.OneYear));

            var codes = cahceValue?.Value.Where(x => inputDto.RoleIds.Contains(x.RoleId)).Select(x => x.Code.ToUpper());

            if (codes.Any())
            {
                var ownerMenusTotal = codes.Intersect(inputDto.Permissions.Select(x => x.ToUpper()))
                                      .Count();

                if (ownerMenusTotal > 0)
                {
                    result = true;
                }
            }
            return(await Task.FromResult(result));
        }
示例#6
0
        public async Task <int> Delete(long Id)
        {
            var depts1 = (await _locaCahce.GetAsync <List <DeptNodeDto> >(EasyCachingConsts.DetpListCacheKey)).Value;
            var depts2 = (await _redisCache.GetAsync <List <DeptNodeDto> >(EasyCachingConsts.DetpListCacheKey)).Value;
            var depts3 = (await _cache.GetAsync <List <DeptNodeDto> >(EasyCachingConsts.DetpListCacheKey)).Value;

            int result = await _systemManagerService.DeleteDept(Id);

            return(await Task.FromResult(result));
        }
示例#7
0
        private async Task <List <CfgDto> > GetAllFromCache()
        {
            var cahceValue = await _cache.GetAsync(EasyCachingConsts.CfgListCacheKey, async() =>
            {
                var allCfgs = await _cfgRepository.GetAll().ToListAsync();
                return(_mapper.Map <List <CfgDto> >(allCfgs));
            }, TimeSpan.FromSeconds(EasyCachingConsts.OneYear));

            return(cahceValue.Value);
        }
示例#8
0
        public async Task <List <DeptDto> > GetAllFromCacheAsync()
        {
            var cahceValue = await _cache.GetAsync(EasyCachingConsts.DetpListCacheKey, async() =>
            {
                var allDepts = await _deptRepository.GetAll(writeDb: true).OrderBy(x => x.Ordinal).ToListAsync();
                return(_mapper.Map <List <DeptDto> >(allDepts));
            }, TimeSpan.FromSeconds(EasyCachingConsts.OneYear));

            return(cahceValue.Value);
        }
示例#9
0
        private async Task <List <DictDto> > GetAllFromCache()
        {
            var cahceValue = await _cache.GetAsync(EasyCachingConsts.DictListCacheKey, async() =>
            {
                var allDicts = await _dictRepository.GetAll(writeDb: true).ToListAsync();
                return(_mapper.Map <List <DictDto> >(allDicts));
            }, TimeSpan.FromSeconds(EasyCachingConsts.OneYear));

            return(cahceValue.Value);
        }
        public async Task <byte[]?> GetAsync(string key, CancellationToken token = default)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            var result = await _hybridCachingProvider.GetAsync <byte[]>(key).ConfigureAwait(false);

            return((!result.HasValue || result.IsNull) ? null : result.Value);
        }
示例#11
0
        public async Task <IEnumerable <string> > GetPermissions(RolePermissionsCheckInputDto inputDto)
        {
            var cahceValue = await _cache.GetAsync(EasyCachingConsts.MenuCodesCacheKey, async() =>
            {
                var allMenus = await _relationRepository.GetAll()
                               .Where(x => x.Menu.Status == true)
                               .Select(x => new RoleMenuCodesDto {
                    RoleId = x.RoleId, Code = x.Menu.Code
                })
                               .ToArrayAsync();
                return(allMenus.Distinct().ToArray());
            }, TimeSpan.FromSeconds(EasyCachingConsts.OneYear));

            var codes = cahceValue.Value?.Where(x => inputDto.RoleIds.Contains(x.RoleId)).Select(x => x.Code.ToUpper());

            if (codes != null && codes.Any())
            {
                return(codes.Intersect(inputDto.Permissions.Select(x => x.ToUpper())));
            }
            return(null);
        }
示例#12
0
        /// <summary>
        /// Proceeds the able.
        /// </summary>
        /// <param name="invocation">Invocation.</param>
        private void ProceedAble(IInvocation invocation)
        {
            var serviceMethod = invocation.Method ?? invocation.MethodInvocationTarget;

            if (GetMethodAttributes(serviceMethod).FirstOrDefault(x => typeof(EasyCachingAbleAttribute).IsAssignableFrom(x.GetType())) is EasyCachingAbleAttribute attribute)
            {
                var returnType = serviceMethod.IsReturnTask()
                        ? serviceMethod.ReturnType.GetGenericArguments().First()
                        : serviceMethod.ReturnType;

                var cacheKey = string.IsNullOrEmpty(attribute.CacheKey)
                    ? _keyGenerator.GetCacheKey(serviceMethod, invocation.Arguments, attribute.CacheKeyPrefix)
                    : attribute.CacheKey;

                object cacheValue  = null;
                var    isAvailable = true;
                try
                {
                    if (attribute.IsHybridProvider)
                    {
                        cacheValue = _hybridCachingProvider.GetAsync(cacheKey, returnType).GetAwaiter().GetResult();
                    }
                    else
                    {
                        var _cacheProvider = _cacheProviderFactory.GetCachingProvider(attribute.CacheProviderName ?? _options.Value.CacheProviderName);
                        cacheValue = _cacheProvider.GetAsync(cacheKey, returnType).GetAwaiter().GetResult();
                    }
                }
                catch (Exception ex)
                {
                    if (!attribute.IsHighAvailability)
                    {
                        throw;
                    }
                    else
                    {
                        isAvailable = false;
                        _logger?.LogError(new EventId(), ex, $"Cache provider get error.");
                    }
                }

                if (cacheValue != null)
                {
                    if (serviceMethod.IsReturnTask())
                    {
                        invocation.ReturnValue =
                            TypeofTaskResultMethod.GetOrAdd(returnType, t => typeof(Task).GetMethods().First(p => p.Name == "FromResult" && p.ContainsGenericParameters).MakeGenericMethod(returnType)).Invoke(null, new object[] { cacheValue });
                    }
                    else
                    {
                        invocation.ReturnValue = cacheValue;
                    }
                }
                else
                {
                    // Invoke the method if we don't have a cache hit
                    invocation.Proceed();

                    if (!string.IsNullOrWhiteSpace(cacheKey) && invocation.ReturnValue != null && isAvailable)
                    {
                        // get the result
                        var returnValue = serviceMethod.IsReturnTask()
                           ? invocation.UnwrapAsyncReturnValue().Result
                           : invocation.ReturnValue;

                        // should we do something when method return null?
                        // 1. cached a null value for a short time
                        // 2. do nothing
                        if (returnValue != null)
                        {
                            if (attribute.IsHybridProvider)
                            {
                                _hybridCachingProvider.Set(cacheKey, returnValue, TimeSpan.FromSeconds(attribute.Expiration));
                            }
                            else
                            {
                                var _cacheProvider = _cacheProviderFactory.GetCachingProvider(attribute.CacheProviderName ?? _options.Value.CacheProviderName);
                                _cacheProvider.Set(cacheKey, returnValue, TimeSpan.FromSeconds(attribute.Expiration));
                            }
                        }
                    }
                }
            }
            else
            {
                // Invoke the method if we don't have EasyCachingAbleAttribute
                invocation.Proceed();
            }
        }
示例#13
0
        public async Task <string> GetCachedResponseAsync(string cacheKey)
        {
            var cachedResponse = await _hybridCachingProvider.GetAsync <string>(cacheKey);

            return(string.IsNullOrWhiteSpace(cachedResponse.Value) ? null : cachedResponse.Value);
        }