2017 © Pedro Peláez
 

cakephp-plugin cakephp-cache

CakePHP render caching

image

awallef/cakephp-cache

CakePHP render caching

  • Thursday, August 31, 2017
  • by awallef
  • Repository
  • 2 Watchers
  • 0 Stars
  • 44 Installations
  • PHP
  • 0 Dependents
  • 1 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 0 % Grown

The README.md

cakephp-cache plugin for CakePHP

This plugin allows you set rules in order to cache Cake's responses using your favourite cache engine/settings from app.php. Then Use Ngnix or Apache modules to serve Pre rendered responses. Tata!, (*1)

Installation

You can install this plugin into your CakePHP application using composer., (*2)

The recommended way to install composer packages is:, (*3)

composer require awallef/cakephp-cache:dev-master

Load it in your config/boostrap.php, (*4)

Plugin::load('Awallef/Cache');

How it works

A middleware creates cache response as you want using app.php cache engines. Then you have two options to retrieve:, (*5)

  • A component retrieve the cached response before your action is called, but after auth and whatever you need.
  • You retrieve the cache response server side with Nginx or any other binary

Cache Settings

With a config file you can tell your app when to cache and when to clear cache. In config folder create a cache.php file with as exemple:, (*6)

<?php
return [
    'Awallef.cache.settings' => [
        'default' => 'default', // default cache config to use if not set in rules...
    ],
    'Awallef.cache.rules' => [

        // cache request
        [
          'cache' => 'html', // default: 'default', can be a fct($request)
          'skip' => false, // default: false, can be a fct($request)
          'clear' => false, // default: false, can be a fct($request)
          'compress' => true, // default: false, can be a fct($request)
          //'key' => 'whatEver',// default is fct($request) => return $request->here()
          'method' => ['GET'],
          'code' => '200', // must be set or '*' !!!!!
          'prefix' => '*',
          'plugin' => '*',
          'controller' => '*',
          'action' => '*',
          'extension' => '*'
        ],

        // clear request
        [
          'cache' => 'html', // default: 'default'
          'skip' => false, // default: false
          'clear' => true, // default: false,
          'key' => '*', // * => Cache::clear(false, cache) (Will clear all keys), 'whatEver' => Cache::delete('whatEver', cache), null => Cache::delete($request->here(), cache)
          'method' => ['POST','PUT','DELETE'],
          'code' => ['200','201','202','302'], // 302 is often triggered by cakephp in case of success crud operation...
          'prefix' => '*',
          'plugin' => '*',
          'controller' => ['Users','Pages'],
          'action' => '*',
          'extension' => '*'
        ],
    ]
];

Cache as your last middleware

in your src/Application.php file add the middleware as last chain block. This will create or delete view renders as cache ( html/json /etc...), (*7)

<?php
namespace App;
...
use Awallef\Cache\Middleware\ResponseCacheMiddleware;

class Application extends BaseApplication
{
    public function middleware($middleware)
    {
        $middleware
            ...
            // Apply Response caching
            ->add(ResponseCacheMiddleware::class);

        return $middleware;
    }
}

Retrieve cache via ActionCacheComponent

in your AppController load the component AFTER Auth!!, (*8)

$this->loadComponent('Awallef/Cache.ActionCache');
// normal settings

// OR

$this->loadComponent('Awallef/Cache.ActionCache',['skip_debug' => false]);
// skip_debug default is true
// so here it means cache is deliver even if you are in debug mode....

Retrieve cache via Nginx

First install nginx redis extension. Then set your cache config to store in redis. You can use my plugin..., (*9)

composer require awallef/cakephp-redis

Configure the engine in app.php like follow:, (*10)

'Cache' => [
    ...
    'redis' => [
        'className' => 'Awallef/Redis.Redis',
        'prefix' => 'hello.com:',
        'duration' => '+24 hours',
        'serialize' => true
    ],
    ...
]

Configure cache.php like follow:, (*11)

return [
        'Awallef.cache.settings' => [
            'default' => 'redis', // default cache config to use if not set in rules...
        ],
    'Awallef.cache.rules' => [

        // cache request
        [
            'skip' => false, // default: false, can be a fct($request)
            'clear' => false, // default: false, can be a fct($request)
            'compress' => true, // default: true, can be a fct($request)
            //'key' => 'whatEver',// default is fct($request) => return $request->here()
            'method' => ['GET'],
            'code' => '200', // must be set or '*' !!!!!
            'prefix' => '*',
            'plugin' => '*',
            'controller' => '*',
            'action' => '*',
            'extension' => '*'
        ],
    ]
];

Configure Nginx too:, (*12)

map $http_accept $hello_com_response_header {
    default   "text/html; charset=UTF-8";
    "~*json"  "application/json; charset=UTF-8";
}
server {
    listen 443;
    server_name hello.com;

    ssl on;
    ...

    # redis key
    set $redis_key  "hello.com:$request_uri";
    if ($args) {
        set $redis_key  "hello.com:$request_uri?$args";
    }

    location / {
        redis_pass 127.0.0.1:6379;
        error_page 404 405 502 504 = @fallback;
        more_set_headers "Content-Type: $hello_com_response_header";
    }

    #default cake handling
    location @fallback {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        fastcgi_index   index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

}

Tip

You've better to use different cache settings for each server side retrieve and action cache component retrieve... Otherwise Nginx/Server will serve action cache without checking any of your ACL rules!, (*13)

more

upgrades coming, (*14)

The Versions

31/08 2017

dev-master

9999999-dev

CakePHP render caching

  Sources   Download

The Requires

 

The Development Requires

15/08 2017

3.4.1.3

3.4.1.3

CakePHP render caching

  Sources   Download

The Requires

 

The Development Requires

15/08 2017

3.4.1.2

3.4.1.2

CakePHP render caching

  Sources   Download

The Requires

 

The Development Requires

15/08 2017

3.4.1.1

3.4.1.1

CakePHP render caching

  Sources   Download

The Requires

 

The Development Requires

15/08 2017

3.4.1.0

3.4.1.0

CakePHP render caching

  Sources   Download

The Requires

 

The Development Requires

22/06 2017

3.4.0.4

3.4.0.4

CakePHP render caching

  Sources   Download

The Requires

 

The Development Requires

07/06 2017

3.4.0.3

3.4.0.3

CakePHP render caching

  Sources   Download

The Requires

 

The Development Requires

07/06 2017

3.4.0.2

3.4.0.2

CakePHP render caching

  Sources   Download

The Requires

 

The Development Requires

07/06 2017

3.4.0.1

3.4.0.1

CakePHP render caching

  Sources   Download

The Requires

 

The Development Requires

07/06 2017

3.4.0.0

3.4.0.0

CakePHP render caching

  Sources   Download

The Requires

 

The Development Requires