[backport] Extract local cache middleware

backport #14171 to 4.1

review @guilleiguaran