Improved template caching implementation.