Introduction :-

                                 Cache Memory is a special very high-speed memory. It is used to speed up and synchronizing with high-speed CPU. It acts as a buffer between RAM and the CPU.  It holds frequently requested data and instructions so that they are immediately available to the CPU when needed.

Why Cache?

- Cache memory is used to reduce the average time to access data from the Main memory as it is a smaller and faster memory which stores copies of the data from frequently used main memory locations.


When CPU attempts to read a word from main memory, check is made to determine if the word is in cache. It so, then word is delivered form cache. If word is not there in cache, then a block of main memory consisting some word along with that word, is read into cache and the required word is delivered to CPU. This is called “Principle of Locality of Reference”.

Locality of Reference:-

Since size of cache memory is less as compared to main memory. So to check which part of main memory should be given priority and loaded in cache is decided based on locality of  reference.


-> Types of Locality of reference

i) Spatial Locality of reference
ii) Temporal Locality of reference


Temporal Locality of reference

Temporal locality means current data or instruction that is being fetched may be needed soon. So we should store that data or instruction in the cache memory so that we can avoid again searching in main memory for the same data. When CPU accesses the current main memory location for reading required data or instruction, it also get stored in cache memory which is based on the fact that same data or instruction may be needed in near future. This is known as temporal locality.

Spatial Locality of reference

Spatial locality means instruction or data near to the current memory location that is being fetched, may be needed soon in near future. This is slightly different from temporal locality. Here we are taking about nearly located memory locations while in temporal locality we were taking about the actual memory location that were being fetched.

Cache Performance:

The performance of the cache is measured in terms of hit ratio. 
When CPU refers to memory and find the data or instruction within the Cache Memory, it is known as cache hit. 
If the desired data or instruction is not found in cache memory and CPU refers to the main memory to find that data or instruction, it is known as cache miss.
(Hit + Miss) = Total CPU Reference
Hit Ratio(h) = Hit / (Hit+Miss)