Learn

When the data requested from the processor is not in the cache, a cache miss occurs:

A cache miss

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.

Instructions

1.

Observe the following changes to the Cache() class:

  • self.data has no entries with all tags equal to None and data equal to empty strings.
  • The self.main_memory variable has been added and holds an instance of the Main_Memory() class. This will be used to read data stored in the main memory.
  • The .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.

2.

Now it’s time to add the cache miss behavior. Inside the .read() method:

  • Add an else clause at the end of the if statement.
  • Set the data variable to a call to self.main_memory.read() with address as 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.

3.

Now add an entry to the cache with the retrieved data. Inside the else clause:

  • Call self.add_entry() with address and data as arguments.

Running the code will start with cache misses, but result in cache hits once the data is found in the cache.

Take this course for free

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?