Пример #1
0
        /// <summary>
        /// If an entry already exists in the cache, it will be overwritten. It will throw an exception for a null data reference.
        /// </summary>
        public void Store(CachedPostIndexContent data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            try
            {
                using var memoryStream = new MemoryStream();
                using (var compressedStream = new GZipStream(memoryStream, CompressionMode.Compress))
                {
                    new BinaryFormatter().Serialize(compressedStream, data);
                }
                var serialisedData = memoryStream.ToArray();
                using var fileStream = File.Open(_dataFile.FullName, FileMode.Create);
                fileStream.Write(serialisedData, 0, serialisedData.Length);
            }
            catch
            {
                // Ignore any errors - if access is denied then there's nothing we can do, just push on
            }
        }
Пример #2
0
        private CachedPostIndexContent GetData(NonNullImmutableList <Post> posts)
        {
            if (posts == null)
            {
                throw new ArgumentNullException(nameof(posts));
            }

            var cachedData = _cache.TryToRetrieve();

            if ((cachedData != null) && cachedData.IsValidForPostsData(posts))
            {
                return(cachedData);
            }

            var postIndexData = _postIndexer.GenerateIndexContent(posts);
            var liveData      = new CachedPostIndexContent(
                postIndexData,
                posts
                );

            _cache.Store(liveData);
            return(liveData);
        }
 /// <summary>
 /// If an entry already exists in the cache, it will be overwritten. It will throw an exception for a null data reference.
 /// </summary>
 public void Store(CachedPostIndexContent data)
 {
     // Ensure that any existing entry is removed before adding a new one (for cases where existing content contains expired data)
     _cache.Remove(CacheKey);
     _cache[CacheKey] = data ?? throw new ArgumentNullException(nameof(data));
 }