Fixing bug in query caching