예제 #1
0
        public void CloudObjectsLiveSource()
        {
            var client = GetClient();

            const string objectName = "object1";
            var          local      = new SampleObject()
            {
                FirstName = "jinga",
                LastName  = "zamooga"
            };

            client.DeleteAsync(objectName).Wait();
            client.SaveAsync(objectName, local).Wait();

            var cache = new SampleDbCache(() => LocalDb.GetConnection(dbName));

            // assume table hasn't been created yet
            DropCacheTable(cache);

            // first fetch is always live
            var fetched = cache.GetAsync(objectName,
                                         async() =>
            {
                var cloudObj = await client.GetAsync <SampleObject>(objectName);
                return(cloudObj.Object);
            }, TimeSpan.FromSeconds(2)).Result;

            // simulate a delay past the 2-second max age allowed
            Task.Delay(3000).Wait();

            // it's the second fetch we care about
            fetched = cache.GetAsync(objectName,
                                     async() =>
            {
                var cloudObj = await client.GetAsync <SampleObject>(objectName);
                return(cloudObj.Object);
            }, TimeSpan.FromSeconds(2)).Result;

            Assert.IsTrue(fetched.FirstName.Equals(local.FirstName));
            Assert.IsTrue(fetched.LastName.Equals(local.LastName));
            Assert.IsTrue(cache.Source == ObjectSource.Live);
        }
예제 #2
0
        public void CloudObjectsCacheSource()
        {
            var client = GetClient();

            const string objectName = "object2";
            var          local      = new SampleObject()
            {
                FirstName = "jinga",
                LastName  = "zamooga"
            };

            client.DeleteAsync(objectName).Wait();
            client.SaveAsync(objectName, local).Wait();

            var cache = new SampleDbCache(() => LocalDb.GetConnection(dbName));

            // assume table hasn't been created yet
            DropCacheTable(cache);

            // first fetch will be live because it's a new object
            var fetched = cache.GetAsync(objectName,
                                         async() =>
            {
                var cloudObj = await client.GetAsync <SampleObject>(objectName);
                return(cloudObj.Object);
            }, TimeSpan.FromMinutes(5)).Result;

            // second fetch will be cached because it's within 5 minute window
            fetched = cache.GetAsync(objectName,
                                     async() =>
            {
                var cloudObj = await client.GetAsync <SampleObject>(objectName);
                return(cloudObj.Object);
            }, TimeSpan.FromMinutes(5)).Result;

            Assert.IsTrue(fetched.FirstName.Equals(local.FirstName));
            Assert.IsTrue(fetched.LastName.Equals(local.LastName));
            Assert.IsTrue(cache.Source == ObjectSource.Cache);
        }
예제 #3
0
        public void CloudObjectsFixedDateExpiration()
        {
            var client = GetClient();
            var cache  = new SampleDbCache(() => LocalDb.GetConnection(dbName));

            var sample = new SampleObject()
            {
                FirstName = "Great",
                LastName  = "Mazinga"
            };

            client.DeleteAsync("object3").Wait();
            client.SaveAsync("object3", sample).Wait();

            // should always get live because it's always later than an hour ago
            var fetched = cache.GetAsync("object3",
                                         async() =>
            {
                var result = await client.GetAsync <SampleObject>("object3");
                return(result.Object);
            },
                                         DateTime.UtcNow.Subtract(TimeSpan.FromHours(1))).Result;

            Assert.IsTrue(cache.Source == ObjectSource.Live);

            fetched = cache.GetAsync("object3",
                                     async() =>
            {
                var result = await client.GetAsync <SampleObject>("object3");
                return(result.Object);
            },
                                     DateTime.UtcNow.Add(TimeSpan.FromHours(1))).Result;

            // should always get cache because it's always earlier than an hour from now
            Assert.IsTrue(cache.Source == ObjectSource.Cache);
        }