Example #1
0
        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);
        }
Example #2
0
        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);
        }
Example #3
0
        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);
        }
Example #4
0
        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);
        }
Example #5
0
        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);
        }
Example #6
0
        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);
        }
Example #7
0
        public bool IsCached(object[] args)
        {
            string hash = GetHash(args);

            return(LastIndexCache.ContainsKey(hash));
        }