Ejemplo n.º 1
0
 protected override void CollectCore()
 {
     if ((_gen1?.Count).GetValueOrDefault() > 0)
     {
         EvictionCount += _gen1.Count;
         Evicted?.Invoke(this, _gen1);
     }
     _gen1 = _gen0;                           // Gen1 items are dropped from the cache at this point
     _gen0 = new Dictionary <TKey, TValue>(); // Gen0 is now empty, we choose not to re-use Gen1 dictionary so the memory can be GC'd
 }
Ejemplo n.º 2
0
        /// <summary>Create a new cache that has a Gen0 size limit and/or a periodic collection time</summary>
        /// <param name="gen0Limit">(Optional) limit on the number of items allowed in Gen0 before a collection</param>
        /// <param name="timeToLive">(Optional) time period after which a unread item is evicted from the cache</param>
        /// <param name="partitions">The number of partitions to split the cache into, defaults to <see cref="Environment.ProcessorCount"/></param>
        public ConcurrentCache(int?gen0Limit, TimeSpan?timeToLive, int partitions = 0)
        {
            if (partitions == 0)
            {
                partitions = Environment.ProcessorCount;
            }
            else if (partitions < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(partitions), partitions, "Must be one or more");
            }

            _partitionCount = partitions;
            _partitions     = new Cache <TKey, TValue> [partitions];
            for (int i = 0; i < partitions; i++)
            {
                _partitions[i]          = new Cache <TKey, TValue>(gen0Limit / partitions, timeToLive);
                _partitions[i].Evicted += (sender, args) => Evicted?.Invoke(sender, args);
            }
        }
 public void EntryEvicted(EntryEvent<TKey, TValue> @event)
 {
     Evicted?.Invoke(@event);
 }
Ejemplo n.º 4
0
 private void OnEvicted(object sender, EventArgs args)
 {
     Evicted?.Invoke(sender, args);
 }
 /// <summary>
 /// Raises the Evicted event with the specified arguments.
 /// </summary>
 /// <param name="expression">Expression found in the cache.</param>
 /// <param name="delegate">Compiled delegate of the expression.</param>
 protected virtual void OnEvicted(LambdaExpression expression, Delegate @delegate) => Evicted?.Invoke(this, new CacheEventArgs(expression, @delegate));