@@ -44,32 +44,38 @@ public function handle($request, Closure $next, $cache_lifetime, $cache_region =
4444 $ regionTag = CacheRegions::getCacheRegionFor ($ cache_region , $ id );
4545 }
4646 }
47-
47+ $ status = 200 ;
4848 if ($ regionTag ) {
4949 Log::debug ("CacheMiddleware: using region tag {$ regionTag }" );
5050 $ data = Cache::tags ($ regionTag )
51- ->remember ($ key , $ cache_lifetime , function () use ($ next , $ request , $ regionTag , $ key , $ cache_lifetime ) {
51+ ->remember ($ key , $ cache_lifetime , function () use ($ next , $ request , $ regionTag , $ key , $ cache_lifetime, & $ status ) {
5252 Log::debug ("CacheMiddleware: cache miss for {$ key } in tag {$ regionTag }" );
5353 $ resp = $ next ($ request );
54- if ($ resp instanceof JsonResponse && $ resp ->getStatusCode () === 200 ) {
55- return $ resp ->getData (true );
54+ if ($ resp instanceof JsonResponse) {
55+ $ status = $ resp ->getStatusCode ();
56+ Log::debug ("CacheMiddleware: cache miss status {$ status }" );
57+ if ($ status === 200 )
58+ return $ resp ->getData (true );
5659 }
5760 // don’t cache non-200 or non-JSON
5861 return Cache::get ($ key );
5962 });
6063 } else {
61- $ data = Cache::remember ($ key , $ cache_lifetime , function () use ($ next , $ request , $ key ) {
64+ $ data = Cache::remember ($ key , $ cache_lifetime , function () use ($ next , $ request , $ key, & $ status ) {
6265 Log::debug ("CacheMiddleware: cache miss for {$ key }" );
6366 $ resp = $ next ($ request );
64- if ($ resp instanceof JsonResponse && $ resp ->getStatusCode () === 200 ) {
65- return $ resp ->getData (true );
67+ if ($ resp instanceof JsonResponse) {
68+ $ status = $ resp ->getStatusCode ();
69+ Log::debug ("CacheMiddleware: cache miss status {$ status }" );
70+ if ($ status === 200 )
71+ return $ resp ->getData (true );
6672 }
6773 return Cache::get ($ key );
6874 });
6975 }
7076
7177 // Build the JsonResponse (either from cache or fresh)
72- $ response = new JsonResponse ($ data , 200 , ['Content-Type ' => 'application/json ' ]);
78+ $ response = new JsonResponse ($ data , $ status , ['Content-Type ' => 'application/json ' ]);
7379
7480 // Mark for revalidation so your ETag middleware can return 304 when unchanged
7581 $ response ->setPublic ();
0 commit comments