/// <summary> /// Creates a new <see cref="MemoryCache"/> instance. /// </summary> /// <param name="optionsAccessor">The options of the cache.</param> public MemoryCache(IOptions <MemoryCacheOptions> optionsAccessor) { if (optionsAccessor == null) { throw new ArgumentNullException(nameof(optionsAccessor)); } _options = optionsAccessor.Value; _entries = new ConcurrentDictionary <object, CacheEntry>(); _setEntry = SetEntry; _entryExpirationNotification = EntryExpired; if (_options.Clock == null) { _options.Clock = new SystemClock(); } _lastExpirationScan = _options.Clock.UtcNow; }
/// <summary> /// Creates a new <see cref="MemoryCache"/> instance. /// </summary> /// <param name="optionsAccessor">The options of the cache.</param> /// <param name="loggerFactory">The factory used to create loggers.</param> public MemoryCache(IOptions <MemoryCacheOptions> optionsAccessor, ILoggerFactory loggerFactory) { ThrowHelper.ThrowIfNull(optionsAccessor); ThrowHelper.ThrowIfNull(loggerFactory); _options = optionsAccessor.Value; _logger = loggerFactory.CreateLogger <MemoryCache>(); _coherentState = new CoherentState(); if (_options.TrackStatistics) { _allStats = new List <WeakReference <Stats> >(); _accumulatedStats = new Stats(); _stats = new ThreadLocal <Stats>(() => new Stats(this)); } _lastExpirationScan = UtcNow; TrackLinkedCacheEntries = _options.TrackLinkedCacheEntries; // we store the setting now so it's consistent for entire MemoryCache lifetime }
/// <summary> /// Creates a new <see cref="MemoryCache"/> instance. /// </summary> /// <param name="optionsAccessor">The options of the cache.</param> /// <param name="loggerFactory">The factory used to create loggers.</param> public MemoryCache(IOptions <MemoryCacheOptions> optionsAccessor, ILoggerFactory loggerFactory) { if (optionsAccessor == null) { throw new ArgumentNullException(nameof(optionsAccessor)); } if (loggerFactory == null) { throw new ArgumentNullException(nameof(loggerFactory)); } _options = optionsAccessor.Value; _logger = loggerFactory.CreateLogger <MemoryCache>(); _entries = new ConcurrentDictionary <object, CacheEntry>(); if (_options.Clock == null) { _options.Clock = new SystemClock(); } _lastExpirationScan = _options.Clock.UtcNow; }
public void NonPositiveCacheSizeLimitThrows() { var options = new MemoryCacheOptions(); Assert.Throws <ArgumentOutOfRangeException>(() => options.SizeLimit = -1); }