Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将缓存添加到Zend\Form\Annotation\AnnotationBuilder_Php_Caching_Annotations_Zend Form_Zend Framework2 - Fatal编程技术网

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

Php 将缓存添加到Zend\Form\Annotation\AnnotationBuilder,php,caching,annotations,zend-form,zend-framework2,Php,Caching,Annotations,Zend Form,Zend Framework2,在Windows上,我终于找到了PHP5.4.4的memcache二进制文件,我正在加速我目前正在开发的应用程序 我已经成功地将memcache设置为映射缓存驱动程序,但我需要修复另一个漏洞:使用注释构建的表单 我正在根据的注释部分创建表单。不幸的是,这需要很多时间,尤其是在为一个页面创建多个表单时 是否可以将缓存添加到此进程?我浏览了代码,但似乎Zend\Form\Annotation\AnnotationBuilder总是通过反映代码和解析注释来创建表单。提前谢谢。您可能想试试这样的东西:

在Windows上,我终于找到了PHP5.4.4的
memcache
二进制文件,我正在加速我目前正在开发的应用程序

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

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


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

我仍然希望有一个本地的解决方案,因为我在我的fav上发现了所有的结果。搜索引擎属于ZF1。请问您是如何让它工作的?我从来没有让这两者一起工作过(Doctrine2和ZendForm2注释生成器)。一些示例代码可以帮助我。谢谢。我想你还没有找到答案——我不确定这是否可能,至少在PHP5.4中是这样(我还没有在5.5上尝试过,但我没有看到任何可能的变化)。当我尝试保存时,我看到“无法序列化闭包”错误-我假设注释生成器正在将闭包添加到它所构建的表单上,尽管我没有看到任何闭包,但我会在生成的表单上执行zend dump()。如果你找到了答案,请更新!谢谢@mgkimsal—缓存表单(假设注释)所带来的真正性能变化主要存在于注释解析中。我将缓存这些内容的解决方案放在下面。给了我显著的性能提升。