Пример #1
0
        // ReSharper disable once ParameterHidesMember
        protected System.Runtime.Caching.CacheItemPolicy ToRuntimePolicy(InMemoryPolicy policy)
        {
            TimeSpan       slidingExpiration;
            DateTimeOffset absoluteExpiration;

            if (policy.SlidingExpiration != null && policy.SlidingExpiration.Value < TimeSpan.MaxValue)
            {
                absoluteExpiration = System.Runtime.Caching.ObjectCache.InfiniteAbsoluteExpiration;
                slidingExpiration  = policy.SlidingExpiration.Value;
            }
            else if (policy.ExpirationFromAdd != null && policy.ExpirationFromAdd.Value < TimeSpan.MaxValue)
            {
                absoluteExpiration = DateTimeOffset.Now.Add(policy.ExpirationFromAdd.Value);
                slidingExpiration  = System.Runtime.Caching.ObjectCache.NoSlidingExpiration;
            }
            else if (policy.AbsoluteExpiration != null && policy.AbsoluteExpiration.Value < DateTimeOffset.MaxValue)
            {
                absoluteExpiration = policy.AbsoluteExpiration.Value;
                slidingExpiration  = System.Runtime.Caching.ObjectCache.NoSlidingExpiration;
            }
            else
            {
                absoluteExpiration = System.Runtime.Caching.ObjectCache.InfiniteAbsoluteExpiration;
                slidingExpiration  = System.Runtime.Caching.ObjectCache.NoSlidingExpiration;
            }

            return(new System.Runtime.Caching.CacheItemPolicy
            {
                AbsoluteExpiration = absoluteExpiration,
                SlidingExpiration = slidingExpiration,
            });
        }
Пример #2
0
        protected ObjectCache(
            String name, System.Runtime.Caching.ObjectCache innerCache, InMemoryPolicy policy)
        {
            this.name       = name;
            this.policy     = policy;
            this.innerCache = innerCache;

            this.synchronizer = CacheSynchronizer.CreateCacheSynchronizer(this, this.policy.SyncProvider);
        }
Пример #3
0
        protected ObjectCache(
            String name, System.Runtime.Caching.ObjectCache innerCache, InMemoryPolicy policy)
        {
            this.name       = name;
            this.policy     = policy;
            this.innerCache = innerCache;

            this.locks = !this.policy.DoNotLock
                ? new MultiLock(
                this.policy.NumberOfLocks ?? 50,
                this.policy.LockTimeoutMilliseconds != null && this.policy.LockTimeoutMilliseconds > 0
                        ? this.policy.LockTimeoutMilliseconds
                        : null,
                this.policy.DoThrowExceptionOnTimeout ?? true)
                : null;

            this.notiferName  = this.policy?.SyncProvider;
            this.synchronizer = CacheSynchronizer.CreateCacheSynchronizer(this, this.notiferName);
        }
Пример #4
0
        protected ObjectCache(
            String name, System.Runtime.Caching.ObjectCache innerCache, InMemoryPolicy policy)
        {
            this.name       = name;
            this.Policy     = policy;
            this.innerCache = innerCache;

            this.Locks = !this.Policy.DoNotLock
                ? new MultiLock(
                this.Policy.NumberOfLocks ?? 50,
                this.Policy.LockTimeoutMilliseconds != null && this.Policy.LockTimeoutMilliseconds > 0
                        ? this.Policy.LockTimeoutMilliseconds
                        : null,
                this.Policy.DoThrowExceptionOnTimeout ?? true)
                : null;

            if (this.Policy.OnSyncProviderFailure != null)
            {
                if (string.IsNullOrEmpty(this.Policy.SyncProvider))
                {
                    throw new ApplicationException($"{name}.OnSyncProviderFailure requires SyncProvider to be defined");
                }

                var cacheItemPolicy = ToRuntimePolicy(this.Policy);
                var syncProviderFailureCacheItemPolicy = ToRuntimePolicy(this.Policy.OnSyncProviderFailure);
                if (syncProviderFailureCacheItemPolicy.AbsoluteExpiration >= cacheItemPolicy.AbsoluteExpiration &&
                    syncProviderFailureCacheItemPolicy.SlidingExpiration >= cacheItemPolicy.SlidingExpiration)
                {
                    throw new ApplicationException($"{name}.OnSyncProviderFailure expiry policy needs to be more restrictive");
                }
            }

            this.notiferName  = this.Policy.SyncProvider;
            this.synchronizer = CacheSynchronizer.CreateCacheSynchronizer(this, this.notiferName,
                                                                          invalidateOnStateChange: this.Policy.OnSyncProviderFailure?.InvalidateOnProviderStateChange ?? false);
        }
Пример #5
0
 public InMemoryScopedCache(String name, InMemoryPolicy policy)
     : base(name, new System.Runtime.Caching.MemoryCache(name), policy)
 {
 }