CakePHP 3: Use Middleware to support REDIS Cache

2019-05-19 14:31:37.278819+00

This article describes how content can be written from Cakephp Middleware to REDIS in order to enable caching.

We assume that REDIS is already installed.

Extend CakePHP with its Middleware

Create a file ../src/Middleware/CacheMiddleware.php

body()) === 0) return;
 
      // do not cache certain URLs
      if (substr($request->url,0,5) === 'admin') return;
 
      error_log('CacheMiddleware called, wrote key:' . md5('prefix/'. $request->url));

      Cache::write(md5('prefix/'. $request->url), $response->body() , 'default');
        
      return $response;
   }
}
?>

Tag: cakephp3