There are a lot of considerations to be made when developing software in regards to cache utilization. So much so that all I can do is point at this article.
https://software.intel.com/en-us/articles/software-techniques-for-shared-cache-multi-core-systems
As far as cache vs ram, a CPU cache simply stores frequently used data on the CPU which will allow quicker access to it than if it had to travel from the RAM. Check out the ars technica artical page 2 for a table on latency times. It's a great read.