Optimal collaborative caching: theory and applications