4

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

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

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

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

4

2 回答 2

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

在这里找到。

于 2012-09-11T15:34:46.393 回答
1

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;
}
于 2013-08-12T16:03:10.940 回答