public void TestRemovePartialEvents()
        {
            var data = new ConcurrentObservableDictionary <string, double>();
            var obs  = new SimpleObserver();

            data.AddPartialObserver(obs, "test", "test2", "test3");

            data.AddOrUpdate("test", 1.0);
            Assert.AreEqual(1.0, obs.LastValue, "Error in test key");

            Assert.IsTrue(data.RemovePartialObserver(obs, "test").All(
                              pair => pair.Key == "test" && pair.Value.Count == 1 && pair.Value.Contains(obs)), "remove <obs, test>");

            data.AddOrUpdate("test", 10.0);
            Assert.AreEqual(1.0, obs.LastValue, "Error in test key after remove <obs, test>");

            Assert.IsTrue(data.RemovePartialObserver("test3").All(pair => pair.Key == "test3"), "remove test3 key");

            data.AddOrUpdate("test3", 30.0);
            Assert.AreEqual(1.0, obs.LastValue, "Error in test3 key after remove test3 key");

            data.AddOrUpdate("test2", 2.0);
            Assert.AreEqual(2.0, obs.LastValue, "Error in test2 key");

            Assert.IsTrue(data.RemovePartialObserver(obs).All(
                              pair => pair.Value.Count == 1 && pair.Value.Contains(obs)), "remove obs");

            data.AddOrUpdate("test2", 20.0);
            Assert.AreEqual(2.0, obs.LastValue, "Error in test2 key after remove obs");
        }
        protected void Subscribe(string key, int count = 1)
        {
            if (key is null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            CachedKeys.AddOrUpdate(key, 1, (k, i) => i + count);
        }
Exemple #3
0
 private static void Main(string[] args)
 {
     Cache.CollectionChanged += (s, e) => { Console.WriteLine($@"{e.Key}: {e.NewValue}"); };
     Cache.AddPartialObserver(e => { Console.WriteLine($@"Auto observer: {e.Key}: {e.NewValue}"); }, "a");
     Cache.AddOrUpdate("a", 22.2);
     Cache.AddOrUpdate("b", 23.2);
     Cache.AddOrUpdate("c", 24.2);
     Cache.AddOrUpdate("a", 22.2);
     Cache.AddOrUpdate("a", 25.2);
 }
        public void TestAddPartialEventsByAction()
        {
            var data = new ConcurrentObservableDictionary <string, double>();
            var ret  = 0.0;

            data.AddPartialObserver(args => ret = args.NewValue, "test", "test2");

            data.AddOrUpdate("test", 1.0);
            Assert.AreEqual(1.0, ret, "Error in test key");

            data.AddOrUpdate("test2", 10.0);
            Assert.AreEqual(10.0, ret, "Error in test2 key");
        }
        public void TestAddPartialEvents()
        {
            var data = new ConcurrentObservableDictionary <string, double>();
            var obs  = new SimpleObserver();

            data.AddPartialObserver(obs, "test", "test2");

            data.AddOrUpdate("test", 1.0);
            Assert.AreEqual(1.0, obs.LastValue, "Error in test key");

            data.AddOrUpdate("test2", 10.0);
            Assert.AreEqual(10.0, obs.LastValue, "Error in test2 key");
        }
        public void TestClearCache()
        {
            var data = new ConcurrentObservableDictionary <string, double>();
            var obs  = new SimpleObserver();

            data.AddPartialObserver(obs, "test", "test2", "test3");

            data.AddOrUpdate("test", 2.0);
            data.AddOrUpdate("test2", 12.0);
            data.AddOrUpdate("test3", 32.0);
            Assert.IsFalse(data.IsEmpty, "data is empty");

            data.Clear();
            Assert.IsTrue(data.IsEmpty, "data is not empty");
        }
        protected virtual async Task UpdateAllData()
        {
            var values = await Database.StringGetAsync(CurrentCachedKeys).ConfigureAwait(false);

            for (var i = 0; i < CurrentCachedKeys.Length; ++i)
            {
                Cache.AddOrUpdate(CurrentCachedKeys[i], values[i]);
            }

            foreach (var cacheKey in Cache.Keys)
            {
                if (CachedKeys.ContainsKey(cacheKey))
                {
                    continue;
                }
                _tasks.Add(Task.Run(() => { Cache.TryRemove(cacheKey, out _); }));
            }

            if (_tasks.Count == 0)
            {
                return;
            }

            Task.WaitAll(_tasks.ToArray());
            _tasks.Clear();
        }
Exemple #8
0
        protected virtual void UpdateAllData()
        {
            _tasks.Clear();


            for (var i = 0; i < CachedKeys.Keys.Count; i++)
            {
                var index = i;

                _tasks.Add(Task.Run(() =>
                {
                    var key = CachedKeys.Keys.ElementAt(index);
                    Cache.AddOrUpdate(key, Database.StringGet(key));
                }));
            }


            foreach (var cacheKey in Cache.Keys)
            {
                if (CachedKeys.ContainsKey(cacheKey))
                {
                    continue;
                }

                _tasks.Add(Task.Run(() => { Cache.TryRemove(cacheKey, out _); }));
            }


            Task.WaitAll(_tasks.ToArray());
        }
        public void TestEvents()
        {
            var data    = new ConcurrentObservableDictionary <string, double>();
            var updated = false;

            data.CollectionChanged += (s, e) => { updated = true; };

            data.AddOrUpdate("test", 1.0);

            Assert.IsTrue(updated);
        }