예제 #1
0
        private bool MaybeReadAllFromCache(JsonOperationContext ctx, AggressiveCacheOptions options)
        {
            if (_cached != null)
            {
                _cached.Dispose();
                _cached = null;
            }

            bool readAllFromCache = options != null;
            var  trackChanges     = readAllFromCache && options.Mode == AggressiveCacheMode.TrackChanges;

            for (int i = 0; i < _commands.Count; i++)
            {
                var command = _commands[i];

                var cacheKey   = GetCacheKey(command, out _);
                var cachedItem = _httpCache.Get(ctx, cacheKey, out var changeVector, out var cached);
                if (cached == null)
                {
                    readAllFromCache = false;
                    continue;
                }

                if (readAllFromCache && (trackChanges && cachedItem.MightHaveBeenModified || cachedItem.Age > options.Duration || command.CanCacheAggressively == false))
                {
                    readAllFromCache = false;
                }

                command.Headers[Constants.Headers.IfNoneMatch] = $"\"{changeVector}\"";
                _cached ??= new Cached(_commands.Count);
                _cached.Values[i] = (cachedItem, cached);
            }

            if (readAllFromCache)
            {
                using (_cached)
                {
                    Result = new List <GetResponse>(_commands.Count);
                    for (int i = 0; i < _commands.Count; i++)
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        var(_, cached) = _cached.Values[i];
                        Result.Add(new GetResponse {
                            Result = cached.Clone(ctx), StatusCode = HttpStatusCode.NotModified
                        });
                    }
                }

                _cached = null;
            }
            return(readAllFromCache);
        }
예제 #2
0
 public void Dispose() => _cached?.Dispose();
예제 #3
0
 private void DisposeCache()
 {
     _cached?.Dispose();
     _cached = null;
 }