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!