How to approach caching in ZF2

I am just starting to delve into caching in general. I have a simple indexAction () that retrieves all the data in a dataset. My approach:

  • check existing key 'controllername-index-index'
  • if exists: return key value
  • if not , do the normal action and add the key

The value inside the key must be a ViewModel, which will be generated and populated with my data.

Here is what I have done so far:

<?php public function indexAction() { $sl = $this->getServiceLocator(); // $cache = $sl->get('cache'); // $key = 'kennzahlen-index-index'; // // if ($cache->hasItem($key)) { // return $cache->getItem($key); // } $viewModel = new ViewModel(); $viewModel->setTemplate('kennzahlen/index/index'); $entityService = $sl->get('kennzahlen_referenzwert_service'); $viewModel->setVariable('entities', $entityService->findAll()); // $cache->setItem($key, $viewModel); return $viewModel; } 

The caching elements are commented out for testing purposes, but basically that's all I do. The configuration / caching service is as follows:

 <?php 'cache' => function () { return \Zend\Cache\StorageFactory::factory(array( 'adapter' => array( 'name' => 'filesystem', 'options' => array( 'cache_dir' => __DIR__ . '/../../data/cache', 'ttl' => 100 ), ), 'plugins' => array( array( 'name' => 'serializer', 'options' => array( ) ) ) )); }, 

Serialization and caching work quite well, but I'm surprised at the missing results. Turning to what ZendDevelopersToolbar tells me, the WITHOUT caching time varies from 1.8s to 2.5s. The presence of incomplete parts of the caching (enabled) does not actually improve the loading time of my page.

So my question is: is this approach completely wrong? Are there different, faster parts that can be saved with neat configuration tricks?

I feel that the loading time of 2 seconds per page is DEFINITELY too slow. 1s is the maximum for me, given the huge amount of data, but, of course, nothing more: S

All help / hints / suggestions would be greatly appreciated. Thanks in advance!

+6
source share
1 answer

One option is to cache the entire output of your page, for example, based on route matching. You need to listen to the routing and scheduling, which route was found as a match, and then act accordingly:

 namespace MyModule; use Zend\Mvc\MvcEvent; class Module { public function onBootstrap(MvcEvent $e) { // A list of routes to be cached $routes = array('foo/bar', 'foo/baz'); $app = $e->getApplication(); $em = $app->getEventManager(); $sm = $app->getServiceManager(); $em->attach(MvcEvent::EVENT_ROUTE, function($e) use ($sm) { $route = $e->getRouteMatch()->getMatchedRouteName(); $cache = $sm->get('cache-service'); $key = 'route-cache-' . $route; if ($cache->hasItem($key)) { // Handle response $content = $cache->getItem($key); $response = $e->getResponse(); $response->setContent($content); return $response; } }, -1000); // Low, then routing has happened $em->attach(MvcEvent::EVENT_RENDER, function($e) use ($sm, $routes) { $route = $e->getRouteMatch()->getMatchedRouteName(); if (!in_array($route, $routes)) { return; } $response = $e->getResponse(); $content = $response->getContent(); $cache = $sm->get('cache-service'); $key = 'route-cache-' . $route; $cache->setItem($key, $content); }, -1000); // Late, then rendering has happened } } 

The second listener checks the rendering event. If this happens, the response result will be cached.

This system (perhaps not with a 100% copy / paste, but with the concept) works because if you return a Response during a route or sending event, the application will briefly turn off the application flow and stop further listeners from starting. Then he will serve this answer as is.

Remember that this will be a full page (including layout). If you do not want this (controller only), move the logic to the controller. The first event (now route) will be the sending of the controller. Listen to this before, so the normal execution of the action will be omitted. To cache the result, check the render event to listen on the layer.

/ update: I wrote a small module to use this DRY in your application: SlmCache

+17
source

All Articles