Beispiel #1
0
        public override IQueryable <TEntity> Query()
        {
            var entityName        = typeof(TEntity).Name;
            var queryableCacheKey = $"{entityName}_QueryAll";
            var enumCacheValue    = (CacheGroup)Enum.Parse(typeof(CacheGroup), entityName);

            _cache.TryGetValue(enumCacheValue, queryableCacheKey, out IQueryable <TEntity> queryAll);

            if (queryAll == null)
            {
                queryAll = base.Query().ToList().AsQueryable();
                _cache.Set(enumCacheValue, queryableCacheKey, queryAll);
            }

            var attachedEntities = _dbContext.ChangeTracker.Entries <TEntity>().Select(e => e.Property("Id").CurrentValue);

            foreach (var item in queryAll)
            {
                var itemId = typeof(TEntity).GetProperty("Id").GetValue(item);
                if (!attachedEntities.Contains(itemId))
                {
                    _dbContext.Attach(item);
                }
            }
            return(queryAll);
        }
Beispiel #2
0
        /// <summary>
        /// Se encarga de verificar si un objeto o lista de objetos se encuentran cacheados, dada una key
        /// </summary>
        /// <typeparam name="T">Tipo alojado en la cache</typeparam>
        /// <param name="key">Key a buscar en la cache</param>
        /// <param name="action">Funcion a invocar si los valores no se encuentran en la cache</param>
        /// <returns></returns>
        private IEnumerable <T> TryToGetFromCache <T>(string key, Func <IEnumerable <T> > action)
        {
            var found = _cache.TryGetValue(CACHE_GROUP, key, out IEnumerable <T> values);

            if (!found)
            {
                values = action();
                _cache.Set(CACHE_GROUP, key, values, new ExpirationSettings {
                    AbsoluteExpiration = CACHE_LEVEL
                });
            }

            return(values);
        }