cache refactoring: Move PyGI direction code into new function

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