public DynamicCache <string, CurrencyPair> GetCache(IDynamicCacheConfiguration configuration)
        {
            var cache = new DynamicCache <string, CurrencyPair>(configuration, LoggerForTests <DynamicCache <string, CurrencyPair> > .Default(), _eventSerializer);

            _actors.Add(cache);

            return(cache);
        }
 public DynamicCache(IDynamicCacheConfiguration configuration, IEventSerializer eventSerializer)
 {
     _eventSerializer  = eventSerializer;
     _configuration    = configuration;
     _sourceCache      = new SourceCache <TAggregate, TKey>(selector => selector.Id);
     _cancel           = new CancellationTokenSource();
     _state            = new BehaviorSubject <DynamicCacheState>(DynamicCacheState.NotConnected);
     _caughtingUpCache = new CaughtingUpCache <TKey, TAggregate>();
 }
        public DynamicCache(IDynamicCacheConfiguration configuration, ILogger <DynamicCache <TKey, TAggregate> > logger, IEventSerializer eventSerializer) : base(logger)
        {
            _blockEventConsumption = new ManualResetEventSlim(true);

            _cacheErrors = new ObservableCollection <ActorMonitoringError>();

            _logger          = logger;
            _cleanup         = new CompositeDisposable();
            _eventSerializer = eventSerializer;
            _configuration   = configuration;
            _sourceCache     = new SourceCache <TAggregate, TKey>(selector => selector.Id);
            _cancel          = new CancellationTokenSource();

            _state         = new BehaviorSubject <DynamicCacheState>(DynamicCacheState.NotConnected);
            _isStaled      = new BehaviorSubject <bool>(true);
            _isCaughtingUp = new BehaviorSubject <bool>(false);

            _caughtingUpCache = new CaughtingUpCache <TKey, TAggregate>();

            _getStateOfTheWorldRetyPolicy = Policy.Handle <Exception>()
                                            .RetryForever((ex) =>
            {
                _cacheErrors.Add(new ActorMonitoringError()
                {
                    CacheErrorStatus = ActorErrorType.DynamicCacheGetStateOfTheWorldFailure,
                    Exception        = ex
                });
            });


            //on reconnected start try to caught up
            _state.Subscribe(state =>
            {
                _logger.LogInformation($"Cache state is {state}");

                if (state == DynamicCacheState.Reconnected)
                {
                    _isCaughtingUp.OnNext(true);
                }
            }).Cleanup(_cleanup);


            //start caughting up by fetching the broker state of the world
            _isCaughtingUp
            .Where(state => state)
            .Subscribe(_ =>

                       _caughtUpWithStateOfTheWorldProc = Task.Run(() =>
            {
                WaitUntilConnected();

                WaitUntilCaughtUpToStateOfTheWorld();
            }, _cancel.Token))

            .Cleanup(_cleanup);


            Observable.FromEventPattern <NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>
                (e => Errors.CollectionChanged += e, e => Errors.CollectionChanged -= e)
            .Where(arg => arg.EventArgs.NewItems.Count > 0)
            .Subscribe(arg =>
            {
                foreach (var error in arg.EventArgs.NewItems.Cast <ActorMonitoringError>())
                {
                    _logger.LogError(error.Message, error.Exception);
                }
            })
            .Cleanup(_cleanup);
        }