Optimize querying selected history entry

Previously it was called 3 times with always the same result, so we can
just cache it.