Ejemplo n.º 1
0
        public async Task SimpleReadThroughCache()
        {
            var path = System.IO.Path.GetTempFileName();
            var conn = new SQLite.SQLiteAsyncConnection(path);
            var db   = new TestDatabase(conn);
            await db.Reset();

            var sqliteThingCache = new SqliteClassCache <Parent, long>(db);
            await sqliteThingCache.Setup();

            var cache1 = new ModelCache(aClassCache: new Dictionary <Type, IModelClassCache>()
            {
                { typeof(Parent), sqliteThingCache }
            });

            // read from origin
            var cascade = new CascadeDataLayer(origin, new ICascadeCache[] { cache1 }, new CascadeConfig()
            {
                DefaultFreshnessSeconds = 1
            });
            var thing1 = await cascade.Get <Parent>(5);

            Assert.AreEqual(5, thing1 !.id);
            Assert.AreEqual(cascade.NowMs, thing1.updatedAtMs);

            origin.IncNowMs();

            var thing2 = await cascade.Get <Parent>(5, freshnessSeconds : 2);

            Assert.AreEqual(thing1.updatedAtMs, thing2 !.updatedAtMs);

            var thing3 = await cascade.Get <Parent>(5, freshnessSeconds : 0);

            Assert.AreEqual(origin.NowMs, thing3 !.updatedAtMs);
        }
Ejemplo n.º 2
0
        public async Task ReadWithoutCache()
        {
            var cascade = new CascadeDataLayer(origin, new ICascadeCache[] {}, new CascadeConfig());
            var thing   = await cascade.Get <Parent>(5);

            Assert.AreEqual(5, thing !.id);
        }
Ejemplo n.º 3
0
        public async Task ReadWithModelCachesMultitest()
        {
            var thingModelStore1 = new ModelClassCache <Parent, long>();
            var cache1           = new ModelCache(aClassCache: new Dictionary <Type, IModelClassCache>()
            {
                { typeof(Parent), thingModelStore1 }
            });
            var thingModelStore2 = new ModelClassCache <Parent, long>();
            var cache2           = new ModelCache(aClassCache: new Dictionary <Type, IModelClassCache>()
            {
                { typeof(Parent), thingModelStore2 }
            });

            // read from origin
            var cascade = new CascadeDataLayer(origin, new ICascadeCache[] { cache1, cache2 }, new CascadeConfig()
            {
                DefaultFreshnessSeconds = 1
            });
            var thing1 = await cascade.Get <Parent>(5);

            Assert.AreEqual(5, thing1 !.id);
            Assert.AreEqual(cascade.NowMs, thing1.updatedAtMs);

            // should also be in both caches
            var store1ThingResponse = await thingModelStore1.Fetch(RequestOp.GetOp <Parent>(5, cascade.NowMs));

            Assert.AreEqual((store1ThingResponse.Result as Parent) !.id, 5);
            Assert.AreEqual(cascade.NowMs, (store1ThingResponse.Result as Parent) !.updatedAtMs);
            var store2ThingResponse = await thingModelStore2.Fetch(RequestOp.GetOp <Parent>(5, cascade.NowMs));

            Assert.AreEqual((store2ThingResponse.Result as Parent) !.id, 5);
            Assert.AreEqual(cascade.NowMs, (store2ThingResponse.Result as Parent) !.updatedAtMs);

            origin.IncNowMs();

            // freshness=5 allows for cached version
            var thing2 = (await cascade.Get <Parent>(5, freshnessSeconds: 5)) !;

            Assert.AreEqual(thing1.updatedAtMs, thing2.updatedAtMs);

            // freshness=0 doesn't allow for cached version
            var thing3 = (await cascade.Get <Parent>(5, freshnessSeconds: 0)) !;

            Assert.AreEqual(origin.NowMs, thing3.updatedAtMs);

            // caches should also be updated
            store1ThingResponse = await thingModelStore1.Fetch(RequestOp.GetOp <Parent>(5, cascade.NowMs));

            Assert.AreEqual(origin.NowMs, (store1ThingResponse.Result as Parent) !.updatedAtMs);
            store2ThingResponse = await thingModelStore2.Fetch(RequestOp.GetOp <Parent>(5, cascade.NowMs));

            Assert.AreEqual(origin.NowMs, (store2ThingResponse.Result as Parent) !.updatedAtMs);

            origin.IncNowMs(2000);

            // freshness=2 should allow for cached version
            var thing4 = (await cascade.Get <Parent>(5, freshnessSeconds: 2)) !;

            Assert.AreEqual(thing3.updatedAtMs, thing4.updatedAtMs);

            // freshness=1 should get fresh version
            var thing5 = (await cascade.Get <Parent>(5, freshnessSeconds: 1)) !;

            Assert.AreEqual(origin.NowMs, thing5.updatedAtMs);

            origin.IncNowMs(1000);

            // clear cache1, freshnessSeconds=1 should return value from cache2 and update cache1
            await cache1.Clear();

            var thing6 = (await cascade.Get <Parent>(thing4.id, freshnessSeconds: 1)) !;                                // should get cache2 version

            Assert.AreEqual(thing6.updatedAtMs, thing5.updatedAtMs);
            store1ThingResponse = await thingModelStore1.Fetch(RequestOp.GetOp <Parent>(5, cascade.NowMs));

            Assert.AreEqual(thing6.updatedAtMs, (store1ThingResponse.Result as Parent) !.updatedAtMs);
        }