コード例 #1
0
        public T GetOrAdd(string key, Func <T> loadFunction)
        {
            LazyLock lazy;
            bool     success;

            synclock.EnterReadLock();
            try
            {
                success = this.TryGetValue(key, out lazy);
            }
            finally
            {
                synclock.ExitReadLock();
            }

            if (!success)
            {
                synclock.EnterWriteLock();
                try
                {
                    if (!this.TryGetValue(key, out lazy))
                    {
                        lazy = new LazyLock();
                        this.Add(key, lazy);
                    }
                }
                finally
                {
                    synclock.ExitWriteLock();
                }
            }

            return(lazy.Get(loadFunction));
        }
コード例 #2
0
 private bool TryGetValue(string key, out LazyLock value)
 {
     value = this.Get(key);
     if (value != null)
     {
         return(true);
     }
     return(false);
 }
コード例 #3
0
        private void Add(string key, LazyLock item)
        {
            var policy = new CacheItemPolicy();

            policy.Priority = CacheItemPriority.NotRemovable;
            if (IsTimespanSet(cachePolicy.AbsoluteExpiration))
            {
                policy.AbsoluteExpiration = DateTimeOffset.Now.Add(cachePolicy.AbsoluteExpiration);
            }
            else if (IsTimespanSet(cachePolicy.SlidingExpiration))
            {
                policy.SlidingExpiration = cachePolicy.SlidingExpiration;
            }

            cache.Add(key, item, policy);
        }