示例#1
0
        /// <summary>
        /// Retrieves the value for key requested. If the key does not exist
        /// in the cache then the Fetch method is used to retrieve the value
        /// from another source.
        /// </summary>
        /// <param name="key">Key for the item required</param>
        /// <param name="loader">Loader to fetch the item from if not in the cache</param>
        /// <returns>An instance of the value associated with the key</returns>
        public V this[K key, IValueLoader <K, V> loader]
        {
            get
            {
                bool added = false;
                Interlocked.Increment(ref _requests);
                CachedItem node, newNode = null;
                if (_dictionary.TryGetValue(key, out node) == false)
                {
                    // Get the item fresh from the loader before trying
                    // to write the item to the cache.
                    Interlocked.Increment(ref _misses);
                    newNode = new CachedItem(
                        GetRandomLinkedList(),
                        key,
                        loader.Load(key));

                    // If the node has already been added to the dictionary
                    // then get it, otherwise add the one just fetched.
                    node = _dictionary.GetOrAdd(key, newNode);

                    // If the node got from the dictionary is the new one
                    // just fetched then it needs to be added to the linked
                    // list.
                    if (node == newNode)
                    {
                        // Set the node as the first item in the list.
                        newNode.List.AddNew(newNode);
                        added = true;
                    }
                }

                // The item is in the dictionary. Check it's still in the list
                // and if so them move it to the head of the linked list.
                if (added == false)
                {
                    node.List.MoveFirst(node);
                }
                return(node.Value);
            }
        }
 public ValueHolder(IValueLoader <T> loader)
 {
     Loader = loader;
 }
示例#3
0
 /// <summary>
 /// Set the value loader that will be used to load items on a cache miss
 /// </summary>
 /// <param name="loader"></param>
 public void SetValueLoader(IValueLoader <K, V> loader)
 {
     _loader = loader;
 }
示例#4
0
 /// <summary>
 /// Constructs a new instance of the cache.
 /// </summary>
 /// <param name="cacheSize">The number of items to store in the cache</param>
 /// <param name="loader">Loader used to fetch items not in the cache</param>
 /// <param name="concurrency">
 /// The expected number of concurrent requests to the cache
 /// </param>
 internal LruCache(int cacheSize, IValueLoader <K, V> loader, int concurrency)
     : this(cacheSize, concurrency)
 {
     SetValueLoader(loader);
 }
示例#5
0
 /// <summary>
 /// Constructs a new instance of the cache.
 /// </summary>
 /// <param name="cacheSize">The number of items to store in the cache</param>
 /// <param name="loader">Loader used to fetch items not in the cache</param>
 internal LruCache(int cacheSize, IValueLoader <K, V> loader)
     : this(cacheSize, loader, Environment.ProcessorCount)
 {
 }
示例#6
0
 private void AddValueLoader(IValueLoader loader)
 {
     m_valueLoaders.Add(loader.AppliesTo, loader);
     loader.GameLoader = this;
 }
示例#7
0
 public ValueHolder(IValueLoader <T> loader) => _loader = loader;
示例#8
0
 private void AddValueLoader(IValueLoader loader)
 {
     m_valueLoaders.Add(loader.AppliesTo, loader);
     loader.GameLoader = this;
 }