/// <summary>
        /// Initializes a new instance of the <see cref="ObservableCachedElement{TKey, TValue}" /> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The cached value.</param>
        /// <param name="expiry">The expiry.</param>
        /// <param name="expirationType">Type of the expiration.</param>
        public ObservableCachedElement(TKey key, TValue value, TimeSpan expiry, ObservableCacheExpirationType expirationType)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (expiry < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(expiry), $"{nameof(expiry)} cannot be negative");
            }

            Key   = key;
            Value = value;

            // ToDo: usage of OriginalExpiry / expiry is a dirty hack - this needs to be done more properly sometimes down the road
            OriginalExpiry = expiry;
            ExpirationType = expirationType;
        }
Example #2
0
 /// <summary>
 ///     If the given <paramref name="keyValuePair" />'s key does not exist in the <paramref name="cache" /> yet it will be
 ///     added with the given value.
 /// </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. 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 attempt on.</param>
 /// <returns>
 ///     An observable stream that returns [true] if successful, [false] if not.
 /// </returns>
 public static IObservable <bool> TryAdd <TKey, TValue>(this IObservableCache <TKey, TValue> cache, KeyValuePair <TKey, TValue> keyValuePair, TimeSpan?expiry = null, ObservableCacheExpirationType expirationType = ObservableCacheExpirationType.DoNothing, IScheduler scheduler = null)
 {
     throw new NotImplementedException();
 }
Example #3
0
 /// <summary>
 ///     Gets the value for the given <paramref name="keys" /> or, if they don't exist in the <paramref name="cache" />,
 ///     calls the <paramref name="producer" /> to build the corresponding value, adds it to the <paramref name="cache" />
 ///     and returns it back to the caller.
 /// </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="keys">The keys to get or produce and add.</param>
 /// <param name="producer">
 ///     The producer function that gets handed in the <paramref name="keys" /> in bulk and is expected
 ///     to return a corresponding .
 /// </param>
 /// <param name="expiry">The expiry. If none is provided the <paramref name="keys" /> will virtually never expire.</param>
 /// <param name="expirationType">Defines how the <paramref name="keys" /> shall expire.</param>
 /// <param name="scheduler">The scheduler to run the retrieval or addition on.</param>
 /// <returns>
 ///     An observable stream containing the corresponding <typeparamref name="TValue" /> instance(s).
 /// </returns>
 public static IObservable <TValue> GetOrAdd <TKey, TValue>(this IObservableCache <TKey, TValue> cache, IEnumerable <TKey> keys, Func <IEnumerable <TKey>, IEnumerable <KeyValuePair <TKey, TValue> > > producer, TimeSpan?expiry = null, ObservableCacheExpirationType expirationType = ObservableCacheExpirationType.DoNothing, IScheduler scheduler = null)
 {
     throw new NotImplementedException();
 }
Example #4
0
        /// <summary>
        /// Adds the specified <paramref name="key"/> with the given <paramref name="value"/> to the <paramref name="cache"/>.
        /// </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="expiry">The expiry of the <paramref name="key"/>.</param>
        /// <param name="expirationType">Defines how the <paramref name="key" /> shall expire.</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, TimeSpan expiry, ObservableCacheExpirationType expirationType = ObservableCacheExpirationType.DoNothing, 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), expiry, expirationType, scheduler));
        }
Example #5
0
 /// <summary>
 ///     Adds a range of <paramref name="keyValuePairs" /> or, if the corresponding key already exists, updates it with the
 ///     given value.
 /// </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="keyValuePairs">The key value pairs to add.</param>
 /// <param name="valueSelector">
 ///     The value selector that gets provided the key, the existing value and the new value (in
 ///     that order) and is expected to return which value to use.
 /// </param>
 /// <param name="expiry">The expiry. If none is provided the <paramref name="keyValuePairs" /> will virtually never expire.</param>
 /// <param name="expirationType">Defines how the <paramref name="keyValuePairs" /> shall expire.</param>
 /// <param name="scheduler">The scheduler to run the addition or update on.</param>
 /// <returns>
 ///     An observable stream that, when done, returns an <see cref="Unit" />.
 /// </returns>
 public static IObservable <KeyValuePair <TKey, TValue> > AddOrUpdate <TKey, TValue>(this IObservableCache <TKey, TValue> cache, IEnumerable <KeyValuePair <TKey, TValue> > keyValuePairs, Func <TKey, TValue, TValue, TValue> valueSelector, TimeSpan?expiry = null, ObservableCacheExpirationType expirationType = ObservableCacheExpirationType.DoNothing, IScheduler scheduler = null)
 {
     throw new NotImplementedException();
 }
Example #6
0
        /// <summary>
        /// Adds the specified <paramref name="keyValuePairs" /> to the <see cref="IObservableCache{TKey,TValue}" />.
        /// </summary>
        /// <param name="cache">The cache to use.</param>
        /// <param name="keyValuePairs">The key/value pairs to add.</param>
        /// <param name="expiry">The expiry of the <paramref name="keyValuePairs" />.</param>
        /// <param name="expirationType">Defines how the <paramref name="keyValuePairs" /> shall expire.</param>
        /// <param name="scheduler">Scheduler to perform the addrange action on.</param>
        /// <returns>
        /// An observable stream that returns an <see cref="Unit" /> for every added element of the <paramref name="keyValuePairs"/>.
        /// </returns>
        public static IObservable <KeyValuePair <TKey, TValue> > AddRange <TKey, TValue>(this IObservableCache <TKey, TValue> cache, IEnumerable <KeyValuePair <TKey, TValue> > keyValuePairs, TimeSpan expiry, ObservableCacheExpirationType expirationType = ObservableCacheExpirationType.DoNothing, IScheduler scheduler = null)
        {
            if (cache == null)
            {
                throw new ArgumentNullException(nameof(cache));
            }
            if (keyValuePairs == null)
            {
                throw new ArgumentNullException(nameof(keyValuePairs));
            }

            var keyValuePairsAsList = keyValuePairs.ToList();

            if (keyValuePairsAsList.Count == 0)
            {
                return(scheduler != null
                    ? Observable.Empty <KeyValuePair <TKey, TValue> >(scheduler)
                    : Observable.Empty <KeyValuePair <TKey, TValue> >());
            }

            return(cache.AddRange(keyValuePairsAsList.AsObservable(scheduler), expiry, expirationType, scheduler));
        }
Example #7
0
 /// <summary>
 /// Gets a <see cref="IObservableCacheChange{TKey,TValue}" /> representing a <see cref="ObservableCacheChangeType.ItemExpired" />,
 /// more particularly one for an item replacement inside the <see cref="IObservableCache{TKey,TValue}" />.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="value">The new value.</param>
 /// <param name="expiresAt">The expires <see cref="DateTime" /> the <paramref name="key" /> expires / expired at.</param>
 /// <param name="expirationType">Type of the expiration.</param>
 /// <returns></returns>
 /// <value>
 /// An <see cref="IObservableCacheChange{TKey,TValue}">instance</see> representing an item replacement <see cref="ObservableCacheChangeType.ItemExpired" />.
 /// </value>
 public static IObservableCacheChange <TKey, TValue> ItemExpired(TKey key, TValue value, DateTime expiresAt, ObservableCacheExpirationType expirationType)
 => new ObservableCacheChange <TKey, TValue>(ObservableCacheChangeType.ItemExpired, key, value, expiresAt: expiresAt, expirationType: expirationType);
Example #8
0
 /// <summary>
 /// Gets a <see cref="IObservableCacheChange{TKey,TValue}" /> representing a <see cref="ObservableCacheChangeType.ItemValueReplaced" />,
 /// more particularly one for an item replacement inside the <see cref="IObservableCache{TKey,TValue}" />.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="newValue">The new value.</param>
 /// <param name="replacedOldValue">The replaced old value.</param>
 /// <param name="expiresAt">The expires <see cref="DateTime"/> the <paramref name="key"/> expires / expired at.</param>
 /// <param name="expirationType">Type of the expiration.</param>
 /// <returns></returns>
 /// <value>
 /// An <see cref="IObservableCacheChange{TKey,TValue}">instance</see> representing an item replacement <see cref="ObservableCacheChangeType.ItemValueReplaced" />.
 /// </value>
 public static IObservableCacheChange <TKey, TValue> ItemReplaced(TKey key, TValue newValue, TValue replacedOldValue, DateTime expiresAt, ObservableCacheExpirationType expirationType)
 => new ObservableCacheChange <TKey, TValue>(ObservableCacheChangeType.ItemValueReplaced, key, newValue, replacedOldValue, expiresAt: expiresAt, expirationType: expirationType);
Example #9
0
 /// <summary>
 /// Gets a <see cref="IObservableCacheChange{TKey,TValue}" /> representing a <see cref="ObservableCacheChangeType.ItemValueChanged" />,
 /// more particularly one for an item's property change.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="value">The new value.</param>
 /// <param name="propertyName">Name of the property.</param>
 /// <param name="expiresAt">The expires <see cref="DateTime"/> the <paramref name="key"/> expires / expired at.</param>
 /// <param name="expirationType">Type of the expiration.</param>
 /// <returns></returns>
 /// <value>
 /// An <see cref="IObservableCacheChange{TKey,TValue}">instance</see> representing an item property changed <see cref="ObservableCacheChangeType.ItemValueChanged" />.
 /// </value>
 public static IObservableCacheChange <TKey, TValue> ItemValueChanged(TKey key, TValue value, string propertyName, DateTime expiresAt, ObservableCacheExpirationType expirationType)
 => new ObservableCacheChange <TKey, TValue>(ObservableCacheChangeType.ItemValueChanged, key, value, changedPropertyName: propertyName, expiresAt: expiresAt, expirationType: expirationType);