// Accesses a data object from the cache. // If the object was reclaimed for garbage collection, // create a new data object at that index location. public MemoryIndexedDocument this[string file] { get { // now look for that file in the cache WeakReference weakRef; if (!_cacheStorage.TryGetValue(file, out weakRef)) { return(null); } // Obtain an instance of a data object from the cache of // of weak reference objects. MemoryIndexedDocument document = weakRef.Target as MemoryIndexedDocument; if (document == null) { // Object was reclaimed, so generate a new one. document = new MemoryIndexedDocument(_documentSource, file); weakRef.Target = document; } else { // Object was obtained with the weak reference. } return(document); } }
public override void AddDocument(string file, bool cacheIt, bool warnOverride) { // load the document MemoryIndexedDocument document = new MemoryIndexedDocument(this, file); // record the keys //string[] keys = document.GetKeys(); ICollection <string> keys = document.GetKeys(); foreach (string key in keys) { if (warnOverride && _keyFileIndex.ContainsKey(key)) { this.WriteMessage(MessageLevel.Warn, String.Format("Entries for the key '{0}' occur in both '{1}' and '{2}'. The last entry will be used.", key, _keyFileIndex[key], file)); } _keyFileIndex[key] = file; } if (cacheIt) { this.CacheDocument(file, document); } document = null; }
private MemoryIndexedDocument GetDocument(string key) { // look up the file corresponding to the key string file; if (_keyFileIndex.TryGetValue(key, out file)) { // now look for that file in the cache MemoryIndexedDocument document = _documentCache[file]; if (document == null) { // not in the cache, so load it document = new MemoryIndexedDocument(this, file); this.CacheDocument(file, document); } return(document); } else { // there is no such key return(null); } }
private void CacheDocument(string file, MemoryIndexedDocument document) { if (_documentCache == null) { return; } // add it to the cache _documentCache.Add(file, document); }
public override XPathNavigator GetContent(string key) { XPathNavigator navigator = null; MemoryIndexedDocument document = GetDocument(key); if (document != null) { navigator = document.GetContent(key); // Remove any strong reference to the document by setting it null document = null; } return(navigator); }
public void Add(string file, MemoryIndexedDocument document) { if (document == null) { return; } // if the cache is full, remove a document if (_cacheStorage.Count >= _cacheSize) { string fileToUnload = _cacheQueue.Dequeue(); _cacheStorage.Remove(fileToUnload); } // add it to the cache _cacheStorage.Add(file, new WeakReference(document, false)); _cacheQueue.Enqueue(file); document = null; }