internal CacheEntry(object key, MemoryCache memoryCache)
 {
     Key       = key ?? throw new ArgumentNullException(nameof(key));
     _cache    = memoryCache ?? throw new ArgumentNullException(nameof(memoryCache));
     _previous = memoryCache.TrackLinkedCacheEntries ? CacheEntryHelper.EnterScope(this) : null;
     _state    = new CacheEntryState(CacheItemPriority.Normal);
 }
示例#2
0
        public async Task Get_when_cache_state_is_invalid_does_not_call_deserialize(CacheEntryState invalidEntityState)
        {
            var cancellationToken = CancellationToken.None;
            var testCacheModel    = new TestCacheModel();

            var cachedByteResults = new byte[32];

            Array.Fill <byte>(cachedByteResults, 255);

            distributedCacheMock
            .Setup(distributedCache => distributedCache.GetAsync(nameof(TestCacheModel), cancellationToken))
            .Returns(Task.FromResult(cachedByteResults)).Verifiable();

            messagePackServiceMock.Setup(messagePackService => messagePackService
                                         .Deserialise <TestCacheModel>(cachedByteResults, sut._messagePackOptions))
            .Returns(Task.FromResult(testCacheModel))
            .Verifiable();

            cacheEntryTrackerMock.Setup(cacheEntryTrackerMock => cacheEntryTrackerMock
                                        .GetState(It.IsAny <string>(), cancellationToken))
            .Returns(Task.FromResult(invalidEntityState));

            var result = await sut.Get <TestCacheModel>(nameof(TestCacheModel), cancellationToken);

            distributedCacheMock.Verify(distributedCache => distributedCache
                                        .GetAsync(nameof(TestCacheModel), cancellationToken), Times.Once);
            messagePackServiceMock.Verify(messagePackService => messagePackService
                                          .Deserialise <TestCacheModel>(cachedByteResults, sut._messagePackOptions), Times.Never);
        }
        public async Task SetState(string cacheItem, CacheEntryState cacheEntryState, CancellationToken cancellationToken)
        {
            var items = await GetOrSetItems(false, cancellationToken);

            if (items.ContainsKey(cacheItem))
            {
                items[cacheItem] = cacheEntryState;
            }
            else
            {
                items.Add(cacheItem, cacheEntryState);
            }

            await _cacheTrackerStore.SaveItems(items, cancellationToken);

            _log.LogInformation("Writing state information for '{0}'", cacheItem);
            _log.LogDebug("Setting {0} state for {1}", Enum.GetName(typeof(CacheEntryState), cacheEntryState), cacheItem);
        }
示例#4
0
 public CacheEntryLockItem(CacheEntryState initialState)
 {
     State = initialState;
 }