Look at util_memoize.
It is used to cache the result of a tcl procedure in many places in the toolkit. So if you have a tcl procedure that rewrites the page content in gatekeepr and returns a string with the content, you can util_memoize that call.
There is an acs-kernel parameter setting for the size of the cache. It removes old data when the limit is reached. You'll need to do testing to see what a good size is for your application.