Learn

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

A cache hit

In the animation, there is a memory hierarchy with a cache in between the processor and main memory. The processor requests the data located at the main memory address 2. The address is found inside the cache so a cache hit occurs. The character "c" will be returned from the cache and the main memory is never accessed.

The goal of the memory hierarchy is to reduce data access time by getting as many cache hits when requesting data from memory.

Instructions

1.

The Cache() class now has entries in self.data. This data will result in a series of cache hits when the simulated instructions are run.

The Cache() class also added a .read() method. Inside the method:

  • super().read() is called. This will call the Memory() class .read() method and will output "Cache read: " as well as add the cache access_time to the total execution time of the simulation.
  • The data variable is initialized to None.

Run the code. The output should have Cache read: NO DATA.

2.

The method .get_entry() has also been added to the Cache() class. It takes a memory address as a parameter and searches for that address in self.data. If it finds the address, CACHE HIT is output and the cache entry is returned as a dictionary. Otherwise, CACHE MISS is output and None is returned.

Inside the .read() method:

  • Define the entry variable and assign it a call to self.get_entry() with address as the parameter.

Run the code. The output should now indicate a cache hit on each read along with the NO DATA error.

3.

Now check the entry for a cache hit.

After the entry variable definition:

  • Write an if statement that checks if entry is not equal to None.
  • Inside the if statement, set data to the value in entry["data"].
4.

The last step is to return the data retrieved from the cache.

Inside the .read() method:

  • Return the data variable.

When you run the code, the output should now show each character retrieved from the cache at the end of each line. The output string should now be complete.

Take this course for free

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