Exemple #1
0
        /// <summary>
        /// Initialize the cache.
        /// </summary>
        /// <param name="capacity">Maximum number of entries.</param>
        /// <param name="evictCount">Number to evict when capacity is reached.</param>
        /// <param name="persistence">Persistence driver.</param>
        /// <param name="prepopulate">If using persistence, prepopulate from existing items.</param>
        public LRUCache(int capacity, int evictCount, PersistenceDriver persistence = null, bool prepopulate = true)
        {
            if (capacity < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(capacity));
            }
            if (evictCount < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(evictCount));
            }
            if (evictCount > capacity)
            {
                throw new ArgumentOutOfRangeException(nameof(evictCount));
            }

            if (persistence != null)
            {
                if (typeof(T1) != typeof(string))
                {
                    throw new InvalidOperationException("Persistence can only be used when the cache key is of type 'string'.");
                }
            }

            _Capacity    = capacity;
            _EvictCount  = evictCount;
            _Cache       = new Dictionary <T1, DataNode <T2> >();
            _Persistence = persistence;

            if (persistence != null && prepopulate)
            {
                Prepopulate();
            }
        }
Exemple #2
0
        /// <summary>
        /// Dispose of the object.  Do not use after disposal.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                lock (_CacheLock)
                {
                    _Cache = null;
                }

                _Capacity   = 0;
                _EvictCount = 0;

                _Events?.Disposed?.Invoke(this, EventArgs.Empty);
                _Events      = null;
                _Persistence = null;
            }
        }