コード例 #1
0
 public DoubleBufferCache(int capacity = 2000, double evictionTimeSec = 20)
 {
     _capacity = capacity;
     // we set swap interval to half evict time, as stale items stay in cache for time which
     // is double of swap interval - first item sits in recent items, then in older items
     _swapInterval = TimeSpan.FromSeconds(evictionTimeSec / 2);
     _recentItems  = new Dictionary <TKey, TValue>(capacity + 10);
     _olderItems   = new Dictionary <TKey, TValue>(capacity + 10);
     // create initial metrics and make a buffer swap to finish full initialization
     _metrics = new CacheMetrics()
     {
         StartedOn = AppTime.UtcNow
     };
     SwapBuffers();
 }
コード例 #2
0
        private void SwapBuffers()
        {
            var utcNow = AppTime.UtcNow;

            //swap metrics
            _metrics.ItemCount = Math.Max(_recentItems.Count, _olderItems.Count);
            _metrics.EndedOn   = utcNow;
            _oldMetrics        = _metrics;
            _metrics           = new CacheMetrics()
            {
                StartedOn = utcNow
            };
            //swap buffers
            var temp = _olderItems;

            _olderItems  = _recentItems;
            _recentItems = temp;
            _recentItems.Clear();
            // update times
            _lastSwappedOn = utcNow;
            _nextSwapOn    = utcNow.Add(_swapInterval);
        }