There are frameworks that work like this, iirc. I think some in Scala, but I'm not sure.
IMO, the added complexity wouldn't be worth it with things like Varnish. I think there are better solutions and this is attacking the wrong problem.
But, YRMV. It depends on the situation at hand always.