Using Redis Cache for CakePHP 3.6

2019-05-26 05:22:24+00

The objective is to cache html-pages using Redis in order to improve performance.

Here's the flow: - Cakephp writes to Cache - Nginx reads from Cache, if not found pass to CakePHP for generation.

Measure Performance before using the Cache

Let's use CURL to measure the website performance before using the Cache:

curl -L -w "%{time_total}\n" -o /dev/null -s "http://www.locavores.co"

Repeat this several time to get a good average value of the reasonable result. Please note that the parameter -L indicates that CURL must follow any redirects.

0.413333

Configure the Redis Cache in CakePHP

'Cache' => [ 'default' => [ 'className' => 'Redis', 'path' => CACHE, ],

Write data to Redis cache in CakePHP MIddleware

In the Middleware component we will write data to the cache. In former Versions of cakephp (< 3.6) this is being implemented by filters.

connect('127.0.0.1', '6379',0);
    $return = $redis->select(0);
    $rc = $redis->set(md5('mx_' . $request->getRequestTarget()), $response->body());

    return $response;
  }
}
?>

Tag: cakephp3