public override CacheEntry Get(object[] args) { string hash = GetHash(args); Debug.WriteLine($"Getting hash: {hash}"); if (!LastIndexCache.ContainsKey(hash)) { return(null); } int lastIndex = LastIndexCache[hash]; var entry = ValueLists[lastIndex]; if (lastIndex > 0) { MoveToFrontOfCache(entry); } else { Debug.WriteLine($"Hash was already the most recently used. Will not move: {entry.Hash}"); Debug.WriteLine( $"Current state:{Environment.NewLine}" + $"Indices: {JsonConvert.SerializeObject(LastIndexCache)}{Environment.NewLine}" + $"Cache: {JsonConvert.SerializeObject(ValueLists)}" ); } Debug.WriteLine($"Returning existing entry: {entry.Hash}"); return(entry); }
private void RemoveLeastRecentlyUsedEntry() { var leastRecentlyUsedEntry = ValueLists[MaxSize - 1]; Debug.WriteLine($"Removing least recently used entry: {leastRecentlyUsedEntry.Hash}"); ValueLists.RemoveAt(MaxSize - 1); LastIndexCache.Remove(leastRecentlyUsedEntry.Hash); }
public override bool Add(object[] args, object result) { string hash = GetHash(args); if (LastIndexCache.ContainsKey(hash)) { return(false); } Debug.WriteLine($"Adding args to front of cache: {JsonConvert.SerializeObject(args)}: {hash}"); AddToFrontOfCache(hash, result); return(true); }
protected void AddToFrontOfCache(string hash, object value) { var newEntry = new CacheEntry() { Hash = hash, Value = value }; Debug.WriteLine($"Adding new entry to front of cache: {hash}"); LastIndexCache.Add(hash, 0); ValueLists.Insert(0, newEntry); UpdateIndices(hash); }
public override CacheEntry Get(object[] args) { string hash = GetHash(args); Debug.WriteLine($"Getting hash: {hash}"); if (!LastIndexCache.ContainsKey(hash)) { return(null); } int lastIndex = LastIndexCache[hash]; var entry = ValueLists[lastIndex]; Debug.WriteLine($"Returning existing entry: {entry.Hash}"); return(entry); }
public override bool Add(object[] args, object result) { string hash = GetHash(args); if (LastIndexCache.ContainsKey(hash)) { return(false); } if (ValueLists.Count >= MaxSize) { RemoveLeastRecentlyUsedEntry(); } Debug.WriteLine($"Adding args to front of cache: {JsonConvert.SerializeObject(args)}: {hash}"); AddToFrontOfCache(hash, result); return(true); }
public bool IsCached(object[] args) { string hash = GetHash(args); return(LastIndexCache.ContainsKey(hash)); }