Don't cache classes unless the environment permits it

Fixes #264.