Want to cache upstream response based on upstream response status code

I am trying to cache upstream response when upstream response with 200 and 401, not caching other response status code.
Also want to make sure caching zone is separate for both cache.

With my below configuration caching is not happening.

Configuration which I tried :
upstream.conf :
proxy_cache_path /apps_01/apigw/tmp/nginx_200_cache_stage levels=1:2 inactive=15m keys_zone=200_cache_stage:10m;
proxy_cache_path /apps_01/apigw/tmp/nginx_401_cache_stage levels=1:2 inactive=15m keys_zone=401_cache_stage:10m;

map $upstream_http_status $cache_zone {
200 200_cache_stage;
401 401_cache_stage;
}

route.conf :
proxy_pass_request_headers off;
proxy_cache $cache_zone;
proxy_cache_valid 200 15m;
proxy_cache_valid 401 15m;
proxy_cache_methods POST;
proxy_cache_key $request_body;

Can anyone help me with this ?

Also want to make sure caching zone is separate for both cache.

it is impossible. because cache path must be set before request to upstream. so you can not set cache path according to the upstream status code.

The proxy_cache directive in your configuration will receive an empty value and thus will not be cached.