Ejemplo n.º 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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }