Add flag for enabling/disabling expression caching.

This also checks the global optimization flag.

Additionally, reduce some logging and catch import errors
when serializing.