コード例 #1
0
        /**
         * Get method, which returns a TValue with an option to remove the entry after access.
         * If WriteBack is used, then missing entries are recovered from memory.
         * Throws an EntryNotFoundException for missing entries when Write-Through is used.
         **/
        private TValue Get(TKey key, bool isRemove)
        {
            //Calculate hash and set of key.
            RefreshIndexes(key);
            TValue returnValue = default(TValue);

            //Check if the key exists in the cache.
            entryIndex = SearchIfExists(key);
            //If the entry does not exist in the cache, either check the memory or throw an exception.
            if (entryIndex == -1)
            {
                if (isWriteBack)
                {
                    returnValue = memoryAccess.Get(key);
                    Put(key, returnValue);
                    return(returnValue);
                }
                else
                {
                    throw new EntryNotFoundException();
                }
            }

            /**
             * If the entry exists in the cache, refresh its access history and set the return value to the entry value.
             * If the remove option is specified, then remove the entry from the cache (and memory too, if Write-Back is used).
             **/
            else
            {
                if (accessList[set].Contains(hashCode))
                {
                    accessList[set].Remove(hashCode);
                }
                accessList[set].Add(hashCode);
                returnValue = values[entryIndex];
                if (isRemove)
                {
                    keys[entryIndex]   = default(TKey);
                    values[entryIndex] = default(TValue);
                }
            }
            if (isRemove && isWriteBack)
            {
                memoryAccess.Remove(key);
            }
            return(returnValue);
        }