Cache opt

Try to address some comments from P.L.
This is the initial pull request for Theano issue #2203.
Mark this as TODO. We need to fix this before we start to use it.