This ensure that we do not run the same script twice with the same set of flags. because we check script when accepting a transaction in the mempool, this shoudl ensure that we do not run the script when connecting a new block.
This is based on core's PR10192 , but the structure is somewhat different (the cache is moved in its own file for instance).