Fix compatibility with caching middleware

Fixes #405
Merge branch 'fix-marshalling-for-cache'