The best practice that I find is delegating the caching function outside of Java, if possible. Java may be good at managing memory, but you should use something more than a simple LRU cache in a dedicated cache system.
There is a lot of cost with GC when it enters.
EHCache is one of the most popular that I know of. The Java Caching System from another answer is also good.
However, I usually unload this work into a basic function (usually this is the JPA save level by the application server, I let it be processed there, so I do not need to deal with it at the application level).
If you cache other data such as web requests, http://hc.apache.org/httpclient-3.x/ is also another good candidate.
However, remember that you also have a “file system”, there is absolutely nothing wrong with writing the file system data that you received. I used this technique several times to fix errors due to misuse of ByteArrayOutputStreams
source share