public async Task <IEnumerable <Product> > GetAllProductsAsync(
            ProductFilterParams filter        = null,
            PaginationParams paginationParams = null)
        {
            if (filter == null && paginationParams != null)
            {
                //magic number: most accessed pagination pattern is X page 9 size
                //sake of preventing exploitation this solution works goodish
                if (paginationParams.PageSize % 9 != 0)
                {
                    var cacheKey = $"productPlainPage:{paginationParams.PageNumber}:{paginationParams.PageSize}";

                    var result = await _distributedCache.GetAsync <IEnumerable <Product> >(cacheKey);

                    if (result == null)
                    {
                        result = await _productRepository.GetAllProductsAsync(null, paginationParams);

                        var cacheOptions = _distributedCache
                                           .CacheOptions()
                                           .FromConfiguration(_configuration, "ProductCache");

                        await _distributedCache.SetAsync(cacheKey, result, cacheOptions);
                    }

                    return(result);
                }
            }

            return(await _productRepository.GetAllProductsAsync(filter, paginationParams));
        }
예제 #2
0
        public async Task <Customer> GetCustomerByIdAsync(int customerId)
        {
            var cacheKey = $"customer:{customerId}";
            var customer = await _distributedCache.GetAsync <Customer>(cacheKey);

            if (customer == null)
            {
                customer = await _customerRepository.GetCustomerByIdAsync(customerId);

                var cacheOptions = _distributedCache
                                   .CacheOptions()
                                   .FromConfiguration(_configuration, "CustomerCache");

                await _distributedCache.SetAsync(cacheKey, customer, cacheOptions);
            }

            return(customer);
        }
예제 #3
0
        public async Task <string> CreateTokenAsync(int actorId)
        {
            string token      = Guid.NewGuid().ToString();
            var    expiration = TimeSpan.FromMinutes(int.Parse(_configuration["TokenService:TokenExpireInMinutes"]));

            var cacheIdFromTokenKey = $"token:actorId:{token}";
            var cacheTokenFromIdKey = $"token:actorToken:{actorId}";

            var cacheOptions = _distributedCache
                               .CacheOptions()
                               .FromConfiguration(_configuration, "TokenCache");

            await _distributedCache.SetIntAsync(cacheIdFromTokenKey, actorId, cacheOptions);

            await _distributedCache.SetStringAsync(cacheTokenFromIdKey, token, cacheOptions);

            return(token);
        }
        public async Task <Account> GetAccountByIdAsync(int accountId)
        {
            var cacheKey = $"account:{accountId}";
            var account  = await _distributedCache.GetAsync <Account>(cacheKey);

            if (account == null)
            {
                account = await _accountRepository.GetAccountByIdAsync(accountId);

                var cacheOptions = _distributedCache
                                   .CacheOptions()
                                   .FromConfiguration(_configuration, "AccountCache");

                await _distributedCache.SetAsync(cacheKey, account, cacheOptions);
            }

            return(account);
        }