When the data requested from the processor is not in the cache, a cache miss occurs:
The above animation represents a cache miss. The data request first goes to the cache. When the data is not found in the cache, a cache miss occurs and the request goes to the main memory. The memory address and retrieved data will then be placed in the cache. Finally, the processor will finish the request by retrieving the data from the cache.
While a cache miss helps put the needed data in the cache, the goal of the cache is to limit the cache misses.
Observe the following changes to the
self.datahas no entries with all tags equal to
Noneand data equal to empty strings.
self.main_memoryvariable has been added and holds an instance of the
Main_Memory()class. This will be used to read data stored in the main memory.
.add_entry()method has been added and is used to add an entry to the cache given the memory address and data.
Run the code. The cache with no entries causes all cache misses. The output indicates
NO DATA because the cache does not retrieve anything from the main memory yet.
Now it’s time to add the cache miss behavior. Inside the
- Add an
elseclause at the end of the
- Set the
datavariable to a call to
addressas the argument.
The output now indicates a read from the main memory and the execution time should be increased from the cache hits in the last exercise.
Now add an entry to the cache with the retrieved data. Inside the
Running the code will start with cache misses, but result in cache hits once the data is found in the cache.