Esempio n. 1
0
        public async Task <Product> FindProductByUrl(string url)
        {
            var cacheKey = Consts.GetCachePrefix(Consts.CachePrefix.ProductUrl, url);

            if (_cache.TryGetValue(cacheKey, out var product))
            {
                return(FreshenProduct(product as Product));
            }

            var newProduct = await _context.Products
                             .Where(p => p.Status != (int)Consts.Status.Deleted)
                             .Include(p => p.Category)
//                .Include(p => p.Brand)
                             .Include(p => p.Images)
                             .Include(p => p.Reviews)
                             .ThenInclude(r => r.User)
                             .Include(p => p.Extras)
                             .ThenInclude(extra => extra.Options)
                             .FirstOrDefaultAsync(p => p.NameUrl.Equals(url, StringComparison.CurrentCultureIgnoreCase));

            if (newProduct != null)
            {
                newProduct = FreshenProduct(newProduct);
            }
            _cache.Set(cacheKey, newProduct);
            return(newProduct);
        }
Esempio n. 2
0
        public void ClearCache(Product p)
        {
            var cacheIdKey = Consts.GetCachePrefix(Consts.CachePrefix.ProductId, p.Id);
            var cacheIdUrl = Consts.GetCachePrefix(Consts.CachePrefix.ProductUrl, p.NameUrl);

            _cache.Remove(cacheIdKey);
            _cache.Remove(cacheIdUrl);
        }
Esempio n. 3
0
        public async Task <Category> FindCategoryById(int id)
        {
            var cacheKey = Consts.GetCachePrefix(Consts.CachePrefix.CategoryId, id);

            if (_cache.TryGetValue(cacheKey, out var cat))
            {
                return(cat as Category);
            }

            var newCat = await _context.Category.FindAsync(id);

            _cache.Set(cacheKey, newCat);
            return(newCat);
        }
Esempio n. 4
0
        public async Task <Category> FindCategoryByName(string name)
        {
            var cacheKey = Consts.GetCachePrefix(Consts.CachePrefix.CategoryUrl, name);

            if (_cache.TryGetValue(cacheKey, out var c))
            {
                return(c as Category);
            }

            var newProduct = await _context.Category.FirstOrDefaultAsync(cat => cat.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase));

            _cache.Set(cacheKey, newProduct);
            return(newProduct);
        }
Esempio n. 5
0
        public async Task <Product> FindProductById(int id)
        {
            var cacheKey = Consts.GetCachePrefix(Consts.CachePrefix.ProductId, id);

            if (_cache.TryGetValue(cacheKey, out var product))
            {
                return(FreshenProduct(product as Product));
            }
            else
            {
                var newProduct = FreshenProduct(await _context.Products.FindAsync(id));
                _cache.Set(cacheKey, newProduct);
                return(newProduct);
            }
        }
Esempio n. 6
0
        public async Task <User> GetCurrentUserAsync()
        {
            var cachePrefix = Consts.GetCachePrefix(Consts.CachePrefix.CurrentUser, 0);

            if (_cache.TryGetValue(cachePrefix, out var user))
            {
                return(user as User);
            }
            var newUser = await _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User);

            if (newUser != null)
            {
                newUser.Roles = await _userManager.GetRolesAsync(newUser);

                _cache.Set(cachePrefix, newUser, new MemoryCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(10)
                });
            }
            return(newUser);
        }