Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="limit">Attempt to have no more than this many items.</param>
        /// <param name="retention">Do not prune if this many or less items.</param>
        /// <param name="interval">Automatic pruning interval.</param>
        /// <param name="store"></param>
        /// <param name="evict"></param>
        public SimpleCache(int limit           = 100, int retention = 10, TimeSpan?interval = null,
                           StoreStrategy store = StoreStrategy.ReplaceNoMatch, EvictStrategy evict = EvictStrategy.LeastRecent)
        {
            CacheStoreStrategy = store;
            CacheEvictStrategy = evict;

            AbsoluteCapacity = limit;
            MinCache         = retention;
            Overflow         = Math.Min(Math.Max(AbsoluteCapacity / 2, 2), 50);

            Items = new ConcurrentDictionary <TKey, CacheItem <TKey, TValue> >(Environment.ProcessorCount, MinCache);

            PruneTimer = new System.Timers.Timer(10_000);
            if (interval.HasValue)
            {
                PruneTimer.Interval = interval.Value.TotalMilliseconds;
                PruneTimer.Elapsed += Prune;
                PruneTimer.Start();
            }
        }
Example #2
0
        public Cache(double interval     = 10, int maxItems = 100, int minItems = 10,
                     StoreStrategy store = StoreStrategy.ReplaceNoMatch, EvictStrategy evict = EvictStrategy.LeastRecent)
        {
            CacheStoreStrategy = store;
            CacheEvictStrategy = evict;

            MaxCache = maxItems;
            MinCache = minItems;
            Overflow = (MaxCache / 2).Constrain(2, 50);

            pruneTimer.Interval = interval * 1000 * 60;
            pruneTimer.Elapsed += (s, e) =>
            {
                try
                {
                    Prune();
                }
                catch (Exception ex)
                {
                    Logging.Stacktrace(ex);
                }
            };
            pruneTimer.Start();
        }