Add caching to Zend \ Form \ Annotation \ AnnotationBuilder

Since I finally found the memcache binary for PHP 5.4.4 on Windows, I am speeding up the application that I am currently developing.

I managed to install memcache as an ORM ORM card cache driver, but I need to fix one more leak: Forms created using annotations.

I create forms according to the Annotations docs section. Unfortunately, this takes a lot of time, especially when creating multiple forms for one page.

Is it possible to add caching to this process? I looked at the code, but it seems that Zend\Form\Annotation\AnnotationBuilder always creates a form, reflecting the code and analyzing annotations. Thanks in advance.

+4
source share
2 answers

You can try something like this:

 class ZendFormCachedController extends Zend_Controller_Action { protected $_formId = 'form'; public function indexAction() { $frontend = array( 'lifetime' => 7200, 'automatic_serialization' => true); $backend = array('cache_dir' => '/tmp/'); $cache = Zend_Cache::factory('Core', 'File', $frontend, $backend); if ($this->getRequest()->isPost()) { $form = $this->getForm(new Zend_Form); } else if (! $form = $cache->load($this->_formId)) { $form = $this->getForm(new Zend_Form); $cache->save($form->__toString(), $this->_formId); } $this->getHelper('layout')->setLayout('zend-form'); $this->view->form = $form; } 

Found here .

+1
source

Louis's answer did not help me, so I just extended the AnnotationBuilder constructor to take the cache object, and then modified getFormSpecification to use this cache to cache the result. My function is below.

Very fast work ... of course, it could be improved. In my case, I was limited to some old hardware, and it took a page load time from 10 + seconds to 1 second

 /** * Creates and returns a form specification for use with a factory * * Parses the object provided, and processes annotations for the class and * all properties. Information from annotations is then used to create * specifications for a form, its elements, and its input filter. * * MODIFIED: Now uses local cache to store parsed annotations * * @param string|object $entity Either an instance or a valid class name for an entity * @throws Exception\InvalidArgumentException if $entity is not an object or class name * @return ArrayObject */ public function getFormSpecification($entity) { if (!is_object($entity)) { if ((is_string($entity) && (!class_exists($entity))) // non-existent class || (!is_string($entity)) // not an object or string ) { throw new Exception\InvalidArgumentException(sprintf( '%s expects an object or valid class name; received "%s"', __METHOD__, var_export($entity, 1) )); } } $formSpec = NULL; if ($this->cache) { //generate cache key from entity name $cacheKey = (is_string($entity) ? $entity : get_class($entity)) . '_form_cache'; //get the cached form annotations, try cache first $formSpec = $this->cache->getItem($cacheKey); } if (empty($formSpec)) { $this->entity = $entity; $annotationManager = $this->getAnnotationManager(); $formSpec = new ArrayObject(); $filterSpec = new ArrayObject(); $reflection = new ClassReflection($entity); $annotations = $reflection->getAnnotations($annotationManager); if ($annotations instanceof AnnotationCollection) { $this->configureForm($annotations, $reflection, $formSpec, $filterSpec); } foreach ($reflection->getProperties() as $property) { $annotations = $property->getAnnotations($annotationManager); if ($annotations instanceof AnnotationCollection) { $this->configureElement($annotations, $property, $formSpec, $filterSpec); } } if (!isset($formSpec['input_filter'])) { $formSpec['input_filter'] = $filterSpec; } //save annotations to cache if ($this->cache) { $this->cache->addItem($cacheKey, $formSpec); } } return $formSpec; } 
+1
source

Source: https://habr.com/ru/post/1433609/


All Articles