コード例 #1
0
ファイル: LazyDatabase.cs プロジェクト: phoenix-apps/Phnx
        /// <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
ファイル: LazyDatabase.cs プロジェクト: phoenix-apps/Phnx
        /// <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
ファイル: LazyDatabase.cs プロジェクト: phoenix-apps/Phnx
 private bool TryGetTable <T>(out LazyDictionary <object, object> table)
 {
     return(_cache.TryGetValue(typeof(T), out table));
 }