There is more than one way to do that.
You could do it with the help of iptables. Here’s a guide (it is for debian but as it uses kernel’s iptables I think it should work for any linux distribution).
How To Set Up A Linux Proxy Server
And, you could use squid or any other proxy server program with caching capabilities.