Fix cache upgrade

Please review @dragotin @icewind1991 @schiesbn @karlitschek 
and give it a good test.