[BUGFIX] Fix fatal errors related to templateCache (Revision 5)

Fixes more fatal errors that occur at times, similar to the fix in
I9bf0c8eafc8ab70215b35153f428839a6721469e.

Change-Id: I62300f8f86a397facaa2b68c40ea0c22fac62a75
Releases: master, 2.2, 2.1
Revision-Id: 1567d18b784cbde2abb5e1784230b76dab64baa1