Computer cache - data removing I am programming CPU cache simulator and I am supposed to implement removing of entries. I will not use LRU but just random.

I am not really clear, when should I call the removing function? When cache (2 or 4 caches) are full and CPU wants to write into cache where all (data and tag) of specific indexes are same? Are there some other possible situations, or maybe I am completely wrong?

Hope you understand my question, if no, please tell me and I will edit my question. Thank you in advance.
Cache lines are evicted :


When the OS requests it, it may occur for example in non cache-coherent systems when a peripheral does a DMA transfer (direct transfer from a peripheral to main memory), or if the CPU is shut down to save its state to RAM...
When all the ways of the cache are already used for the requested line.


In a write-through cache, new data can be directly loaded, overwriting previous contents. In a write-back cache, if the cache line was modified, it must be written back before loading new data.

There could be other exceptional cases, for example if a parity error is detected and new data must be loaded...
But, generally, caches are filled but never emptied.