Really rough implementation of caching

Certainly breaks some stuff, esp SQL backend, but it's something.