Fix query cache on first/last calls.

[ fix #3514 ]