Only cache the keys from the query.
Fixed local variable conflict.
Fixed a wrong if statement.