예제 #1
0
        public void TestCache()
        {
            Func <string, TableDescription> creator = tn => client.DescribeTable(tn).Table;

            var tableName  = GetTableName();
            var tableCache = SdkCache.GetCache <string, TableDescription>(client, DynamoDBTests.TableCacheIdentifier, StringComparer.Ordinal);

            Assert.AreEqual(0, tableCache.ItemCount);

            using (var counter = new ServiceResponseCounter(client))
            {
                var table = tableCache.GetValue(tableName, creator);
                Assert.AreEqual(1, counter.ResponseCount);
                Assert.AreEqual(1, tableCache.ItemCount);

                // verify the item is still there
                table = tableCache.GetValue(tableName, creator);
                Assert.AreEqual(1, counter.ResponseCount);

                // verify item was reloaded
                tableCache.Clear(tableName);
                table = tableCache.GetValue(tableName, creator);
                Assert.AreEqual(2, counter.ResponseCount);
                Assert.AreEqual(1, tableCache.ItemCount);

                // test item expiration
                tableCache.MaximumItemLifespan = TimeSpan.FromSeconds(1);
                Thread.Sleep(tableCache.MaximumItemLifespan);
                table = tableCache.GetValue(tableName, creator);
                Assert.AreEqual(3, counter.ResponseCount);
                Assert.AreEqual(1, tableCache.ItemCount);
            }
        }
예제 #2
0
        public void ChangingTableTest()
        {
            var item = new Document(new Dictionary <string, DynamoDBEntry>
            {
                { "Id", 42 },
                { "Name", "Floyd" },
                { "Coffee", "Yes" }
            });
            var tableCache = SdkCache.GetCache <string, TableDescription>(client, DynamoDBTests.TableCacheIdentifier, StringComparer.Ordinal);

            CreateTable(defaultKeys: true);
            var table = Table.LoadTable(client, tableName);

            table.PutItem(item);

            using (var counter = new ServiceResponseCounter(client))
            {
                var doc = table.GetItem(42, "Floyd");
                Assert.IsNotNull(doc);
                Assert.AreNotEqual(0, doc.Count);
                Assert.AreEqual(1, counter.ResponseCount);
                AssertExtensions.ExpectException(() => table.GetItem("Floyd", 42));

                var oldTableDescription = tableCache.GetValue(tableName, null);

                DeleteTable();
                CreateTable(defaultKeys: false);

                table.PutItem(item);
                AssertExtensions.ExpectException(() => table.GetItem(42, "Yes"));

                counter.Reset();
                Table.ClearTableCache();
                table = Table.LoadTable(client, tableName);
                doc   = table.GetItem(42, "Yes");
                Assert.IsNotNull(doc);
                Assert.AreNotEqual(0, doc.Count);
                Assert.AreEqual(2, counter.ResponseCount);

                counter.Reset();
                Table.ClearTableCache();
                PutItem(tableCache, tableName, oldTableDescription);
                table = Table.LoadTable(client, tableName);
                doc   = tableCache.UseCache(tableName,
                                            () => table.GetItem(42, "Yes"),
                                            () => table = Table.LoadTable(client, tableName),
                                            shouldRetryForException: null);

                Assert.IsNotNull(doc);
                Assert.AreNotEqual(0, doc.Count);
                Assert.AreEqual(2, counter.ResponseCount);
            }
        }