Exemple #1
0
        public TwoLayerCache(TwoLayerCacheConfiguration configuration)
        {
            _log                    = configuration.Log;
            _memoryCache            = configuration.MemoryCache;
            _clientId               = configuration.ClientId;
            _serializer             = configuration.Serializer;
            _database               = configuration.Database;
            _expirationEnabled      = configuration.ExpirationEnabled;
            _useSlidingExpiration   = configuration.UseSlidingExpiration;
            _expiration             = configuration.Expiration;
            _appendAdditionalKeys   = configuration.AppendAdditionalKeys;
            _appendAdditionalValues = configuration.AppendAdditionalValues;
            _redisGet               = configuration.RedisGet;
            _redisGetAsync          = configuration.RedisGetAsync;
            _redisGetMany           = configuration.RedisGetMany;
            _redisGetManyAsync      = configuration.RedisGetManyAsync;
            _putScript              = configuration.PutScript;
            _removeScript           = configuration.RemoveScript;
            _logErrorMessage        = configuration.LogErrorMessage;
            _regionKey              = configuration.RegionKey;
            _usePipelining          = configuration.UsePipelining;
            _maxSynchronizationTime = configuration.MaxSynchronizationTime;

            var connectionMultiplexer = configuration.ConnectionMultiplexer;
            var invalidationChannel   = string.Concat("{", configuration.RegionKey, "}@", "Invalidation");

            _invalidationChannel = invalidationChannel;
            connectionMultiplexer.GetSubscriber().Subscribe(invalidationChannel).OnMessage((Action <ChannelMessage>)OnInvalidationMessage);
            connectionMultiplexer.ConnectionFailed   += OnConnectionFailed;
            connectionMultiplexer.ErrorMessage       += OnErrorMessage;
            connectionMultiplexer.ConnectionRestored += OnConnectionRestored;
        }
        /// <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);
        }
Exemple #3
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);
        }