Symfony:清除Doctrine缓存

50

我需要在 Symfony 中清除我的Doctrine的缓存。

有一种命令行方式可以清除缓存

或者我应该在哪里找到并删除属于缓存的文件呢?


5
rm -rf app/cache/* 没有起作用? - Vitalii Zurian
答案是否有所帮助?如果没有,请尝试使用app/console cache:clear清除完整缓存。 - amitchhajer
2
请注意,Doctrine 缓存通常存储在 APC 中而不是文件系统中,因此删除缓存文件无法解决问题。 - @thecatontheflat - caponica
1
请注意,一般的 app/console cache:clear 命令只会清除 Symfony (app) 缓存。我认为它不会清除 Doctrine 缓存。 - caponica
8个回答

142

对于Symfony 3+:

 php bin/console

将列出所有命令,以下是与缓存相关的命令:

 php bin/console doctrine:cache:clear-metadata 
 php bin/console doctrine:cache:clear-query  
 php bin/console doctrine:cache:clear-result

在Symfony 3之前:

app/console

将列出如何实现它的方法。

 app/console doctrine:cache:clear-metadata 
 app/console doctrine:cache:clear-query  
 app/console doctrine:cache:clear-result 

顺便说一下,如果你正在使用mongodb,你可以执行'app/console doctrine:mongodb:cache:clear-metadata'或者只需执行'app/console doctrine',你就可以看到所有可用的选项。 - Gigala
3
请注意,您始终可以使用 app/console list 来显示所有命令,或者使用 app/console list doctrine 来仅显示 'doctrine' 命名空间中的命令。 - caponica
2
我在生产环境中尝试了 doctrine:cache:clear-result 命令,但它没有起作用。我还尝试了 bin/doctrine orm:clear-cache:result 命令,但也没有效果。我正在使用 memcache - Sithu

11

如果你想在代码中实现它(来自Doctrine文档):

If you simply want to delete all cache entries you can do so with the deleteAll() method.

$cacheDriver = new \Doctrine\Common\Cache\ArrayCache();
$deleted = $cacheDriver->deleteAll();

2
缓存刷新和缓存清除有什么区别? - Czechnology
1
这段代码创建了一个新的缓存驱动程序。它应该利用当前配置中的QueryCacheProfile或结果缓存实现。如果您使用Redis或Memcached等,它不会自动清除应用程序中的缓存。 - Kafoso

2
如果您使用APC,您也可以直接调用代码。
<?php
$deleted = apc_clear_cache() && apc_clear_cache('user');

在同一服务器上的 PHP 页面中。这就是 Antho 答案中 deleteAll() 方法所做的,但你不依赖于 Doctrine 类。顺便说一下:完整的缓存将被清除 - 以防你将其用于非 Doctrine 的东西。


2

我曾经对Doctrine的结果缓存感到困惑 - 最终我不得不重启Memcached。


如何重新启动Memcached? - Skrol29

1

我知道这篇文章的标题是Symfony 2,但是对于从谷歌过来的读者,如果你使用的是Symfony 3+,那么:

bin/console

与之相对的是:
app/console

0
也许现在有点晚了,但是在我的情况下,Doctrine没有在生产环境中生成代理类。所以我将 auto_generate_proxy_classes 设置为 true:
#symfony2&3 app/config/config.yml
#symfony4 config/packages/doctrine.yaml (by default true since 4.2)

doctrine:
    orm:
        auto_generate_proxy_classes: true #"%kernel.debug%"

1
这些代理类是做什么用的? - pablosd

0
也许有人在寻找
Symfony 6.2+
public function __construct(..., private KernelInterface $kernel

$this->kernel->shutdown(); //RESET ALL

你运营着多个实体,并且通过会话管理连接。
/** @var SiteService $siteService */
$siteService = $this->kernel->getContainer()->get(SiteService::class); //Important kernel->getContainer()
/** @var Site[] $sites */
$sites = $siteService->repository->getByInstalledSites();

foreach ($sites as $site) {
    dump('Domain:'. $site->getDomain());
    $session = new Session();
    $session->set('db_user', $site->getUser());
    $session->set('db_password', $this->getDecryptPassword($site->getPassword()));
    $session->set('db_name', $site->getDatabaseName())
    
    $user = $this->userService->getById(1);

    dump($user);
    //what you want to do ...

    $this->kernel->shutdown(); //Running All bundles shutdown and container set null        
    $this->kernel->boot(); // services_resetter->reset and preboot -> loading bundles&Containers and all bundles set container & run boot
}

0
希望这能帮助到遇到Doctrine缓存问题的人。
我遇到了OrderBy属性和PostLoad事件似乎无法触发的问题。问题在于实体已经被加载到EntityManager中(在我创建它时)。因为它已经被加载,后续对它的获取调用会直接从内存/缓存中获取,预期的事件不会被触发。
要解决这个问题,你可以清除EntityManager中的实体。然后当你获取实体时,PostLoad回调、OrderBy等都会被触发。你可以这样清除EntityManager中的实体:
$this->em->clear(MyEntity::class);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接