Exemple #1
0
        private bool MaybeReadAllFromCache(JsonOperationContext ctx, AggressiveCacheOptions options)
        {
            DisposeCache();

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

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

                if (command.Headers.ContainsKey(Constants.Headers.IfNoneMatch))
                {
                    continue; // command already explicitly handling setting this, let's not touch it.
                }
                var cacheKey   = GetCacheKey(command, out _);
                var cachedItem = _httpCache.Get(ctx, cacheKey, out var changeVector, out var cached);
                if (cachedItem.Item == null)
                {
                    using (cachedItem)
                    {
                        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);
        }
Exemple #2
0
 internal AggressiveCacheConventions(DocumentConventions conventions)
 {
     _conventions            = conventions;
     _aggressiveCacheOptions = new AggressiveCacheOptions(TimeSpan.FromDays(1), AggressiveCacheMode.TrackChanges);
 }