Normally I would say a cache is invalid if the values it represent could have changed in the time between creating the cache value and using it.
Maybe the remark is not valid which can be because I didn't understand immediately how this program works.
As I read it now, it looks like the cache is re-created anew by the initialisation in line 7.
Remains my new question why the calc() function returns a value as soon as it has found something. Why the nested for loops if the result is either a "continue" or a "return value"?