Example #1
0
        /// <summary>
        ///     Adds the specified <paramref name="keyValuePair" /> to the <paramref name="cache" />.
        /// </summary>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="cache">The cache to use.</param>
        /// <param name="keyValuePair">The key/value pair to add.</param>
        /// <param name="expiry">
        ///     The expiry of the <paramref name="keyValuePair" />. If none is provided the
        ///     <paramref name="keyValuePair" /> will virtually never expire.
        /// </param>
        /// <param name="expirationType">Defines how the <paramref name="keyValuePair" /> shall expire.</param>
        /// <param name="scheduler">The scheduler to run the addition on.</param>
        /// <returns>
        ///     An observable stream that, when done, returns an <see cref="Unit" />.
        /// </returns>
        public static IObservable <KeyValuePair <TKey, TValue> > Add <TKey, TValue>(this IObservableCache <TKey, TValue> cache, KeyValuePair <TKey, TValue> keyValuePair, TimeSpan expiry, ObservableCacheExpirationType expirationType = ObservableCacheExpirationType.DoNothing, IScheduler scheduler = null)
        {
            if (cache == null)
            {
                throw new ArgumentNullException(nameof(cache));
            }

            return(cache.Add(Observable.Return(keyValuePair), expiry, expirationType, scheduler));
        }
Example #2
0
        /// <summary>
        /// Adds the specified <paramref name="key" /> with the given <paramref name="value" /> to the <paramref name="cache"/>
        /// with its expiry set to <see cref="TimeSpan.MaxValue"/> and <see cref="ObservableCacheExpirationType"/> to <see cref="ObservableCacheExpirationType.DoNothing"/>.
        /// </summary>
        /// <param name="cache">The cache to use.</param>
        /// <param name="key">The key of the element to add.</param>
        /// <param name="value">The value of the element to add.</param>
        /// <param name="scheduler">Scheduler to perform the add action on.</param>
        /// <returns>
        /// An observable stream that, when done, returns an <see cref="Unit" />.
        /// </returns>
        public static IObservable <KeyValuePair <TKey, TValue> > Add <TKey, TValue>(this IObservableCache <TKey, TValue> cache, TKey key, TValue value, IScheduler scheduler = null)
        {
            if (cache == null)
            {
                throw new ArgumentNullException(nameof(cache));
            }
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            return(cache.Add(new KeyValuePair <TKey, TValue>(key, value), scheduler));
        }
        public void ConverterObservableCacheTests()
        {
            IObservableCache  cache    = InstantiateCache();
            IConverter        cDown    = new ConvertDown();
            IConverter        cUp      = new ConvertUp();
            TestCacheListener listener = new TestCacheListener();

            IObservableCache convCache = ConverterCollections.GetObservableCache(cache, cUp, cDown, cUp, cDown);

            Assert.IsNotNull(convCache);
            //AddCacheListener(listener)
            convCache.AddCacheListener(listener);
            Assert.AreEqual(listener.inserted, 0);
            convCache.Add(1, 1);
            Assert.AreEqual(listener.inserted, 1);
            Assert.AreEqual(listener.updated, 0);
            convCache[1] = 2;
            Assert.AreEqual(listener.updated, 1);
            Assert.AreEqual(listener.deleted, 0);
            convCache.Remove(1);
            Assert.AreEqual(listener.deleted, 1);
            //RemoveCacheListener(listener)
            convCache.RemoveCacheListener(listener);
            Assert.AreEqual(listener.inserted, 1);
            convCache.Add(1, 1);
            Assert.AreEqual(listener.inserted, 1);

            listener.inserted = listener.updated = listener.deleted = 0;
            convCache.Clear();
            //AddCacheListener(listener, key, isLite);
            convCache.AddCacheListener(listener, "1", true);
            for (int i = 0; i < 3; i++)
            {
                convCache.Add(i, i);
            }
            Assert.AreEqual(listener.inserted, 1);
            Assert.AreEqual(listener.updated, 0);
            Assert.AreEqual(listener.deleted, 0);
            for (int i = 0; i < 3; i++)
            {
                convCache[i] = i + 1;
            }
            Assert.AreEqual(listener.inserted, 1);
            Assert.AreEqual(listener.updated, 1);
            Assert.AreEqual(listener.deleted, 0);
            convCache.Clear();
            Assert.AreEqual(listener.inserted, 1);
            Assert.AreEqual(listener.updated, 1);
            Assert.AreEqual(listener.deleted, 1);
            //RemoveCacheListener(listener, key)
            convCache.RemoveCacheListener(listener, "1");
            convCache.Add(1, 1);
            Assert.AreEqual(listener.inserted, 1);

            listener.inserted = listener.updated = listener.deleted = 0;
            convCache.Clear();
            IFilter filter = AlwaysFilter.Instance;

            //AddCacheListener(listener, filter, isLite)
            convCache.AddCacheListener(listener, filter, true);
            for (int i = 0; i < 3; i++)
            {
                convCache.Add(i, i);
            }
            Assert.AreEqual(listener.inserted, 3);
            Assert.AreEqual(listener.updated, 0);
            Assert.AreEqual(listener.deleted, 0);
            for (int i = 0; i < 3; i++)
            {
                convCache[i] = i + 1;
            }
            Assert.AreEqual(listener.inserted, 3);
            Assert.AreEqual(listener.updated, 3);
            Assert.AreEqual(listener.deleted, 0);
            convCache.Clear();
            Assert.AreEqual(listener.inserted, 3);
            Assert.AreEqual(listener.updated, 3);
            Assert.AreEqual(listener.deleted, 3);
            //RemoveCacheListener(listener, filter)
            convCache.RemoveCacheListener(listener, filter);
            convCache.Add(1, 1);
            Assert.AreEqual(listener.inserted, 3);

            Assert.IsInstanceOf(typeof(ConverterCollections.ConverterObservableCache), convCache);
            ConverterCollections.ConverterObservableCache coc =
                convCache as ConverterCollections.ConverterObservableCache;
            Assert.IsNotNull(coc);
            Assert.AreEqual(coc.ObservableCache, cache);
        }