Esempio n. 1
0
        public static IEntityCache <T> PopulateCache <T>(this IEntityCache <T> entityCache, IRepository <T> repository)
            where T : ITrackedEntity
        {
            entityCache.Clear();

            foreach (var entity in repository.All())
            {
                entityCache.Add(entity);
            }

            return(entityCache);
        }
        public async Task <IEnumerable <StellarisMod> > GetAllAsync(CancellationToken cancellationToken = default)
        {
            await _lock.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                CachingOptions cachingOptions = _cachingOptions.Get(CacheOptionName);
                if (cachingOptions.Enabled && _lastCacheTimeUtc + cachingOptions.Lifetime < DateTime.UtcNow)
                {
                    _log.LogTrace("Stellaris mods found in cache");
                    return(_stellarisModsCache.Find(_ => true).Select(e => e.Entity));
                }

                _log.LogDebug("Retrieving Stellaris mods from database");
                IEnumerable <StellarisMod> results = await _collection.Find(_ => true).ToListAsync(cancellationToken).ConfigureAwait(false);

                if (results?.Any() != true)
                {
                    _log.LogTrace("Stellaris mods not found, returning empty");
                    results = Enumerable.Empty <StellarisMod>();
                }

                if (cachingOptions.Enabled)
                {
                    _stellarisModsCache.Clear();
                    foreach (StellarisMod mod in results)
                    {
                        _stellarisModsCache.AddOrReplace(mod.ID, mod, cachingOptions.Lifetime);
                    }
                }
                return(results);
            }
            finally
            {
                _lock.Release();
            }
        }
        public async Task DeleteMany(Expression <Func <TEntity, bool> > predicate)
        {
            await AlterRepository.DeleteMany(predicate);

            Cache.Clear();
        }
Esempio n. 4
0
 private void ClearCache()
 {
     _cache.Clear();
     _isCacheValid = false;
 }