Skip to main content

Command Palette

Search for a command to run...

Caching

Updated
2 min read

Caching is the process of keeping frequently accessed data, instructions, or files in a memory closer to the CPU for faster access and faster execution of processes. If we have to read the data from secondary memory or disk, it takes more time since it is much farther from the CPU. Caching is based on the principle of locality, which includes temporal locality and spatial locality.

Temporal locality refers to the fact that the CPU tends to access the same data repeatedly within short intervals of time. Spatial locality refers to the observation that the CPU tends to access data that is close to the data it previously accessed.

Cache Hit and Cache Miss

A cache hit happens when the required data is found in the cache itself and can be accessed directly. A cache miss, on the other hand, happens when the required data is not available in the cache, and the OS has to bring that data block from secondary memory into the cache so that the CPU can access it.

During any data access, the system first checks for the data in the cache. If found, it is called a cache hit and the data is accessed immediately. If the data is not found, it results in a cache miss, and the system fetches the data from secondary memory. Using cache mapping techniques, the fetched data is copied into the cache for future access. If the cache is full, cache replacement techniques are used to decide which data to remove and which to keep.

Cache Mapping Techniques involves Direct Mapping, Associative Mapping and Set Associative Mapping. Cache Replacement Techniques involves LFU, FIFO, MFU, LRU and Random.