Exemple #1
0
        public RedisScopedCache(String name, RedisCachePolicy policy)
        {
            this.Name = name;
            var log = NLog.LogManager.GetLogger(typeof(RedisScopedCache).FullName);

            this.Policy = policy;

            log.Debug("Init Cache {0}", this.Name);

            if (policy == null)
            {
                log.Error("Invalid Policy for Cache {0}", this.Name);
                throw new ArgumentNullException(nameof(policy));
            }

            if (string.IsNullOrEmpty(policy.ConnectionName) && string.IsNullOrEmpty(policy.ConnectionString))
            {
                throw new ArgumentException(
                          $"{nameof(policy.ConnectionName)} is undefined", $"{nameof(policy)}.{nameof(policy.ConnectionName)}");
            }

            if (policy.SlidingExpiration.HasValue && policy.SlidingExpiration.Value < TimeSpan.MaxValue)
            {
                this.expireWithin         = policy.SlidingExpiration.Value;
                this.useSlidingExpiration = true;
                this.expireAt             = null;
            }
            else if (policy.ExpirationFromAdd.HasValue && policy.ExpirationFromAdd.Value < TimeSpan.MaxValue)
            {
                this.expireWithin         = policy.ExpirationFromAdd.Value;
                this.useSlidingExpiration = false;
                this.expireAt             = null;
            }
            else if (policy.AbsoluteExpiration.HasValue && policy.AbsoluteExpiration.Value < DateTimeOffset.MaxValue)
            {
                this.expireWithin         = null;
                this.useSlidingExpiration = false;
                this.expireAt             = policy.AbsoluteExpiration.Value.LocalDateTime;
            }
            else
            {
                this.expireWithin         = null;
                this.useSlidingExpiration = false;
                this.expireAt             = null;
            }

            this.useSlidingExpiration = (policy.SlidingExpiration < TimeSpan.MaxValue);

            if (string.IsNullOrEmpty(policy.ConnectionName))
            {
                this.InnerCache = new ScopedCacheContext(
                    policy.ConnectionString, policy.Converter, policy.ClusterType, policy.MonitorPort, policy.MonitorIntervalMilliseconds);
            }
            else
            {
                this.InnerCache = new ScopedCacheContext(policy.ConnectionName, policy.Converter);
            }

            synchronizer = CacheSynchronizer.CreateCacheSynchronizer(this, policy.SyncProvider);
        }
Exemple #2
0
        public MongoDbCache(String cacheCollectionName, MongoDbCachePolicy policy)
        {
            this.policy = policy ?? throw new ArgumentNullException(nameof(policy));

            if (!string.IsNullOrEmpty(policy.ConnectionName))
            {
                this.connectionString = CacheManager.GetConnectionString(policy.ConnectionName)?.ConnectionString;

                if (string.IsNullOrEmpty(connectionString))
                {
                    throw new ArgumentException(
                              $"{nameof(ICacheConnectionString.ConnectionString)} not found for {nameof(policy.ConnectionName)} {policy.ConnectionName}", $"{nameof(policy)}.{nameof(policy.ConnectionName)}");
                }
            }
            else if (!string.IsNullOrEmpty(policy.ConnectionString))
            {
                this.connectionString = policy.ConnectionString;
            }
            else
            {
                throw new ArgumentException(
                          $"{nameof(policy.ConnectionString)} is undefined", $"{nameof(policy)}.{nameof(policy.ConnectionString)}");
            }

            this.cacheDbName = !string.IsNullOrEmpty(policy.DatabaseName)
                ? policy.DatabaseName : "CacheDb";

            this.cacheCollectionName = cacheCollectionName;

            if (policy.SlidingExpiration.HasValue && policy.SlidingExpiration.Value < TimeSpan.MaxValue)
            {
                this.expireWithin         = policy.SlidingExpiration.Value;
                this.useSlidingExpiration = true;
                this.expireAt             = null;
            }
            else if (policy.ExpirationFromAdd.HasValue && policy.ExpirationFromAdd.Value < TimeSpan.MaxValue)
            {
                this.expireWithin         = policy.ExpirationFromAdd.Value;
                this.useSlidingExpiration = false;
                this.expireAt             = null;
            }
            else if (policy.AbsoluteExpiration.HasValue && policy.AbsoluteExpiration.Value < DateTimeOffset.MaxValue)
            {
                this.expireWithin         = null;
                this.useSlidingExpiration = false;
                this.expireAt             = policy.AbsoluteExpiration.Value.LocalDateTime;
            }
            else
            {
                this.expireWithin         = null;
                this.useSlidingExpiration = false;
                this.expireAt             = null;
            }

            this.useSlidingExpiration = (policy.SlidingExpiration < TimeSpan.MaxValue);

            this.synchronizer = CacheSynchronizer.CreateCacheSynchronizer(this, policy.SyncProvider);
        }
Exemple #3
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);
        }
Exemple #4
0
        public RedisCache(String name, RedisCachePolicy policy)
        {
            this.name = name;
            this.log  = NLog.LogManager.GetLogger(nameof(RedisCache));

            log.Debug("Init Cache {0}", this.name);

            if (policy == null)
            {
                log.Error("Invalid Policy for Cache {0}", this.name);
                throw new ArgumentNullException(nameof(policy));
            }

            this.connectionString            = policy.ConnectionString;
            this.monitorPort                 = policy.MonitorPort;
            this.monitorIntervalMilliseconds = policy.MonitorIntervalMilliseconds;
            this.converterType               = policy.Converter;
            this.clusterType                 = policy.ClusterType;

            if (policy.SlidingExpiration.HasValue && policy.SlidingExpiration.Value < TimeSpan.MaxValue)
            {
                this.expireWithin         = policy.SlidingExpiration.Value;
                this.useSlidingExpiration = true;
                this.expireAt             = null;
            }
            else if (policy.ExpirationFromAdd.HasValue && policy.ExpirationFromAdd.Value < TimeSpan.MaxValue)
            {
                this.expireWithin         = policy.ExpirationFromAdd.Value;
                this.useSlidingExpiration = false;
                this.expireAt             = null;
            }
            else if (policy.AbsoluteExpiration.HasValue && policy.AbsoluteExpiration.Value < DateTimeOffset.MaxValue)
            {
                this.expireWithin         = null;
                this.useSlidingExpiration = false;
                this.expireAt             = policy.AbsoluteExpiration.Value.LocalDateTime;
            }
            else
            {
                this.expireWithin         = null;
                this.useSlidingExpiration = false;
                this.expireAt             = null;
            }

            this.useSlidingExpiration = (policy.SlidingExpiration < TimeSpan.MaxValue);

            this.innerCache   = new CacheContext(this.connectionString, this.converterType, this.clusterType, this.monitorPort, this.monitorIntervalMilliseconds);
            this.synchronizer = CacheSynchronizer.CreateCacheSynchronizer(this, policy.SyncProvider);
        }
Exemple #5
0
        public MongoDbCache(String cacheCollectionName, MongoDbCachePolicy policy)
        {
            if (policy == null)
            {
                throw new ArgumentNullException("policy");
            }

            this.connectionString = policy.ConnectionString;

            this.cacheDbName = !string.IsNullOrEmpty(policy.DatabaseName)
                ? policy.DatabaseName : "CacheDb";

            this.cacheCollectionName = cacheCollectionName;

            if (policy.SlidingExpiration.HasValue && policy.SlidingExpiration.Value < TimeSpan.MaxValue)
            {
                this.expireWithin         = policy.SlidingExpiration.Value;
                this.useSlidingExpiration = true;
                this.expireAt             = null;
            }
            else if (policy.ExpirationFromAdd.HasValue && policy.ExpirationFromAdd.Value < TimeSpan.MaxValue)
            {
                this.expireWithin         = policy.ExpirationFromAdd.Value;
                this.useSlidingExpiration = false;
                this.expireAt             = null;
            }
            else if (policy.AbsoluteExpiration.HasValue && policy.AbsoluteExpiration.Value < DateTimeOffset.MaxValue)
            {
                this.expireWithin         = null;
                this.useSlidingExpiration = false;
                this.expireAt             = policy.AbsoluteExpiration.Value.LocalDateTime;
            }
            else
            {
                this.expireWithin         = null;
                this.useSlidingExpiration = false;
                this.expireAt             = null;
            }

            this.useSlidingExpiration = (policy.SlidingExpiration < TimeSpan.MaxValue);

            this.synchronizer = CacheSynchronizer.CreateCacheSynchronizer(this, policy.SyncProvider);
        }
Exemple #6
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);
        }
Exemple #7
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);
        }
Exemple #8
0
        public RedisCache(String name, RedisCachePolicy policy)
        {
            this.name = name;
            this.log  = NLog.LogManager.GetLogger(typeof(RedisCache).FullName);

            log.Debug("Init Cache {0}", this.name);

            if (policy == null)
            {
                log.Error("Invalid Policy for Cache {0}", this.name);
                throw new ArgumentNullException(nameof(policy));
            }

            if (!string.IsNullOrEmpty(policy.ConnectionName))
            {
                this.connectionString = CacheManager.GetConnectionString(policy.ConnectionName)?.ConnectionString;

                if (string.IsNullOrEmpty(connectionString))
                {
                    throw new ArgumentException(
                              $"{nameof(ICacheConnectionString.ConnectionString)} not found for {nameof(policy.ConnectionName)} {policy.ConnectionName}", $"{nameof(policy)}.{nameof(policy.ConnectionName)}");
                }
            }
            else if (!string.IsNullOrEmpty(policy.ConnectionString))
            {
                this.connectionString = policy.ConnectionString;
            }
            else
            {
                throw new ArgumentException(
                          $"{nameof(policy.ConnectionString)} is undefined", $"{nameof(policy)}.{nameof(policy.ConnectionString)}");
            }

            this.monitorPort = policy.MonitorPort;
            this.monitorIntervalMilliseconds = policy.MonitorIntervalMilliseconds;
            this.converterType = policy.Converter;
            this.clusterType   = policy.ClusterType;

            if (policy.SlidingExpiration.HasValue && policy.SlidingExpiration.Value < TimeSpan.MaxValue)
            {
                this.expireWithin         = policy.SlidingExpiration.Value;
                this.useSlidingExpiration = true;
                this.expireAt             = null;
            }
            else if (policy.ExpirationFromAdd.HasValue && policy.ExpirationFromAdd.Value < TimeSpan.MaxValue)
            {
                this.expireWithin         = policy.ExpirationFromAdd.Value;
                this.useSlidingExpiration = false;
                this.expireAt             = null;
            }
            else if (policy.AbsoluteExpiration.HasValue && policy.AbsoluteExpiration.Value < DateTimeOffset.MaxValue)
            {
                this.expireWithin         = null;
                this.useSlidingExpiration = false;
                this.expireAt             = policy.AbsoluteExpiration.Value.LocalDateTime;
            }
            else
            {
                this.expireWithin         = null;
                this.useSlidingExpiration = false;
                this.expireAt             = null;
            }

            this.useSlidingExpiration = (policy.SlidingExpiration < TimeSpan.MaxValue);

            this.innerCache = new CacheContext(
                this.connectionString, this.converterType, this.clusterType, this.monitorPort, this.monitorIntervalMilliseconds);

            this.notiferName = policy.SyncProvider;

            this.synchronizer = CacheSynchronizer.CreateCacheSynchronizer(this, this.notiferName);
        }