public override void SetUp()
        {
            this.Context = TestConfiguration.GetDataContext();

            this.CacheClient = new MemcachedClient();

            this.CacheClient.FlushAll();
            this._cacheHitCount = 0;

            this.TableCache        = GetEnyimTableCache(this.CacheClient);
            this.TableCache.OnHit += () => this._cacheHitCount++;
            this.TableCache.OnLog += s => Logger.Debug(s);
        }
Beispiel #2
0
        public void DataContext_Caching_UserSpecificIndexUpdatedByFullIndex()
        {
            cacheClient.FlushAll();
            this._cacheHitCount = 0;

            var ctx = TestConfiguration.GetDataContext(tablePrefix);

            var fullTable = ctx.GetTable <FullEntity>(() =>
            {
                var cache    = new EnyimTableCache(cacheClient, TimeSpan.MaxValue);
                cache.OnLog += s => Debug.WriteLine("{0} FullEntityCache: {1}", DateTime.Now, s);
                cache.OnHit += this.OnCacheHit;
                return(cache);
            });

            var userSpecificTable1 = ctx.GetTable <UserEntity>
                                     (
                userId1,
                () =>
            {
                var cache    = new EnyimTableCache(cacheClient, TimeSpan.MaxValue);
                cache.OnLog += s => Debug.WriteLine("{0} UserEntityCache1: {1}", DateTime.Now, s);
                cache.OnHit += this.OnCacheHit;
                return(cache);
            }
                                     );

            var userSpecificTable2 = ctx.GetTable <UserEntity>
                                     (
                userId2,
                () =>
            {
                var cache    = new EnyimTableCache(cacheClient, TimeSpan.MaxValue);
                cache.OnLog += s => Debug.WriteLine("{0} UserEntityCache2: {1}", DateTime.Now, s);
                cache.OnHit += this.OnCacheHit;
                return(cache);
            }
                                     );

            var fullQuery          = from en in fullTable where en.DateTimeField >= dtNow && (intRange.Contains(en.IntField)) select en;
            var userSpecificQuery1 = from en in userSpecificTable1 where en.DateTimeField >= dtNow && (intRange.Contains(en.IntField)) select en;
            var userSpecificQuery2 = from en in userSpecificTable2 where en.DateTimeField < dtNow && en.IntField < 3 select en;

            // loading indexes - they should be put to cache
            Assert.AreEqual(2, fullQuery.AsEnumerable().Count(), "Failed to load full query from table");
            Assert.AreEqual(1, userSpecificQuery1.AsEnumerable().Count(), "Failed to load user-specific query1 from table");
            Assert.AreEqual(0, userSpecificQuery2.AsEnumerable().Count(), "The user-specific query2 should return 0 entities");
            Assert.AreEqual(0, this._cacheHitCount, "Cache wasn't flushed for some strange reason");

            // now loading from cache
            Assert.AreEqual(2, fullQuery.AsEnumerable().Count(), "Failed to load full query from cache");
            Assert.AreEqual(1, userSpecificQuery1.AsEnumerable().Count(), "Failed to load user-specific query from cache");
            Assert.AreEqual(0, userSpecificQuery2.AsEnumerable().Count(), "The user-specific query2 should return 0 entities");
            Assert.AreEqual(3, this._cacheHitCount, "The queries were not loaded from cache");

            // now adding 2 new entities - they should appear in both indexes
            fullTable.InsertOnSubmit
            (
                new FullEntity()
            {
                HashKey       = userId1,
                RangeKey      = rangeKey5,
                DateTimeField = dtNow + TimeSpan.FromDays(1000),
                IntField      = 5
            }
            );
            fullTable.InsertOnSubmit
            (
                new FullEntity()
            {
                HashKey       = userId1,
                RangeKey      = rangeKey4,
                DateTimeField = dtNow + TimeSpan.FromDays(9999),
                IntField      = 4
            }
            );
            ctx.SubmitChanges();

            this._cacheHitCount = 0;

            // now loading from cache
            Assert.AreEqual(4, fullQuery.AsEnumerable().Count(), "Failed to load full query from cache");
            Assert.AreEqual(3, userSpecificQuery1.AsEnumerable().Count(), "Failed to load user-specific query from cache");
            Assert.AreEqual(0, userSpecificQuery2.AsEnumerable().Count(), "The user-specific query2 should return 0 entities");
            Assert.AreEqual(3, this._cacheHitCount, "The queries were not loaded from cache");


            // now removing a couple of entities

            fullTable.RemoveOnSubmit(new FullEntity()
            {
                HashKey = userId1, RangeKey = rangeKey1
            });
            fullTable.RemoveOnSubmit(new FullEntity()
            {
                HashKey = userId1, RangeKey = rangeKey3
            });
            ctx.SubmitChanges();

            this._cacheHitCount = 0;

            // now loading from cache
            Assert.AreEqual(3, fullQuery.AsEnumerable().Count(), "Failed to load full query from cache");
            Assert.AreEqual(2, userSpecificQuery1.AsEnumerable().Count(), "Failed to load user-specific query from cache");
            Assert.AreEqual(0, userSpecificQuery2.AsEnumerable().Count(), "The user-specific query2 should return 0 entities");
            Assert.AreEqual(3, this._cacheHitCount, "The queries were not loaded from cache");

            // now loading from DynamoDb again
            cacheClient.FlushAll();
            this._cacheHitCount = 0;

            Assert.AreEqual(3, fullQuery.AsEnumerable().Count(), "Failed to load full query from cache");
            Assert.AreEqual(2, userSpecificQuery1.AsEnumerable().Count(), "Failed to load user-specific query from cache");
            Assert.AreEqual(0, userSpecificQuery2.AsEnumerable().Count(), "The user-specific query2 should return 0 entities");
            Assert.AreEqual(0, this._cacheHitCount, "Cache wasn't flushed for some strange reason");
        }