예제 #1
0
        /// <summary>Getter for index</summary>
        /// <param name="key">key to find (or load if needed)</param>
        /// <param name="createItem">
        /// An optional factory method for creating the item if it does not exist in the cache.
        /// </param>
        /// <returns>the object value associated with key, or null if not found or could not be loaded</returns>
        public async Task <T> GetItem(TKey key, ItemCreator <TKey, T> createItem = null)
        {
            INode <T> node = FindExistingNodeByKey(key);

            node?.Touch();

            ItemCreator <TKey, T> creator = createItem ?? loadItem;

            if ((node?.Value == null) && (creator != null))
            {
                Task <T> task = creator(key);
                if (task == null)
                {
                    throw new ArgumentNullException(nameof(createItem),
                                                    "Expected a non-null Task. Did you intend to return a null-returning Task instead?");
                }

                T value = await task;

                lock (this)
                {
                    node = FindExistingNodeByKey(key);
                    if (node?.Value == null)
                    {
                        node = owner.AddAsNode(value);
                    }

                    lifespanManager.CheckValidity();
                }
            }

            return(node?.Value);
        }
예제 #2
0
        /// <summary>Getter for index</summary>
        /// <param name="key">key to find (or load if needed)</param>
        /// <param name="createItem">
        /// An optional factory method for creating the item if it does not exist in the cache.
        /// </param>
        /// <returns>the object value associated with key, or null if not found or could not be loaded</returns>
        public async Task <T> GetItem(TKey key, ItemCreator <TKey, T> createItem = null)
        {
            INode <T> node = FindExistingNodeByKey(key);

            node?.Touch();

            ItemCreator <TKey, T> creator = createItem ?? loadItem;

            if ((node?.Value == null) && (creator != null))
            {
                T value = await creator(key);

                lock (this)
                {
                    node = FindExistingNodeByKey(key);
                    if (node?.Value == null)
                    {
                        node = owner.AddAsNode(value);
                    }

                    lifespanManager.CheckValidity();
                }
            }

            return(node?.Value);
        }
예제 #3
0
        /// <summary>Getter for index</summary>
        /// <param name="key">key to find (or load if needed)</param>
        /// <returns>the object value associated with key, or null if not found & could not be loaded</returns>
        public async Task <T> GetItem(TKey key, ItemLoader <TKey, T> loadItem = null)
        {
            INode <T> node = FindExistingNodeByKey(key);

            node?.Touch();

            lifespanManager.CheckValidity();

            ItemLoader <TKey, T> loader = loadItem ?? this.loadItem;

            if ((node?.Value == null) && (loader != null))
            {
                node = owner.AddAsNode(await loader(key));
            }

            return(node?.Value);
        }