Esempio n. 1
0
        public void RunTest1()
        {
            // add task to cache
            RemoteTask remoteTask1 = new RemoteTask()
            {
                id = Guid.NewGuid(), name = "task1", completed = false
            };

            _remoteTasksCache.AddOrUpdate(remoteTask1);

            // update task
            remoteTask1.completed = true;
            _remoteTasksCache.AddOrUpdate(remoteTask1);

            // ExpireAfter seems only to work when all caches meet the remove function condition
            // if one return null (no expiry), no cache will be deleted (bug?)
            // this behavior can be reproduced by commenting out
            // remoteTask1.completed = true;
            var _remover = _remoteTasksCache.ExpireAfter(RemoveFunc, Scheduler.Default).Subscribe();

            // task2
            RemoteTask remoteTask2 = new RemoteTask()
            {
                id = Guid.NewGuid(), name = "task2", completed = true
            };

            _remoteTasksCache.AddOrUpdate(remoteTask2);
        }
Esempio n. 2
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();
        }