示例#1
0
        private void RefreshIfNecessary()
        {
            CachedInstance ci = current;

            if (ci == null || updateTimer.ElapsedMilliseconds - ci.createdAt >= maxAgeMs)
            {
                lock (myLock)
                {
                    ci = current;
                    if (ci == null || updateTimer.ElapsedMilliseconds - ci.createdAt >= maxAgeMs)
                    {
                        current = new CachedInstance(createNewObjectFunc(), updateTimer);
                    }
                }
            }
            else if (updateTimer.ElapsedMilliseconds - ci.createdAt >= minAgeMs)
            {
                SetTimeout.OnBackground(() =>
                {
                    ci = current;
                    if (updateTimer.ElapsedMilliseconds - ci.createdAt >= minAgeMs)
                    {
                        lock (myLock)
                        {
                            ci = current;
                            if (updateTimer.ElapsedMilliseconds - ci.createdAt >= minAgeMs)
                            {
                                current = new CachedInstance(createNewObjectFunc(), updateTimer);
                            }
                        }
                    }
                }, 0);
            }
        }
示例#2
0
 /// <summary>
 /// Reloads the cached object now without regard for the current age. Returns a reference to the object created by this method.
 /// </summary>
 public T Reload()
 {
     Interlocked.Increment(ref updateCounter);
     try
     {
         CachedInstance ci = current = new CachedInstance(createNewObjectFunc(), updateTimer);
         return(ci.instance);
     }
     finally
     {
         Interlocked.Decrement(ref updateCounter);
     }
 }
示例#3
0
        public object ActivateInstance(IObjectAssemblySpecification registration)
        {
            string key = registration.Key;

            CachedInstance ci = null;

            if (_cache.TryGetValue(key, out ci))
            {
                lock (_syncLock) ci.LastCacheHit = DateTime.UtcNow;
            }
            else
            {
                ci = new CachedInstance {
                    Instance = registration.CreateInstance(), LastCacheHit = DateTime.UtcNow
                };
                _cache.TryAdd(key, ci);
            }

            MaybeGroomCache();
            return(ci.Instance);
        }
示例#4
0
        public void FlushCache(IObjectAssemblySpecification registration)
        {
            CachedInstance _ = null;

            _cache.TryRemove(registration.Key, out _);
        }
示例#5
0
        private bool NeedsUpdate(long ageLimitMs)
        {
            CachedInstance ci = current;

            return(ci == null || updateTimer.ElapsedMilliseconds - ci.createdAt >= ageLimitMs);
        }
示例#6
0
 public static MockAuthentication Get() => CachedInstance.Copy();