Proactive caching will enable the 5G networks of the future to meet the challenge of continuously increasing wireless data traffic. Thanks to proactive caching, we are observing a shift from standard cellular networks, with one base station providing wireless connectivity to all the users in the cell, to heterogeneous networks, where several small base stations can assist the macro base station. Going one step further, users can also share their local content via device-to-device communications, avoiding multiple requests to the base station. In this complex network scenario, we propose a proactive caching policy to exploit all these communication opportunities and reduce congestion on the backhaul link, with the goal of minimizing the syst...