Beispiel #1
0
                protected override R GetOrAddCore(T args)
                {
#if DEBUG
                    Interlocked.Increment(ref _accessCount);

                    var swTotal = Stopwatch.StartNew();
#endif
                    //
                    // NB: CWT does not call the function under its internal lock.
                    //
                    var res = _cache.GetOrAdd(args, _function);
#if DEBUG
                    try
#endif
                    {
                        return(res.Value);
                    }
#if DEBUG
                    finally
                    {
                        if (!_isNew.Value)
                        {
                            Interlocked.Add(ref _lookupTicks, swTotal.ElapsedTicks);
                            Interlocked.Increment(ref _hitCount);
                        }

                        _isNew.Value = false;
                    }
#endif
                }
Beispiel #2
0
                protected override R GetOrAddCore(T args)
                {
#if DEBUG
                    Interlocked.Increment(ref _accessCount);

                    var swTotal = Stopwatch.StartNew();
#endif
                    var res = (R)_cache.GetOrAdd(args, _function);
#if DEBUG
                    if (!_isNew.Value)
                    {
                        Interlocked.Add(ref _lookupTicks, swTotal.ElapsedTicks);
                        Interlocked.Increment(ref _hitCount);
                    }

                    _isNew.Value = false;
#endif
                    return(res);
                }
Beispiel #3
0
                protected override R GetOrAddCore(T args)
                {
                    var entry = default(IMetricsCacheEntry <WeakReference <T>, R>);

                    _lock.EnterUpgradeableReadLock();
                    try
                    {
#if DEBUG
                        Interlocked.Increment(ref _accessCount);
#endif
                        var swTotalStart = _stopwatch.ElapsedTicks;

                        //
                        // NB: CWT does not call the function under its internal lock.
                        //
                        entry = _cache.GetOrAdd(args, _function);
#if DEBUG
                        var keys = _cache.Keys;
                        Debug.Assert(_entries.Count == keys.Count);
#endif
                        //
                        // NB: Calculating stats outside the lock to keep lock duration as short as possible.
                        //
                        var duration   = new TimeSpan(_stopwatch.ElapsedTicks - swTotalStart);
                        var accessTime = _stopwatch.Elapsed;

                        lock (entry) // TODO: review; if access per entry is high, we may be better off without the lock and only guarantee approximate values
                        {
                            entry.HitCount++;
                            entry.TotalDuration += duration;
                            entry.LastAccessTime = accessTime;
                        }
                    }
                    finally
                    {
                        _lock.ExitUpgradeableReadLock();
                    }

                    return(entry.Value);
                }
                protected override R GetOrAddCore(T args)
                {
                    var entry = default(ILruCacheEntry <WeakReference <T>, R>);

                    //
                    // NB: CWT already has a fat lock inside of it. We're putting the extra lock here to perform Trim activities, which
                    //     need to maintain the LRU cache.
                    //
                    _lock.EnterUpgradeableReadLock();
                    try
                    {
#if DEBUG
                        var swTotal = Stopwatch.StartNew();
                        Interlocked.Increment(ref _accessCount);
#endif
                        //
                        // NB: CWT does not call the function under its internal lock.
                        //
                        entry = _cache.GetOrAdd(args, _function);

                        MostRecent(entry);
#if DEBUG
                        var metrics = entry.GetMetrics();
                        lock (metrics)
                        {
                            metrics.HitCount++;
                            metrics.TotalDuration += swTotal.Elapsed;
                        }
#endif
                    }
                    finally
                    {
                        _lock.ExitUpgradeableReadLock();
                    }

                    return(entry.Value);
                }
 private static string GetOrAdd(IWeakCacheDictionary <Obj, string> cache)
 {
     return(cache.GetOrAdd(new Obj(), o => o.ToString()));
 }