Add model cache support
Add tests, and some issues fixed