Esempio n. 1
0
        public TodoCache(ILogger logger, TimeSpan?expireAfter = null, int?limitSizeTo = null)
        {
            this.logger      = logger;
            this.SourceCache = new SourceCache <TodoDto, int>(t => t.Id);

            // Expiration time
            if (expireAfter.HasValue)
            {
                SourceCache.ExpireAfter(u => expireAfter.Value, TimeSpan.FromSeconds(expireAfter.Value.TotalSeconds / 10))
                .Subscribe(x => logger.LogInformation("SourceCache Expiration: {0} filled trades have been removed from memory", x.Count()));
            }

            // Limit size
            if (limitSizeTo.HasValue)
            {
                SourceCache.LimitSizeTo(limitSizeTo.Value)
                .Subscribe(x => logger.LogInformation("SourceCache LimitSize: {0} filled trades have been removed from memory", x.Count()));
            }

            // Shared object
            SharedCache = SourceCache.Connect()
                          .WhereReasonsAre(ChangeReason.Add, ChangeReason.Moved, ChangeReason.Refresh, ChangeReason.Update, ChangeReason.Remove)
                          .Transform(w => new DataTracker <TodoDto>(w, null))
                          .ForEachChange(e =>
            {
                e.Current.Reason  = e.Reason;
                e.Current.Updated = DateTime.Now;
            })
                          .AsObservableCache();
        }