将缓存添加到Zend \ Form \ Annotation \ AnnotationBuilder

时间:2012-09-11 15:28:43

标签: php caching annotations zend-form zend-framework2

因为我终于在Windows上找到了PHP 5.4.4的memcache二进制文件,所以我正在加速我正在开发的应用程序。

我已成功将memcache设置为Doctrine ORM Mapping Cache驱动程序,但我需要修复另一个泄漏:使用注释构建的表单。

我正在根据docs的“注释”部分创建表单。不幸的是,这需要花费很多时间,特别是在为单个页面创建多个表单时。

是否可以在此流程中添加缓存?我浏览了代码,但似乎Zend\Form\Annotation\AnnotationBuilder总是通过反映代码和解析注释来创建表单。提前谢谢。

2 个答案:

答案 0 :(得分:1)

你可能想尝试这样的事情:

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

找到here

答案 1 :(得分:1)

路易斯的答案对我不起作用所以我所做的只是扩展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;
}