示例#1
0
        /// <summary>
        /// Removes an item from a context within the overall memory cache of these items. The context must be obtained from the
        /// memory cache instance prior to use. This operation is thread safe - all operations are concurrency locked within the
        /// confines of the context.
        /// </summary>
        public void Remove(ITRexSpatialMemoryCacheContext context, ITRexMemoryCacheItem element)
        {
            lock (_contexts)
            {
                context.Remove(element);

                // Perform some house keeping to keep the cache size in bounds
                ItemRemovedFromContext(element.IndicativeSizeInBytes());
            }
        }
示例#2
0
        /// <summary>
        /// Adds an item into a context within the overall memory cache of these items. The context must be obtained from the
        /// memory cache instance prior to use. This operation is thread safe - all operations are concurrency locked within the
        /// confines of the context.
        /// </summary>
        public CacheContextAdditionResult Add(ITRexSpatialMemoryCacheContext context, ITRexMemoryCacheItem element, long invalidationVersion)
        {
            lock (_contexts)
            {
                // If the invalidation version of a sub grid derivative being added to the cache does not match the current invalidation version
                // then prevent it being added to the cache, and return a negative result to indicate the refusal
                if (invalidationVersion != context.InvalidationVersion)
                {
                    _log.LogInformation($"Sub grid derivative at {element.CacheOriginX}:{element.CacheOriginY} not added to cache context due to invalidation version mismatch ({invalidationVersion} vs {context.InvalidationVersion})");
                    return(CacheContextAdditionResult.RejectedDueToInvlidationVersionMismatch);
                }

                if (context.MarkedForRemoval)
                {
                    context.Reanimate();
                }

                var result = context.Add(element);

                if (result == CacheContextAdditionResult.Added)
                {
                    // Perform some house keeping to keep the cache size in bounds
                    ItemAddedToContext(element.IndicativeSizeInBytes());
                    while (_currentSizeInBytes > MaxSizeInBytes && !MRUList.IsEmpty())
                    {
                        MRUList.EvictOneLRUItem();
                    }
                }

                return(result);
            }
        }