public UnboundedMemoizationCacheWithExceptionBase(Func <T, R> function, ICacheDictionary <T, IValueOrError <R> > cache) { _function = args => { #pragma warning disable IDE0079 // Remove unnecessary suppression #pragma warning disable CA1031 // Do not catch general exception types (by design) try { return(ValueOrError.CreateValue <R>(function(args))); } catch (Exception ex) { return(ValueOrError.CreateError <R>(ex)); } #pragma warning restore CA1031 #pragma warning restore IDE0079 }; _cache = cache; }
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); }; }