Пример #1
0
        /// <summary>
        /// Add a lazy loading table to the dictionary if it is not already loaded
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="getFromExternalSource"></param>
        /// <param name="overrideLifetime">The new lifetime for objects in this table only. This overrides <see cref="EntriesDefaultMaximumLifetime"/>. If this is <see langword="null"/>, this table's entries are set to never expire</param>
        /// <returns>Whether the table was added to the cache database</returns>
        public bool TryAddTable <TKey, TValue>(Func <TKey, TValue> getFromExternalSource, TimeSpan?overrideLifetime)
        {
            LazyDictionary <object, object> newTable;

            if (overrideLifetime.HasValue)
            {
                newTable = new LazyDictionary <object, object>(key => getFromExternalSource((TKey)key), overrideLifetime.Value);
            }
            else
            {
                newTable = new LazyDictionary <object, object>(key => getFromExternalSource((TKey)key));
            }

            return(_cache.TryAdd(typeof(TValue), newTable));
        }
Пример #2
0
        /// <summary>
        /// Add a lazy loading table to the dictionary if it is not already loaded
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="getFromExternalSource"></param>
        /// <returns>Whether the table was added to the cache database</returns>
        public bool TryAddTable <TKey, TValue>(Func <TKey, TValue> getFromExternalSource)
        {
            if (getFromExternalSource is null)
            {
                throw new ArgumentNullException(nameof(getFromExternalSource));
            }

            LazyDictionary <object, object> newTable;

            if (EntriesHaveDefaultMaximumLifetime)
            {
                newTable = new LazyDictionary <object, object>(key => getFromExternalSource((TKey)key), EntriesDefaultMaximumLifetime.Value);
            }
            else
            {
                newTable = new LazyDictionary <object, object>(key => getFromExternalSource((TKey)key));
            }

            return(_cache.TryAdd(typeof(TValue), newTable));
        }
Пример #3
0
 private bool TryGetTable <T>(out LazyDictionary <object, object> table)
 {
     return(_cache.TryGetValue(typeof(T), out table));
 }