コード例 #1
0
        public RedisCache(string regionName, IDictionary<string, string> properties, RedisCacheElement element, ConnectionMultiplexer connectionMultiplexer, RedisCacheProviderOptions options)
        {
            this.connectionMultiplexer = connectionMultiplexer.ThrowIfNull("connectionMultiplexer");
            this.options = options.ThrowIfNull("options").ShallowCloneAndValidate();

            RegionName = regionName.ThrowIfNull("regionName");

            if (element == null)
            {
                expiry = TimeSpan.FromSeconds(
                    PropertiesHelper.GetInt32(Cfg.Environment.CacheDefaultExpiration, properties, DefaultExpiry)
                );
            }
            else
            {
                expiry = element.Expiration;
            }

            log.DebugFormat("using expiration : {0} seconds", expiry.TotalSeconds);

            var @namespace = CacheNamePrefix + RegionName;

            CacheNamespace = new RedisNamespace(@namespace);
            SyncInitialGeneration();
        }
コード例 #2
0
        internal RedisCacheProviderOptions ShallowCloneAndValidate()
        {
            var clone = new RedisCacheProviderOptions(this);

            var name = typeof(RedisCacheProviderOptions).Name;

            if (clone.Serializer == null)
            {
                throw new InvalidOperationException("A serializer was not configured on the " + name + ".");
            }

            if (clone.AcquireLockRetryStrategy == null)
            {
                throw new InvalidOperationException("An acquire lock retry strategy was not configured on the " + name + ".");
            }

            if (clone.LockValueFactory == null)
            {
                throw new InvalidOperationException("A lock value factory was not confugred on the " + name + ".");
            }

            if (clone.CacheConfigurations == null)
            {
                throw new InvalidOperationException("The cache configurations cannot be null on the " + name + ".");
            }

            return(clone);
        }
 // Copy constructor.
 private RedisCacheProviderOptions(RedisCacheProviderOptions options)
 {
     Serializer = options.Serializer;
     OnException = options.OnException;
     LockValueFactory = options.LockValueFactory;
     Database = options.Database;
 }
コード例 #4
0
        internal RedisCacheProviderOptions ShallowCloneAndValidate()
        {
            var clone = new RedisCacheProviderOptions(this);

            var name = typeof(RedisCacheProviderOptions).Name;

            if (clone.Serializer == null)
            {
                throw new InvalidOperationException("A serializer was not configured on the " + name + ".");
            }

            if (clone.AcquireLockRetryStrategy == null)
            {
                throw new InvalidOperationException("An acquire lock retry strategy was not configured on the " + name + ".");
            }

            if (clone.LockValueFactory == null)
            {
                throw new InvalidOperationException("A lock value factory was not confugred on the " + name + ".");
            }

            if (clone.CacheConfigurations == null)
            {
                throw new InvalidOperationException("The cache configurations cannot be null on the " + name + ".");
            }

            return clone;
        }
コード例 #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="connectionMultiplexer"></param>
        /// <param name="options"></param>
        public RedisCache(RedisCacheConfiguration configuration, ConnectionMultiplexer connectionMultiplexer, RedisCacheProviderOptions options)
        {
            configuration.ThrowIfNull("configuration")
            .Validate();
            RegionName          = configuration.RegionName;
            _expiration         = configuration.Expiration;
            _slidingExpiration  = configuration.SlidingExpiration;
            _lockTimeout        = configuration.LockTimeout;
            _acquireLockTimeout = configuration.AcquireLockTimeout;

            this._connectionMultiplexer = connectionMultiplexer.ThrowIfNull("connectionMultiplexer");
            this._options = options.ThrowIfNull("options")
                            .ShallowCloneAndValidate();


            Log.Debug("creating cache: regionName='{0}', expiration='{1}', lockTimeout='{2}', acquireLockTimeout='{3}'",
                      RegionName, _expiration, _lockTimeout, _acquireLockTimeout
                      );

            options.KeyPrefix = options.KeyPrefix.LastIndexOf(":", StringComparison.Ordinal) > -1
              ? options.KeyPrefix
              : options.KeyPrefix + ":";

            CacheNamespace = new RedisNamespace(options.KeyPrefix + RegionName);
        }
コード例 #6
0
        public RedisCache(string regionName, IDictionary <string, string> properties, RedisCacheElement element, ConnectionMultiplexer connectionMultiplexer, RedisCacheProviderOptions options)
        {
            this.connectionMultiplexer = connectionMultiplexer.ThrowIfNull("connectionMultiplexer");
            this.options = options.ThrowIfNull("options").ShallowCloneAndValidate();

            RegionName = regionName.ThrowIfNull("regionName");

            if (element == null)
            {
                expiry = TimeSpan.FromSeconds(
                    PropertiesHelper.GetInt32(Cfg.Environment.CacheDefaultExpiration, properties, DefaultExpiry)
                    );
            }
            else
            {
                expiry = element.Expiration;
            }

            log.DebugFormat("using expiration : {0} seconds", expiry.TotalSeconds);

            var @namespace = CacheNamePrefix + RegionName;

            CacheNamespace = new RedisNamespace(@namespace);
            SyncInitialGeneration();
        }
コード例 #7
0
 // Copy constructor.
 private RedisCacheProviderOptions(RedisCacheProviderOptions options)
 {
     Serializer       = options.Serializer;
     OnException      = options.OnException;
     LockValueFactory = options.LockValueFactory;
     Database         = options.Database;
 }
コード例 #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="properties"></param>
        /// <param name="connectionMultiplexer"></param>
        /// <param name="options"></param>
        public RequestRecoveryRedisCache(RedisCacheConfiguration configuration,
                                         IDictionary <string, string> properties,
                                         ConnectionMultiplexer connectionMultiplexer,
                                         RedisCacheProviderOptions options)
            : base(configuration, connectionMultiplexer, options)
        {
            AsyncLocal <HttpContext> _httpContextCurrent = new AsyncLocal <HttpContext>();

            _httpContextCurrent.Value.Items[RequestRecoveryRedisCache.SkipNHibernateCacheKey] = true;
        }
コード例 #9
0
 // Copy constructor.
 private RedisCacheProviderOptions(RedisCacheProviderOptions options)
 {
     Serializer = options.Serializer;
     Exception  = options.Exception;
     AcquireLockRetryStrategy = options.AcquireLockRetryStrategy;
     LockFailed          = options.LockFailed;
     UnlockFailed        = options.UnlockFailed;
     LockValueFactory    = options.LockValueFactory;
     Database            = options.Database;
     CacheConfigurations = options.CacheConfigurations;
 }
 // Copy constructor.
 private RedisCacheProviderOptions(RedisCacheProviderOptions options)
 {
     Serializer = options.Serializer;
     Exception = options.Exception;
     AcquireLockRetryStrategy = options.AcquireLockRetryStrategy;
     LockFailed = options.LockFailed;
     UnlockFailed = options.UnlockFailed;
     LockValueFactory = options.LockValueFactory;
     Database = options.Database;
     CacheConfigurations = options.CacheConfigurations;
 }
コード例 #11
0
        /// <summary>
        /// Set the options to be used to configure each cache.
        /// </summary>
        /// <param name="options"></param>
        public static void SetOptions(RedisCacheProviderOptions options)
        {
            lock (syncRoot)
            {
                if (optionsStatic != null)
                {
                    throw new InvalidOperationException("The options can only be configured once.");
                }

                optionsStatic = options.ThrowIfNull();
            }
        }
コード例 #12
0
        /// <summary>
        /// Set the options to be used to configure each cache.
        /// </summary>
        /// <param name="options"></param>
        public static void SetOptions(RedisCacheProviderOptions options)
        {
            lock (syncRoot)
            {
                if (optionsStatic != null)
                {
                    throw new InvalidOperationException("The options can only be configured once.");
                }

                optionsStatic = options.ThrowIfNull();
            }
        }
コード例 #13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="regionName"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
#pragma warning disable 618
        public ICache BuildCache(string regionName, IDictionary <string, string> properties)
#pragma warning restore 618
        {
            if (_connectionMultiplexerStatic == null)
            {
                throw new InvalidOperationException(
                          "A 'ConnectionMultiplexer' must be configured with SetConnectionMultiplexer(). " +
                          "For example, call 'RedisCacheProvider.SetConnectionMultiplexer(ConnectionMultiplexer.Connect(\"localhost:6379\"))' " +
                          "before creating the ISessionFactory."
                          );
            }

            // Double-check so that we don't have to lock if necessary.
            if (optionsStatic == null)
            {
                lock (syncRoot)
                {
                    if (optionsStatic == null)
                    {
                        optionsStatic = new RedisCacheProviderOptions();
                    }
                }
            }

            if (Log.IsDebugEnabled())
            {
                var sb = new StringBuilder();
                foreach (var pair in properties)
                {
                    sb.Append("name=");
                    sb.Append(pair.Key);
                    sb.Append("&value=");
                    sb.Append(pair.Value);
                    sb.Append(";");
                }
                Log.Debug("building cache with region: {0}, properties: {1}", regionName, sb);
            }

            RedisCacheConfiguration configuration = null;

            if (!String.IsNullOrWhiteSpace(regionName) && optionsStatic.CacheConfigurations != null)
            {
                configuration = optionsStatic.CacheConfigurations.FirstOrDefault(x => x.RegionName == regionName);
            }

            if (configuration == null)
            {
                Log.Debug("loading cache configuration for '{0}' from properties/defaults", regionName);
                configuration = RedisCacheConfiguration.FromPropertiesOrDefaults(regionName, properties);
            }

            return(BuildCache(configuration, properties, _connectionMultiplexerStatic, optionsStatic));
        }
コード例 #14
0
        public ICache BuildCache(string regionName, IDictionary<string, string> properties)
        {
            if (connectionMultiplexerStatic == null)
            {
                throw new InvalidOperationException(
                    "A 'ConnectionMultiplexer' must be configured with SetConnectionMultiplexer(). " + 
                    "For example, call 'RedisCacheProvider.SetConnectionMultiplexer(ConnectionMultiplexer.Connect(\"localhost:6379\"))' " +
                    "before creating the ISessionFactory."
                );
            }

            // Double-check so that we don't have to lock if necessary.
            if (optionsStatic == null)
            {
                lock (syncRoot)
                {
                    if (optionsStatic == null)
                    {
                        optionsStatic = new RedisCacheProviderOptions();
                    }
                }
            }

            if (log.IsDebugEnabled)
            {
                var sb = new StringBuilder();
                foreach (var pair in properties)
                {
                    sb.Append("name=");
                    sb.Append(pair.Key);
                    sb.Append("&value=");
                    sb.Append(pair.Value);
                    sb.Append(";");
                }
                log.DebugFormat("building cache with region: {0}, properties: {1}", regionName, sb);
            }

            RedisCacheConfiguration configuration = null;

            if (!String.IsNullOrWhiteSpace(regionName) && optionsStatic.CacheConfigurations != null)
            {
                configuration = optionsStatic.CacheConfigurations.FirstOrDefault(x => x.RegionName == regionName);
            }

            if (configuration == null)
            {
                log.DebugFormat("loading cache configuration for '{0}' from properties/defaults", regionName);
                configuration = RedisCacheConfiguration.FromPropertiesOrDefaults(regionName, properties);
            }

            return BuildCache(configuration, properties, connectionMultiplexerStatic, optionsStatic);
        }
コード例 #15
0
        public ICache BuildCache(string regionName, IDictionary <string, string> properties)
        {
            if (connectionMultiplexerStatic == null)
            {
                throw new InvalidOperationException(
                          "A 'ConnectionMultiplexer' must be configured with SetConnectionMultiplexer(). " +
                          "For example, call 'RedisCacheProvider.SetConnectionMultiplexer(ConnectionMultiplexer.Connect(\"localhost:6379\"))' " +
                          "before creating the ISessionFactory."
                          );
            }

            // Double-check so that we don't have to lock if necessary.
            if (optionsStatic == null)
            {
                lock (syncRoot)
                {
                    if (optionsStatic == null)
                    {
                        optionsStatic = new RedisCacheProviderOptions();
                    }
                }
            }

            if (log.IsDebugEnabled)
            {
                var sb = new StringBuilder();
                foreach (var pair in properties)
                {
                    sb.Append("name=");
                    sb.Append(pair.Key);
                    sb.Append("&value=");
                    sb.Append(pair.Value);
                    sb.Append(";");
                }
                log.Debug("building cache with region: " + regionName + ", properties: " + sb);
            }

            RedisCacheElement configElement = null;

            if (!String.IsNullOrWhiteSpace(regionName))
            {
                configElement = providerSection.Caches[regionName];
            }

            return(BuildCache(regionName, properties, configElement, connectionMultiplexerStatic, optionsStatic));
        }
コード例 #16
0
        public RedisCache(RedisCacheConfiguration configuration, ConnectionMultiplexer connectionMultiplexer, RedisCacheProviderOptions options)
        {
            configuration.ThrowIfNull("configuration")
            .Validate();
            RegionName         = configuration.RegionName;
            expiration         = configuration.Expiration;
            slidingExpiration  = configuration.SlidingExpiration;
            lockTimeout        = configuration.LockTimeout;
            acquireLockTimeout = configuration.AcquireLockTimeout;

            this.connectionMultiplexer = connectionMultiplexer.ThrowIfNull("connectionMultiplexer");
            this.options = options.ThrowIfNull("options")
                           .ShallowCloneAndValidate();

            log.DebugFormat("creating cache: regionName='{0}', expiration='{1}', lockTimeout='{2}', acquireLockTimeout='{3}'",
                            RegionName, expiration, lockTimeout, acquireLockTimeout
                            );

            CacheNamespace = new RedisNamespace(options.KeyPrefix + RegionName);
        }
コード例 #17
0
        /// <summary>
        /// 到redis 不可用是,处理redis 请求
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="properties"></param>
        /// <param name="connectionMultiplexer"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        protected override RedisCache BuildCache(RedisCacheConfiguration configuration,
                                                 IDictionary <string, string> properties,
                                                 ConnectionMultiplexer connectionMultiplexer,
                                                 RedisCacheProviderOptions options)
        {
            //options.OnException = (e) =>
            //{
            //  if (HttpContext.Current != null)
            //  {
            //    HttpContext.Current.Items[RequestRecoveryRedisCache.SkipNHibernateCacheKey] = true;
            //  }
            //  else
            //  {
            //    CallContext.SetData(RequestRecoveryRedisCache.SkipNHibernateCacheKey, true);
            //  }

            //};
            //var evtArg = new ExceptionEventArgs(configuration.RegionName, RedisCacheMethod.Clear, e);
            //options.OnException(this, evtArg);
            options.OnException(null, new ExceptionEventArgs(configuration.RegionName, RedisCacheMethod.Unknown, new Exception()));

            return(new RequestRecoveryRedisCache(configuration, properties, connectionMultiplexer, options));
        }
コード例 #18
0
        internal RedisCacheProviderOptions ShallowCloneAndValidate()
        {
            var clone = new RedisCacheProviderOptions(this);

            var name = typeof(RedisCacheProviderOptions).Name;

            if (clone.Serializer == null)
            {
                throw new InvalidOperationException("A serializer was not configured on the " + name + ".");
            }

            if (clone.OnException == null)
            {
                throw new InvalidOperationException("A handler for on exception was not confugred on the " + name + ".");
            }

            if (clone.LockValueFactory == null)
            {
                throw new InvalidOperationException("A lock value factory was not confugred on the " + name + ".");
            }

            return(clone);
        }
        internal RedisCacheProviderOptions ShallowCloneAndValidate()
        {
            var clone = new RedisCacheProviderOptions(this);

            var name = typeof(RedisCacheProviderOptions).Name;

            if (clone.Serializer == null)
            {
                throw new InvalidOperationException("A serializer was not configured on the " + name + ".");
            }

            if (clone.OnException == null)
            {
                throw new InvalidOperationException("A handler for on exception was not confugred on the " + name + ".");                
            }

            if (clone.LockValueFactory == null)
            {
                throw new InvalidOperationException("A lock value factory was not confugred on the " + name + ".");
            }

            return clone;
        }
コード例 #20
0
 internal static void InternalSetOptions(RedisCacheProviderOptions options)
 {
     optionsStatic = options;
 }
コード例 #21
0
 protected virtual RedisCache BuildCache(RedisCacheConfiguration configuration, IDictionary <string, string> properties, ConnectionMultiplexer connectionMultiplexer, RedisCacheProviderOptions options)
 {
     return(new RedisCache(configuration, connectionMultiplexer, options));
 }
コード例 #22
0
        public RedisCache(string regionName, ConnectionMultiplexer connectionMultiplexer, RedisCacheProviderOptions options)
            : this(new RedisCacheConfiguration(regionName), connectionMultiplexer, options)
        {

        }
コード例 #23
0
        public RedisCache(RedisCacheConfiguration configuration, ConnectionMultiplexer connectionMultiplexer, RedisCacheProviderOptions options)
        {
            configuration.ThrowIfNull("configuration")
                .Validate();
            RegionName = configuration.RegionName;
            expiration = configuration.Expiration;
            slidingExpiration = configuration.SlidingExpiration;
            lockTimeout = configuration.LockTimeout;
            acquireLockTimeout = configuration.AcquireLockTimeout;

            this.connectionMultiplexer = connectionMultiplexer.ThrowIfNull("connectionMultiplexer");
            this.options = options.ThrowIfNull("options")
                .ShallowCloneAndValidate();

            log.DebugFormat("creating cache: regionName='{0}', expiration='{1}', lockTimeout='{2}', acquireLockTimeout='{3}'", 
                RegionName, expiration, lockTimeout, acquireLockTimeout
            );

            CacheNamespace = new RedisNamespace(cacheNamespacePrefix + RegionName);
        }
コード例 #24
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="regionName"></param>
        /// <param name="connectionMultiplexer"></param>
        /// <param name="options"></param>

        public RedisCache(string regionName, ConnectionMultiplexer connectionMultiplexer, RedisCacheProviderOptions options)
            : this(new RedisCacheConfiguration(regionName), connectionMultiplexer, options)
        {
        }
コード例 #25
0
 protected virtual RedisCache BuildCache(string regionName, IDictionary <string, string> properties, RedisCacheElement configElement, ConnectionMultiplexer connectionMultiplexer, RedisCacheProviderOptions options)
 {
     return(new RedisCache(regionName, properties, configElement, connectionMultiplexer, options));
 }
コード例 #26
0
        public ICache BuildCache(string regionName, IDictionary<string, string> properties)
        {
            if (connectionMultiplexerStatic == null)
            {
                throw new InvalidOperationException(
                    "A 'ConnectionMultiplexer' must be configured with SetConnectionMultiplexer(). " + 
                    "For example, call 'RedisCacheProvider.SetConnectionMultiplexer(ConnectionMultiplexer.Connect(\"localhost:6379\"))' " +
                    "before creating the ISessionFactory."
                );
            }

            // Double-check so that we don't have to lock if necessary.
            if (optionsStatic == null)
            {
                lock (syncRoot)
                {
                    if (optionsStatic == null)
                    {
                        optionsStatic = new RedisCacheProviderOptions();
                    }
                }
            }

            if (log.IsDebugEnabled)
            {
                var sb = new StringBuilder();
                foreach (var pair in properties)
                {
                    sb.Append("name=");
                    sb.Append(pair.Key);
                    sb.Append("&value=");
                    sb.Append(pair.Value);
                    sb.Append(";");
                }
                log.Debug("building cache with region: " + regionName + ", properties: " + sb);
            }

            RedisCacheElement configElement = null;
            if (!String.IsNullOrWhiteSpace(regionName))
            {
                configElement = providerSection.Caches[regionName];
            }

            return BuildCache(regionName, properties, configElement, connectionMultiplexerStatic, optionsStatic);
        }
コード例 #27
0
 internal static void InternalSetOptions(RedisCacheProviderOptions options)
 {
     optionsStatic = options;
 }
コード例 #28
0
 public RedisCache(string regionName, ConnectionMultiplexer connectionMultiplexer, RedisCacheProviderOptions options)
     : this(regionName, new Dictionary <string, string>(), null, connectionMultiplexer, options)
 {
 }
コード例 #29
0
        public RedisCache(string regionName, ConnectionMultiplexer connectionMultiplexer, RedisCacheProviderOptions options)
            : this(regionName, new Dictionary<string, string>(), null, connectionMultiplexer, options)
        {

        }
コード例 #30
0
 protected virtual RedisCache BuildCache(RedisCacheConfiguration configuration, IDictionary<string, string> properties, ConnectionMultiplexer connectionMultiplexer, RedisCacheProviderOptions options)
 {
     return new RedisCache(configuration, connectionMultiplexer, options);
 }
コード例 #31
0
 protected virtual RedisCache BuildCache(string regionName, IDictionary<string, string> properties, RedisCacheElement configElement, ConnectionMultiplexer connectionMultiplexer, RedisCacheProviderOptions options)
 {
     return new RedisCache(regionName, properties, configElement, connectionMultiplexer, options);
 }