Exemplo n.º 1
0
            public void RemovesExistingValue()
            {
                var cache = new CacheStorage <string, int>();

                cache.Add("1", 1);

                Assert.IsTrue(cache.Contains("1"));

                cache.Remove("1");

                Assert.IsFalse(cache.Contains("1"));
            }
Exemplo n.º 2
0
            public void AutomaticallyRemovesExpiredItemsOfACacheStorageWithDefaultExpirationPolicyInitializationCode()
            {
                var cache = new CacheStorage <string, int>(() => ExpirationPolicy.Duration(TimeSpan.FromMilliseconds(500)));

                cache.Add("1", 1);

                Assert.IsTrue(cache.Contains("1"));

                ThreadHelper.Sleep(2000);

                Assert.IsFalse(cache.Contains("1"));
            }
Exemplo n.º 3
0
            public void AutomaticallyRemovesExpiredItems()
            {
                var cache = new CacheStorage <string, int>();

                cache.Add("1", 1, expiration: new TimeSpan(0, 0, 1));

                Assert.IsTrue(cache.Contains("1"));

                ThreadHelper.Sleep(2000);

                Assert.IsFalse(cache.Contains("1"));
            }
Exemplo n.º 4
0
            public void IsAutomaticallyEnabledWhenStartedDisabledButAddingItemWithCustomExpirationPolicy()
            {
                var cache = new CacheStorage <string, int>();

                cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250);

                cache.Add("1", 1, expiration: new TimeSpan(0, 0, 0, 0, 250));

                Assert.IsTrue(cache.Contains("1"));

                ThreadHelper.Sleep(750);

                Assert.IsFalse(cache.Contains("1"));
            }
Exemplo n.º 5
0
            public void AutomaticallyRemovesExpiredItems()
            {
                var cache = new CacheStorage <string, int>();

                cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250);

                cache.Add("1", 1, expiration: new TimeSpan(0, 0, 0, 0, 250));

                Assert.IsTrue(cache.Contains("1"));

                ThreadHelper.Sleep(750);

                Assert.IsFalse(cache.Contains("1"));
            }
Exemplo n.º 6
0
            public void ReturnsTrueForExistingKey()
            {
                var cache = new CacheStorage <string, int>();

                cache.Add("1", 1);
                cache.Add("2", 2);

                Assert.IsTrue(cache.Contains("2"));
            }
Exemplo n.º 7
0
            public void AddsItemToCacheAndReturnsIt()
            {
                var cache = new CacheStorage <string, int>();

                var value = cache.GetFromCacheOrFetch("1", () => 1);

                Assert.IsTrue(cache.Contains("1"));
                Assert.AreEqual(1, cache["1"]);
                Assert.AreEqual(1, value);
            }
Exemplo n.º 8
0
            public void ReturnsCachedItem()
            {
                var cache = new CacheStorage <string, int>();

                cache.Add("1", 1);
                var value = cache.GetFromCacheOrFetch("1", () => 2);

                Assert.IsTrue(cache.Contains("1"));
                Assert.AreEqual(1, cache["1"]);
                Assert.AreEqual(1, value);
            }
Exemplo n.º 9
0
            public void ItemStaysInCacheWhenExpiringEventIsCanceled()
            {
                var key   = "1";
                var value = 1;

                var cache = new CacheStorage <string, int>();

                cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250);
                cache.Expiring += (sender, e) =>
                {
                    e.Cancel = true;
                };

                cache.Add(key, value, expiration: new TimeSpan(0, 0, 0, 0, 250));

                ThreadHelper.Sleep(750);

                Assert.IsTrue(cache.Contains(key));
            }
Exemplo n.º 10
0
        /// <summary>
        /// Returns either a list of close contacts or a the value, if the node's storage contains the value for the key.
        /// </summary>
        public (List <Contact> contacts, string val) FindValue(Contact sender, ID key)
        {
            Validate.IsFalse <SendingQueryToSelfException>(sender.ID == ourContact.ID, "Sender should not be ourself!");
            SendKeyValuesIfNewContact(sender);
            bucketList.AddContact(sender);

            if (storage.Contains(key))
            {
                return(null, storage.Get(key));
            }
            else if (CacheStorage.Contains(key))
            {
                return(null, CacheStorage.Get(key));
            }
            else
            {
                // Exclude sender.
                return(bucketList.GetCloseContacts(key, sender.ID), null);
            }
        }
Exemplo n.º 11
0
        public static async Task <TimeZoneInfo> TryToDetermineAsync(GeoTagsEntry geoTags)
        {
            var key = Key + geoTags;

            if (CacheStorage.Contains(key))
            {
                return(CacheStorage.Get <string>(key).As <TimeZoneInfo>());
            }

            try {
                var result = await GoogleApiProvider.DetermineTimeZoneAsync(geoTags);

                CacheStorage.Set(key, result);
                return(result);
            } catch (WebException e) {
                Logging.Warning(e.Message);
                return(null);
            } catch (Exception e) {
                Logging.Warning(e);
                CacheStorage.Set(key, "");
                return(null);
            }
        }
Exemplo n.º 12
0
        public static async Task <GeoTagsEntry> TryToLocateAsync([CanBeNull] string address)
        {
            if (string.IsNullOrWhiteSpace(address))
            {
                return(null);
            }

            var key = Key + address;

            if (CacheStorage.Contains(key))
            {
                var cache = CacheStorage.Get <string>(key).As <Point?>();
                return(cache.HasValue ? new GeoTagsEntry(cache.Value.X, cache.Value.Y) : null);
            }

            try {
                var result = await YahooApiProvider.LocateAsync(address);

                if (result.LatitudeValue.HasValue && result.LongitudeValue.HasValue)
                {
                    Logging.Write($"“{address}”, geo tags: ({result})");
                    CacheStorage.Set(key, new Point(result.LatitudeValue.Value, result.LongitudeValue.Value).As <string>());
                    return(result);
                }

                CacheStorage.Set(key, "");
                return(null);
            } catch (WebException e) {
                Logging.Warning("TryToLocateAsync(): " + e.Message);
                return(null);
            } catch (Exception e) {
                Logging.Warning("TryToLocateAsync(): " + e);
                CacheStorage.Set(key, "");
                return(null);
            }
        }
Exemplo n.º 13
0
            public void AutomaticallyRemovesExpiredItemsOfACacheStorageWithDefaultExpirationPolicyInitializationCode()
            {
                var cache = new CacheStorage<string, int>(() => ExpirationPolicy.Duration(TimeSpan.FromMilliseconds(250)));
                cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250);

                cache.Add("1", 1);

                Assert.IsTrue(cache.Contains("1"));

                ThreadHelper.Sleep(750);

                Assert.IsFalse(cache.Contains("1"));
            }
Exemplo n.º 14
0
            public void AutomaticallyRemovesExpiredItems()
            {
                var cache = new CacheStorage<string, int>();
     			
     			cache.Add("1", 1, expiration: new TimeSpan(0, 0, 1));

				Assert.IsTrue(cache.Contains("1"));

				ThreadHelper.Sleep(1500);

				Assert.IsFalse(cache.Contains("1"));
			}
Exemplo n.º 15
0
            public void ThrowsArgumentNullExceptionForNullKey()
            {
                var cache = new CacheStorage<string, int>();

                ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(() => cache.Contains(null));
            }
Exemplo n.º 16
0
            public void ReturnsTrueForExistingKey()
            {
                var cache = new CacheStorage<string, int>();
                cache.Add("1", 1);
                cache.Add("2", 2);

                Assert.IsTrue(cache.Contains("2"));
            }
Exemplo n.º 17
0
            public void ReturnsCachedItem()
            {
                var cache = new CacheStorage<string, int>();

                cache.Add("1", 1);
                var value = cache.GetFromCacheOrFetch("1", () => 2);

                Assert.IsTrue(cache.Contains("1"));
                Assert.AreEqual(1, cache["1"]);
                Assert.AreEqual(1, value);
            }
Exemplo n.º 18
0
            public void AddsItemToCacheWithOverrideAndReturnsIt()
            {
                var cache = new CacheStorage<string, int>();

                cache.Add("1", 1);
                var value = cache.GetFromCacheOrFetch("1", () => 2, true);

                Assert.IsTrue(cache.Contains("1"));
                Assert.AreEqual(2, cache["1"]);
                Assert.AreEqual(2, value);
            }
Exemplo n.º 19
0
            public void RemovesExistingValue()
            {
                var cache = new CacheStorage<string, int>();
                cache.Add("1", 1);

                Assert.IsTrue(cache.Contains("1"));

                cache.Remove("1");

                Assert.IsFalse(cache.Contains("1"));
            }
Exemplo n.º 20
0
            public void ThrowsArgumentNullExceptionForNullKey()
            {
                var cache = new CacheStorage <string, int>();

                ExceptionTester.CallMethodAndExpectException <ArgumentNullException>(() => cache.Contains(null));
            }
Exemplo n.º 21
0
            public void ItemStaysInCacheWhenExpiringEventIsCanceled()
            {
                var key = "1";
                var value = 1;

                var cache = new CacheStorage<string, int>();
                cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250);
                cache.Expiring += (sender, e) =>
                {
                    e.Cancel = true;
                };

                cache.Add(key, value, expiration: new TimeSpan(0, 0, 0, 0, 250));

                ThreadHelper.Sleep(750);

                Assert.IsTrue(cache.Contains(key));
            }
Exemplo n.º 22
0
            public void IsAutomaticallyEnabledWhenStartedDisabledButAddingItemWithCustomExpirationPolicy()
            {
                var cache = new CacheStorage<string, int>();
                cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250);

                cache.Add("1", 1, expiration: new TimeSpan(0, 0, 0, 0, 250));

                Assert.IsTrue(cache.Contains("1"));

                ThreadHelper.Sleep(750);

                Assert.IsFalse(cache.Contains("1"));
            }
Exemplo n.º 23
0
            public void AutomaticallyRemovesExpiredItems()
            {
                var cache = new CacheStorage<string, int>();
                cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250);

                cache.Add("1", 1, expiration: new TimeSpan(0, 0, 0, 0, 250));

                Assert.IsTrue(cache.Contains("1"));

                ThreadHelper.Sleep(750);

                Assert.IsFalse(cache.Contains("1"));
            }