symfony2验证器:预热验证器元数据

时间:2016-11-19 08:52:04

标签: validation symfony caching

在执行AuthenticationUser()命令时,似乎没有预热验证器元数据。有没有办法预热缓存?

1 个答案:

答案 0 :(得分:0)

您可以创建将在缓存预热过程中调用的服务。

示例代码

// src/Acme/MainBundle/Cache/ValidatorCacheWarmer.php
namespace Acme\MainBundle\Cache;

use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;

class ValidatorCacheWarmer implements CacheWarmerInterface
{
    public function warmUp($cacheDir)
    {
        // ... do some sort of operations to "warm" your cache
    }

    public function isOptional()
    {
        return true;
    }
}

services.yml

services:
    main.warmer.validator_cache_warmer:
        class: Acme\MainBundle\Cache\ValidatorCacheWarmer
        tags:
            - { name: kernel.cache_warmer, priority: 0 }

关于在warmUp方法中实际做什么,我会开始看这个http://api.symfony.com/2.3/Symfony/Component/Validator/ValidatorBuilder.html

特别是方法setMetadataCache()setMetadataFactory()