public When_saving_two_aggregates_in_parallel()
        {
            _testStore = new TestInMemoryEventStore();
            _rep1 = new CacheRepository(new Repository(_testStore), _testStore, new MemoryCache());

            _aggregate1 = new TestAggregate(Guid.NewGuid());
            _aggregate2 = new TestAggregate(Guid.NewGuid());

            _rep1.Save(_aggregate1);
            _rep1.Save(_aggregate2);

            var t1 = new Task(() =>
            {
                for (var i = 0; i < 100; i++)
                {
                    var aggregate = _rep1.Get<TestAggregate>(_aggregate1.Id);
                    aggregate.DoSomething();
                    _rep1.Save(aggregate);
                }
            });

            var t2 = new Task(() =>
            {
                for (var i = 0; i < 100; i++)
                {
                    var aggregate = _rep1.Get<TestAggregate>(_aggregate2.Id);
                    aggregate.DoSomething();
                    _rep1.Save(aggregate);
                }
            });
            t1.Start();
            t2.Start();

            Task.WaitAll(t1, t2);
        }
        public void Setup()
        {
            // This will clear the cache between runs.
            var cacheKeys = MemoryCache.Default.Select(kvp => kvp.Key).ToList();
            foreach (var cacheKey in cacheKeys)
                MemoryCache.Default.Remove(cacheKey);

            _testStore = new TestInMemoryEventStore();
            _rep1 = new CacheRepository(new Repository(_testStore,new TestEventPublisher()), _testStore);
            _rep2 = new CacheRepository(new Repository(_testStore,new TestEventPublisher()), _testStore);

            _aggregate = new TestAggregate(Guid.NewGuid());
            _rep1.Save(_aggregate);

            var t1 = new Task(() =>
                                  {
                                      for (var i = 0; i < 100; i++)
                                      {
                                          var aggregate = _rep1.Get<TestAggregate>(_aggregate.Id);
                                          aggregate.DoSomething();
                                          _rep1.Save(aggregate);
                                      }
                                  });

            var t2 = new Task(() =>
                                  {
                                      for (var i = 0; i < 100; i++)
                                      {
                                          var aggregate = _rep2.Get<TestAggregate>(_aggregate.Id);
                                          aggregate.DoSomething();
                                          _rep2.Save(aggregate);
                                      }
                                  });
            var t3 = new Task(() =>
                                  {
                                      for (var i = 0; i < 100; i++)
                                      {
                                          var aggregate = _rep2.Get<TestAggregate>(_aggregate.Id);
                                          aggregate.DoSomething();
                                          _rep2.Save(aggregate);
                                      }
                                  });
            t1.Start();
            t2.Start();
            t3.Start();

            Task.WaitAll(t1, t2, t3);
        }
 public void Should_get_same_aggregate_from_different_cache_repository()
 {
     var rep = new CacheRepository(new TestRepository(), new TestInMemoryEventStore(), _memoryCache);
     var aggregate = rep.Get<TestAggregate>(_aggregate.Id);
     Assert.Equal(_aggregate.DidSomethingCount, aggregate.DidSomethingCount);
     Assert.Equal(_aggregate.Id, aggregate.Id);
     Assert.Equal(_aggregate.Version, aggregate.Version);
 }
        public When_saving_same_aggregate_in_parallel()
        {
            // New up a new Cache for each run
            var memoryCache = new CqrsMemoryCache();

            _testStore = new TestInMemoryEventStore();
            _rep1 = new CacheRepository(new Repository(_testStore, new TestEventPublisher()), _testStore, memoryCache);
            _rep2 = new CacheRepository(new Repository(_testStore, new TestEventPublisher()), _testStore, memoryCache);

            _aggregate = new TestAggregate(Guid.NewGuid());
            _rep1.Save(_aggregate);

            var t1 = new Task(() =>
            {
                for (var i = 0; i < 100; i++)
                {
                    var aggregate = _rep1.Get<TestAggregate>(_aggregate.Id);
                    aggregate.DoSomething();
                    _rep1.Save(aggregate);
                }
            });

            var t2 = new Task(() =>
            {
                for (var i = 0; i < 100; i++)
                {
                    var aggregate = _rep2.Get<TestAggregate>(_aggregate.Id);
                    aggregate.DoSomething();
                    _rep2.Save(aggregate);
                }
            });
            var t3 = new Task(() =>
            {
                for (var i = 0; i < 100; i++)
                {
                    var aggregate = _rep2.Get<TestAggregate>(_aggregate.Id);
                    aggregate.DoSomething();
                    _rep2.Save(aggregate);
                }
            });
            t1.Start();
            t2.Start();
            t3.Start();

            Task.WaitAll(t1, t2, t3);
        }
Example #5
0
        public void Test()
        {
            var repository = new MockRepository<Person>();
            var cacheRepository = new CacheRepository<Person>();

            var people = repository.Get();

            Assert.IsTrue(people.Count() == 3);

            var cachedPeople = cacheRepository.Get();

            Assert.IsTrue(cachedPeople.Count() == 3);
        }
        public When_saving_two_aggregates_in_parallel()
        {
            // This will clear the cache between runs.
            var memoryCache = new CqrsMemoryCache();

            _testStore = new TestInMemoryEventStore();
            _rep1 = new CacheRepository(new Repository(_testStore, new TestEventPublisher()), _testStore, memoryCache);

            _aggregate1 = new TestAggregate(Guid.NewGuid());
            _aggregate2 = new TestAggregate(Guid.NewGuid());

            _rep1.Save(_aggregate1);
            _rep1.Save(_aggregate2);

            var t1 = new Task(() =>
            {
                for (var i = 0; i < 100; i++)
                {
                    var aggregate = _rep1.Get<TestAggregate>(_aggregate1.Id);
                    aggregate.DoSomething();
                    _rep1.Save(aggregate);
                }
            });

            var t2 = new Task(() =>
            {
                for (var i = 0; i < 100; i++)
                {
                    var aggregate = _rep1.Get<TestAggregate>(_aggregate2.Id);
                    aggregate.DoSomething();
                    _rep1.Save(aggregate);
                }
            });
            t1.Start();
            t2.Start();

            Task.WaitAll(new[] { t1, t2 });
        }
Example #7
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        protected virtual WorkflowEngineEntity GetWorkflowEngineEntity()
        {
            var engine = CacheRepository.Get <WorkflowEngineEntity>(CacheKey);

            if (engine == null)
            {
                lock (CacheLocker)
                {
                    engine = CacheRepository.Get <WorkflowEngineEntity>(CacheKey);
                    if (engine == null)
                    {
                        engine = SetArgsCache();
                    }
                }
            }
            return(engine);
        }
Example #8
0
        public When_saving_same_aggregate_in_parallel()
        {
            var memoryCache = new MemoryCache();

            _testStore = new TestInMemoryEventStore();
            _rep1      = new CacheRepository(new Repository(_testStore), _testStore, memoryCache);
            _rep2      = new CacheRepository(new Repository(_testStore), _testStore, memoryCache);

            _aggregate = new TestAggregate(Guid.NewGuid());
            _rep1.Save(_aggregate).Wait();

            var t1 = new Task(async() =>
            {
                for (var i = 0; i < 100; i++)
                {
                    var aggregate = await _rep1.Get <TestAggregate>(_aggregate.Id);
                    aggregate.DoSomething();
                    await _rep1.Save(aggregate);
                }
            });

            var t2 = new Task(async() =>
            {
                for (var i = 0; i < 100; i++)
                {
                    var aggregate = await _rep2.Get <TestAggregate>(_aggregate.Id);
                    aggregate.DoSomething();
                    await _rep2.Save(aggregate);
                }
            });
            var t3 = new Task(async() =>
            {
                for (var i = 0; i < 100; i++)
                {
                    var aggregate = await _rep2.Get <TestAggregate>(_aggregate.Id);
                    aggregate.DoSomething();
                    await _rep2.Save(aggregate);
                }
            });

            t1.Start();
            t2.Start();
            t3.Start();

            Task.WaitAll(t1, t2, t3);
        }
Example #9
0
        /// <summary>
        /// 得到角色
        /// </summary>
        /// <param name="accountId"></param>
        /// <returns></returns>
        protected virtual long[] GetAuditors(long accountId)
        {
            var key   = GetAuditorAccountCacheKey(accountId);
            var value = CacheRepository.Get <long[]>(key);

            if (value == null)
            {
                var query = new QueryInfo();
                query.Query <AuditorAccountEntity>().Where(it => it.Account.Id == accountId).Select(it => it.Auditor.Id);
                var infos = Repository.GetEntities <AuditorAccountEntity>(query);
                value = infos == null
                    ? new long[0]
                    : infos.Where(it => it.Auditor != null).Select(it => it.Auditor.Id).ToArray();
                CacheRepository.Set(key, value, 3600 * 15);
            }
            return(value);
        }
Example #10
0
        public async void Should_Update()
        {
            // Arrange
            var entities = FakeEntity.FakeEntities();
            var entity   = entities.First();

            entity.IsDeleted = true;
            var repository = new CacheRepository <FakeEntity>(entities);

            // Act
            await repository.UpdateAsync(UserId, entity);

            var result = repository.Get(entity.Id);

            // Assert
            Assert.AreEqual(entity, result);
        }
Example #11
0
 public async Task SaveStatisticsAsync(List <TweetModel> tweets)
 {
     await Task.Run(() => {
         int numberOfTotalTweets           = (int)CacheRepository.Get <int>("NumberOfTotalTweets");
         NumberOfTotalTweetsPerSession    += tweets.Count;
         numberOfTotalTweets              += NumberOfTotalTweetsPerSession;
         TimeSpan timeSpan                 = DateTime.Now - StartTime;
         int?timeElapsedInSecondsFromCache = CacheRepository.Get <int?>("TimeElapsedInSeconds");
         TimeElapsedInSeconds              = ((int)timeSpan.TotalSeconds);
         TweetsPerSecond     = (int)Math.Round((double)(numberOfTotalTweets / TimeElapsedInSeconds));
         int tweetsPerMinute = TweetsPerSecond * 60;
         int tweetsPerHour   = tweetsPerMinute * 60;
         CacheRepository.Set <int>("NumberOfTotalTweets", numberOfTotalTweets, 9999);
         CacheRepository.Set <long>("TimeElapsedInSeconds", TimeElapsedInSeconds, 9999);
         CacheRepository.Set <int>("TweetsPerSecond", TweetsPerSecond, 9999);
         CacheRepository.Set <long>("TweetsPerMinute", tweetsPerMinute, 9999);
         CacheRepository.Set <long>("TweetsPerHour", tweetsPerHour, 9999);
     });
 }
Example #12
0
        /// <summary>
        /// 得到角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        protected virtual long GetFlowIdByNodeId(long id)
        {
            var key   = GetNodeCacheKey(id);
            var value = CacheRepository.Get <string>(key);

            if (value == null)
            {
                var query = new QueryInfo();
                query.Query <NodeEntity>()
                .Select(it => it.Flow.Id);
                var flowId = Repository.GetEntities <NodeEntity>(query)?.FirstOrDefault()?.Flow?.Id;
                if (flowId.HasValue)
                {
                    CacheRepository.Set(key, flowId, DateTime.MaxValue);
                    value = flowId.ToString();
                }
            }
            return(value.Convert <long>());
        }
Example #13
0
        /// <summary>
        /// 得到角色
        /// </summary>
        /// <param name="flowKey"></param>
        /// <returns></returns>
        protected virtual FlowEntity GetFlow(string flowKey)
        {
            var key   = GetFlowCacheKey(flowKey);
            var value = CacheRepository.Get <FlowEntity>(key);

            if (value == null)
            {
                var query = new QueryInfo();
                query.Query <FlowEntity>()
                .Select(
                    it => new object[]
                {
                    it,
                    it.Nodes.Select(s => new object[] { s, s.Auditor.AuditorAccounts.Select(n => n), s.Conditions.Select(n => n), s.NodeProperties.Select(n => n) }),
                });
                value = Repository.GetEntities <FlowEntity>(query)?.FirstOrDefault();
                CacheRepository.Set(key, value, DateTime.MaxValue);
            }
            CreateArgsService(value);
            return(value);
        }
        private SessionStateStoreData LoadSessionStateStoreData(HttpContext context,
                                                                string id,
                                                                TimeSpan sessionTimeout)
        {
            if (IsDebugEnabled)
            {
                log.Debug("세션 정보를 캐시에서 로드합니다... id=[{0}]", id);
            }

            try {
                SessionStateItemCollection itemCollection = null;

                var bytes = CacheRepository.Get(id) as byte[];

                if (bytes != null)
                {
                    itemCollection = WebTool.DeserializeSessionState(bytes);

                    if (IsDebugEnabled)
                    {
                        log.Debug("세션 정보를 캐시에서 로드했습니다!!! id=[{0}]", id);
                    }
                }

                return(new SessionStateStoreData(itemCollection ?? new SessionStateItemCollection(),
                                                 SessionStateUtility.GetSessionStaticObjects(context),
                                                 (int)sessionTimeout.TotalMinutes));
            }
            catch (Exception ex) {
                if (log.IsWarnEnabled)
                {
                    log.Warn("캐시에 세션 정보를 로드하는데 실패했습니다. id=[{0}]", id);
                    log.Warn(ex);
                }
            }
            return(null);
        }
Example #15
0
        static void Main(string[] args)
        {
            IRepository <Customer> cacheRepository = new CacheRepository <Customer>();

            bool tryAgain = true;

            do
            {
                // Add record to cache
                Console.WriteLine("Adding record to cache");
                Customer customer = new Customer()
                {
                    FirstName = "Donald", LastName = "Trump"
                };
                cacheRepository.Create(customer);

                // Get cache items
                Console.WriteLine("Retrieving records from cache");
                IEnumerable <Customer> customers = cacheRepository.Get();
                Console.WriteLine(string.Format("Amount of items in cache: {0}", customers.Count()));

                // Remove from cache
                Console.WriteLine("Removing record from cache");
                cacheRepository.Delete(customer);
                Console.WriteLine(string.Format("Amount of items in cache: {0}", customers.Count()));

                Console.WriteLine("Re-run? (Y/N)");
                if (Console.ReadLine() != "Y")
                {
                    tryAgain = false;
                }
            }while (tryAgain);


            Console.WriteLine("Press <Enter> to stop the client.");
            Console.ReadLine();
        }
 public void Should_get_same_aggregate_from_different_cache_repository()
 {
     var rep = new CacheRepository(new TestRepository(), new TestInMemoryEventStore());
     var aggregate = rep.Get<TestAggregate>(_aggregate.Id);
     Assert.That(aggregate, Is.EqualTo(_aggregate));
 }
 public void Setup()
 {
     _rep = new CacheRepository(new TestRepository(), new TestEventStore());
     _aggregate = _rep.Get<TestAggregate>(Guid.NewGuid());
 }
Example #18
0
        public async Task ProcessHashTagsAsync(List <TweetModel> tweets)
        {
            List <string> listOfHashtags = new List <string>();

            try
            {
                await Task.Run(() =>
                {
                    if (tweets != null && tweets.Count > 0)
                    {
                        List <string> listOfTweetTexts = tweets.Select(x => x.FullText).ToList();
                        listOfHashtags.AddRange(this.HashtagProcessor.GetListOfHashtags(tweets));
                        Dictionary <string, int> hashTagsWithCount = CacheRepository.Get <Dictionary <string, int> >("HashtagsWithCount") != null ? CacheRepository.Get <Dictionary <string, int> >("HashtagsWithCount") : new Dictionary <string, int>();
                        int tweetsWithHashtags = CacheRepository.Get <int>("TweetsWithHashTags");
                        hashTagsWithCount      = this.HashtagProcessor.GetHashtagsWithCount(hashTagsWithCount, listOfHashtags);
                        if (hashTagsWithCount == null)
                        {
                            hashTagsWithCount = new Dictionary <string, int>();
                        }
                        Dictionary <string, int> topHashtags = hashTagsWithCount.OrderByDescending(x => x.Value).Take(25).ToDictionary(x => x.Key, x => x.Value);
                        tweetsWithHashtags += this.HashtagProcessor.GetNumberOfTweetsWithHashTags(tweets);
                        CacheRepository.Set <Dictionary <string, int> >("HashtagsWithCount", hashTagsWithCount, 9999);
                        CacheRepository.Set <Dictionary <string, int> >("TopHashtags", topHashtags, 9999);
                        CacheRepository.Set <Dictionary <string, int> >("TweetsWithHashTags", topHashtags, 9999);
                        CacheRepository.Set <int>("TweetsWithHashTags", tweetsWithHashtags, 9999);
                    }
                }
                               );
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Error at ProcessEmojis", null);
            }
        }
        public void Should_get_events_from_start()
        {
            var aggregate = _rep.Get <TestAggregate>(_aggregate.Id);

            Assert.That(aggregate.Version, Is.EqualTo(4));
        }
        public void Should_get_same_aggregate_on_get()
        {
            var aggregate = _rep.Get <TestAggregate>(_aggregate.Id);

            Assert.That(aggregate, Is.EqualTo(_aggregate));
        }
Example #21
0
 public When_getting_earlier_than_expected_events_from_event_store()
 {
     _memoryCache = new MemoryCache();
     _rep         = new CacheRepository(new TestRepository(), new TestEventStoreWithBugs(), _memoryCache);
     _aggregate   = _rep.Get <TestAggregate>(Guid.NewGuid()).Result;
 }
 public When_getting_wrong_events_from_event_store()
 {
     _memoryCache = new CqrsMemoryCache();
     _rep = new CacheRepository(new TestRepository(), new TestEventStoreWithBugs(), _memoryCache);
     _aggregate = _rep.Get<TestAggregate>(Guid.NewGuid());
 }
Example #23
0
        public async Task Should_get_same_aggregate_on_second_try()
        {
            var aggregate = await _rep.Get <TestAggregate>(_aggregate.Id);

            Assert.Equal(_aggregate, aggregate);
        }
Example #24
0
 public When_getting_aggregate()
 {
     _memoryCache = new MemoryCache();
     _rep         = new CacheRepository(new TestRepository(), new TestEventStore(), _memoryCache);
     _aggregate   = _rep.Get <TestAggregate>(Guid.NewGuid()).Result;
 }
Example #25
0
 public string Get(string key) => _cache.Get(key);
        public void Should_get_same_aggregate_on_get()
        {
            var aggregate = _rep.Get <TestAggregate>(_aggregate.Id);

            Assert.Equal(_aggregate, aggregate);
        }
Example #27
0
 public void Setup()
 {
     _rep       = new CacheRepository <ISingleSignOnToken>(new TestAggregateRepository(), new TestEventStore());
     _aggregate = _rep.Get <TestAggregate>(Guid.NewGuid());
 }
 public When_getting_aggregate()
 {
     _memoryCache = new MemoryCache();
     _rep = new CacheRepository(new TestRepository(), new TestEventStore(), _memoryCache);
     _aggregate = _rep.Get<TestAggregate>(Guid.NewGuid());
 }
 public void Setup()
 {
     _rep       = new CacheRepository(new TestRepository(), new TestEventStore());
     _aggregate = _rep.Get <TestAggregate>(Guid.NewGuid());
 }