将缓存添加到ZendFormAnnotationAnnotationBuilder


Add caching to ZendFormAnnotationAnnotationBuilder

由于我终于在Windows上找到了适用于PHP 5.4.4的memcache二进制文件,我正在加快我目前正在开发的应用程序的速度。

我已经成功地将memcache设置为Doctrine ORM映射缓存驱动程序,但我需要修复另一个漏洞:使用注释构建的表单。

我正在根据文档的"注释"部分创建表单。不幸的是,这需要花费大量时间,尤其是在为单个页面创建多个表单时。

是否可以将缓存添加到此进程?我已经浏览了代码,但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;
    }

在这里找到。

Louis的回答对我不起作用,所以我只是简单地扩展了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;
}
相关文章:
  • 没有找到相关文章