示例#1
0
 /// <summary>
 /// The constructor for creating the region strategy.
 /// </summary>
 /// <param name="connectionMultiplexer">The Redis connection.</param>
 /// <param name="configuration">The region configuration.</param>
 /// <param name="properties">The NHibernate configuration properties.</param>
 protected AbstractRegionStrategy(IConnectionMultiplexer connectionMultiplexer,
                                  RedisCacheRegionConfiguration configuration, IDictionary <string, string> properties)
 {
     Log                   = NHibernateLogger.For(GetType());
     RegionName            = configuration.RegionName;
     Expiration            = configuration.Expiration;
     UseSlidingExpiration  = configuration.UseSlidingExpiration;
     AppendHashcode        = configuration.AppendHashcode;
     RegionKey             = configuration.RegionKey;
     ConnectionMultiplexer = connectionMultiplexer;
     Database              = configuration.DatabaseProvider.Get(connectionMultiplexer, configuration.Database);
     Serializer            = configuration.Serializer;
     LockTimeout           = configuration.LockConfiguration.KeyTimeout;
     _keyLocker            = new RedisKeyLocker(RegionName, Database, configuration.LockConfiguration);
 }
示例#2
0
 /// <summary>
 /// Default constructor.
 /// </summary>
 public FastRegionStrategy(IConnectionMultiplexer connectionMultiplexer,
                           RedisCacheRegionConfiguration configuration, IDictionary <string, string> properties)
     : base(connectionMultiplexer, configuration, properties)
 {
     if (!ExpirationEnabled)
     {
         return;
     }
     PutManyScript = ExpirationPutManyLuaScript;
     if (UseSlidingExpiration)
     {
         GetScript     = SlidingGetLuaScript;
         GetManyScript = SlidingGetManyLuaScript;
     }
 }
        /// <inheritdoc />
        public TwoLayerCacheRegionStrategy(
            IConnectionMultiplexer connectionMultiplexer,
            RedisCacheRegionConfiguration configuration,
            RegionMemoryCacheBase memoryCache,
            IDictionary <string, string> properties)
            : base(connectionMultiplexer, configuration, properties)
        {
            var usePipelining = GetBoolean("cache.region_strategy.two_layer_cache.use_pipelining", properties, false);

            Log.Debug("Use pipelining for region {0}: {1}", RegionName, usePipelining);

            var clientId = GetInteger("cache.region_strategy.two_layer_cache.client_id", properties, Guid.NewGuid().GetHashCode());

            Log.Debug("Client id for region {0}: {1}", RegionName, clientId);

            var maxSynchronizationTime = GetTimeSpanFromSeconds(
                "cache.region_strategy.two_layer_cache.max_synchronization_time", properties, TimeSpan.FromSeconds(10));

            Log.Debug("Max synchronization time for region {0}: {1} seconds", RegionName, maxSynchronizationTime.TotalSeconds);

            _twoLayerCache = new TwoLayerCache(new TwoLayerCacheConfiguration
            {
                ConnectionMultiplexer = connectionMultiplexer,
                RegionKey             = RegionKey,
                MemoryCache           = memoryCache,
                Expiration            = Expiration,
                Serializer            = Serializer,
                UseSlidingExpiration  = UseSlidingExpiration,
                Log       = Log,
                Database  = Database,
                PutScript = PutScript,
                AppendAdditionalValues = AppendAdditionalValues,
                ExpirationEnabled      = ExpirationEnabled,
                AppendAdditionalKeys   = AppendAdditionalKeys,
                RemoveScript           = RemoveScript,
                RedisGet               = base.ExecuteGet,
                RedisGetAsync          = base.ExecuteGetAsync,
                RedisGetMany           = base.ExecuteGetMany,
                RedisGetManyAsync      = base.ExecuteGetManyAsync,
                LogErrorMessage        = LogErrorMessage,
                UsePipelining          = usePipelining,
                ClientId               = clientId,
                MaxSynchronizationTime = maxSynchronizationTime
            });
            // Initialize the version here as OnVersionUpdate was already called in the
            // DefaultRegionStrategy constructor
            _twoLayerCache.OnVersionUpdate(-1, CurrentVersion);
        }
示例#4
0
        /// <inheritdoc />
        public virtual AbstractRegionStrategy Create(IConnectionMultiplexer connectionMultiplexer,
                                                     RedisCacheRegionConfiguration configuration, IDictionary <string, string> properties)
        {
            if (configuration.RegionStrategy == typeof(DefaultRegionStrategy))
            {
                return(new DefaultRegionStrategy(connectionMultiplexer, configuration, properties));
            }
            if (configuration.RegionStrategy == typeof(FastRegionStrategy))
            {
                return(new FastRegionStrategy(connectionMultiplexer, configuration, properties));
            }

            throw new CacheException(
                      $"{configuration.RegionStrategy} is not supported by {GetType()}, register " +
                      $"a custom {typeof(ICacheRegionStrategyFactory)} or use a supported one.");
        }
示例#5
0
        /// <inheritdoc />
        public DistributedLocalCacheRegionStrategy(
            IConnectionMultiplexer connectionMultiplexer,
            RedisCacheRegionConfiguration configuration,
            RegionMemoryCacheBase memoryCache,
            IDictionary <string, string> properties)
            : base(connectionMultiplexer, configuration, properties)
        {
            var lockConfiguration      = configuration.LockConfiguration;
            var acquireTimeout         = lockConfiguration.AcquireTimeout;
            var retryTimes             = lockConfiguration.RetryTimes;
            var maxRetryDelay          = lockConfiguration.MaxRetryDelay;
            var minRetryDelay          = lockConfiguration.MinRetryDelay;
            var lockRetryDelayProvider = lockConfiguration.RetryDelayProvider;

            _usePipelining = GetBoolean("cache.region_strategy.distributed_local_cache.use_pipelining", properties, false);
            Log.Debug("Use pipelining for region {0}: {1}", RegionName, _usePipelining);

            _clientId = GetInteger("cache.region_strategy.distributed_local_cache.client_id", properties, Guid.NewGuid().GetHashCode());
            Log.Debug("Client id for region {0}: {1}", RegionName, _clientId);

            _maxSynchronizationTime = GetTimeSpanFromSeconds(
                "cache.region_strategy.distributed_local_cache.max_synchronization_time", properties, TimeSpan.FromSeconds(10));
            Log.Debug("Max synchronization time for region {0}: {1} seconds", RegionName, _maxSynchronizationTime.TotalSeconds);

            _memoryCache            = memoryCache;
            _synchronizationChannel = string.Concat("{", configuration.RegionKey, "}@", "Synchronization");
            _lockValueProvider      = lockConfiguration.ValueProvider;
            _lockKeySuffix          = lockConfiguration.KeySuffix;
            _lockAcquireTimeout     = lockConfiguration.AcquireTimeout;
            _lockKeyTimeout         = lockConfiguration.KeyTimeout;
            _retryPolicy            = new RetryPolicy <string, Func <object> >(
                retryTimes,
                acquireTimeout,
                () => lockRetryDelayProvider.GetValue(minRetryDelay, maxRetryDelay)
                )
                                      .ShouldRetry(s => s == null)
                                      .OnFailure(OnFailedLock);
            _subscriber = ConnectionMultiplexer.GetSubscriber();

            ConnectionMultiplexer.ConnectionFailed   += OnConnectionFailed;
            ConnectionMultiplexer.ConnectionRestored += OnConnectionRestored;
            ConnectionMultiplexer.ErrorMessage       += OnErrorMessage;
            _subscriber.Subscribe(_synchronizationChannel).OnMessage((Action <ChannelMessage>)OnSynchronizationMessage);
            _subscriber.Subscribe(GetClientChannel(_clientId)).OnMessage((Action <ChannelMessage>)OnPrivateMessage);
        }
        /// <summary>
        /// Default constructor.
        /// </summary>
        public DefaultRegionStrategy(IConnectionMultiplexer connectionMultiplexer,
                                     RedisCacheRegionConfiguration configuration, IDictionary <string, string> properties)
            : base(connectionMultiplexer, configuration, properties)
        {
            var maxVersion = GetInteger("cache.region_strategy.default.max_allowed_version", properties, 1000);

            Log.Debug("Max allowed version for region {0}: {1}", RegionName, maxVersion);

            _usePubSub = GetBoolean("cache.region_strategy.default.use_pubsub", properties, true);
            Log.Debug("Use pubsub for region {0}: {1}", RegionName, _usePubSub);

            _regionKeyArray   = new RedisKey[] { RegionKey };
            _maxVersionNumber = new RedisValue[] { maxVersion };
            InitializeVersion();

            if (_usePubSub)
            {
                ConnectionMultiplexer.GetSubscriber().SubscribeAsync(RegionKey, (channel, value) =>
                {
                    UpdateVersion(value);
                });
            }
        }
        /// <summary>
        /// Default constructor.
        /// </summary>
        public DefaultRegionStrategy(IConnectionMultiplexer connectionMultiplexer,
                                     RedisCacheRegionConfiguration configuration, IDictionary <string, string> properties)
            : base(connectionMultiplexer, configuration, properties)
        {
            var maxVersion = GetLong("cache.region_strategy.default.max_allowed_version", properties, 10000);

            Log.Debug("Max allowed version for region {0}: {1}", RegionName, maxVersion);

            _usePubSub = GetBoolean("cache.region_strategy.default.use_pubsub", properties, true);
            Log.Debug("Use pubsub for region {0}: {1}", RegionName, _usePubSub);

            _retryTimes = GetInteger("cache.region_strategy.default.retry_times", properties, 1);
            Log.Debug("Retry times for region {0}: {1}", RegionName, _retryTimes);

            _regionKeyArray  = new RedisKey[] { RegionKey };
            _maxVersionArray = new RedisValue[] { maxVersion };
            _databaseIndex   = Database.Database;
            InitializeVersion();

            if (_usePubSub)
            {
                ConnectionMultiplexer.GetSubscriber().Subscribe(RegionKey).OnMessage((Action <ChannelMessage>)OnVersionMessage);
            }
        }