cache refactoring: Move variable declarations to blocks where they are used

https://bugzilla.gnome.org/show_bug.cgi?id=640812