Only call ->clearInstanceCache() if $targetUser instanceof User

Cherry picked from master.

Bug: 61252
Change-Id: Ibb79ca9a27e50b58fdd5dd541e243054fb095753