Fix fragment caching in mixed encoding scenario

Fixes the case where a fragment cache might get persisted with a few extra bytes of garbage due to `String#length` meaning different things in different encodings.