Добавить кеширование в Zend \ Form \ Annotation \ AnnotationBuilder

Поскольку я наконец нашел двоичный файл memcache для PHP 5.4.4 в Windows, я ускоряю приложение, которое я сейчас разрабатываю.

Мне удалось установить memcache как драйвер кэширования карт ORC, но мне нужно исправить еще одну утечку: Формы, созданные с помощью аннотаций.

Я создаю формы в соответствии с разделом Annotations документации . К сожалению, это занимает много времени, особенно при создании нескольких форм для одной страницы.

Можно ли добавить кеширование в этот процесс? Я просмотрел код, но похоже, что Zend\Form\Annotation\AnnotationBuilder всегда создает форму, отражая код и анализируя аннотации. Заранее спасибо.

Вы можете попробовать что-то вроде этого:

 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; } 

Найдено здесь .

Ответ Луи не работал для меня, поэтому я просто расширил конструктор AnnotationBuilder, чтобы взять объект кеша, а затем модифицировал getFormSpecification чтобы использовать этот кэш для кэширования результата. Моя функция ниже.

Очень быстрая работа … конечно, ее можно было бы улучшить. В моем случае я был ограничен некоторым старым оборудованием, и это заняло время загрузки на странице с 10 + секунд до 1 секунды

 /** * 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; }