Beispiel #1
0
                public Cache(Func <T, R> function, Func <IMemoizationCacheEntryMetrics, TMetric> ranker, int maxCapacity, bool descending, double ageThreshold, bool cacheError, IStopwatchFactory stopwatchFactory)
                {
                    _function = args =>
                    {
                        var weakArgs = WeakReferenceExtensions.Create(args);

                        var invokeDuration = default(TimeSpan);
#if DEBUG
                        Interlocked.Increment(ref _invocationCount);
#endif
                        Trim();

                        var res = default(IMetricsCacheEntry <WeakReference <T>, R>);
                        try
                        {
                            var swInvokeStart = _stopwatch.ElapsedTicks;

                            var value = function(args);

                            invokeDuration = new TimeSpan(_stopwatch.ElapsedTicks - swInvokeStart);

                            res = new MetricsValueCacheEntry <WeakReference <T>, R>(weakArgs, value);
                        }
                        catch (Exception ex) when(_cacheError)
                        {
                            res = new MetricsErrorCacheEntry <WeakReference <T>, R>(weakArgs, ex);
                        }

                        res.CreationTime   = new TimeSpan(_stopwatch.ElapsedTicks);
                        res.InvokeDuration = invokeDuration;

                        _lock.EnterWriteLock();
                        try
                        {
                            _entries.Add(res);
                        }
                        finally
                        {
                            _lock.ExitWriteLock();
                        }

                        return(res);
                    };

                    _cache     = new WeakCacheDictionary <T, IMetricsCacheEntry <WeakReference <T>, R> >();
                    _lock      = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
                    _entries   = new HashSet <IMetricsCacheEntry <WeakReference <T>, R> >();
                    _stopwatch = stopwatchFactory.StartNew();

                    //
                    // Exclude newest items which have statically irrelevant data, so they get a chance to become relevant.
                    //
                    var candidates = _entries.OrderBy(e => _stopwatch.ElapsedTicks - e.CreationTime.Ticks).Take(Math.Max(1, (int)(maxCapacity * ageThreshold)));
                    _ranker = descending ? candidates.OrderByDescending(e => ranker(e)) : candidates.OrderBy(e => ranker(e));

                    _maxCapacity = maxCapacity;
                    _cacheError  = cacheError;
                }
Beispiel #2
0
                protected override void ClearCore(bool disposing)
                {
                    base.ClearCore(disposing);

                    //
                    // Unfortunately, CWT does not expose its Clear method publicly.
                    //
                    _cache = new WeakCacheDictionary <T, IValueOrError <R> >();
                }
                public Cache(Func <T, R> function, int maxCapacity, bool cacheError)
                {
                    _function = args =>
                    {
                        var weakArgs = WeakReferenceExtensions.Create(args);

#if DEBUG
                        var invokeDuration = default(TimeSpan);
                        Interlocked.Increment(ref _invocationCount);
#endif
                        Trim();

                        var res = default(ILruCacheEntry <WeakReference <T>, R>);
                        try
                        {
#if DEBUG
                            var swInvoke = Stopwatch.StartNew();
#endif
                            var value = function(args);
#if DEBUG
                            invokeDuration = swInvoke.Elapsed;
#endif
                            res = new LruValueCacheEntry <WeakReference <T>, R>(weakArgs, value);
                        }
                        catch (Exception ex) when(_cacheError)
                        {
                            res = new LruErrorCacheEntry <WeakReference <T>, R>(weakArgs, ex);
                        }

                        Interlocked.Increment(ref _count);
#if DEBUG
                        res.GetMetrics().InvokeDuration = invokeDuration;
#endif
                        return(res);
                    };

                    _cache       = new WeakCacheDictionary <T, ILruCacheEntry <WeakReference <T>, R> >();
                    _lock        = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
                    _maxCapacity = maxCapacity;
                    _cacheError  = cacheError;
                }
Beispiel #4
0
                public Cache(Func <T, R> function)
                {
                    _cache = new WeakCacheDictionary <T, object>();
#if DEBUG
                    _isNew = new ThreadLocal <bool>();
#endif
                    _function = args =>
                    {
#if DEBUG
                        var swInvoke = Stopwatch.StartNew();
                        _isNew.Value = true;
#endif
                        var value = function(args);
#if DEBUG
                        Interlocked.Add(ref _invocationTicks, swInvoke.ElapsedTicks);
#endif
                        Interlocked.Increment(ref _count);

                        return(value);
                    };
                }
Beispiel #5
0
                public CacheWithException(Func <T, R> function)
                {
                    _cache = new WeakCacheDictionary <T, IValueOrError <R> >();
#if DEBUG
                    _isNew = new ThreadLocal <bool>();
#endif
                    _function = args =>
                    {
#if DEBUG
                        _isNew.Value = true;
                        var swInvoke = Stopwatch.StartNew();
#endif
                        var value = default(IValueOrError <R>);

#pragma warning disable IDE0079 // Remove unnecessary suppression
#pragma warning disable CA1031  // Do not catch general exception types (by design)

                        try
                        {
                            value = ValueOrError.CreateValue(function(args));
                        }
                        catch (Exception ex)
                        {
                            value = ValueOrError.CreateError <R>(ex);
                        }

#pragma warning restore CA1031
#pragma warning restore IDE0079

#if DEBUG
                        Interlocked.Add(ref _invocationTicks, swInvoke.ElapsedTicks);
#endif
                        Interlocked.Increment(ref _count);

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