The issue isn't with the cache
Use a foreach instead of while