/// <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; }
/// <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; }
/// <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); }
/// <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) { }
private void AddValueLoader(IValueLoader loader) { m_valueLoaders.Add(loader.AppliesTo, loader); loader.GameLoader = this; }
public ValueHolder(IValueLoader <T> loader) => _loader = loader;