Invalidate cache if new width != old width

Also, improve the debug log so that it doesn't complain about an invalid
cache not being equal to the correct value, and pull the debug logger
into a function.