3.0 bake countercache

Implement countercache detection and code generation in bake. Implements #1908