如何清理Magento缓存?

时间:2011-03-28 14:06:54

标签: php magento

我需要一个简单的脚本来刷新Magento缓存的每个元素。 我正在运行1.3.2.3并且无法升级。

3 个答案:

答案 0 :(得分:5)

用于刷新magento缓存的示例cron作业脚本: (该剧本由 amartinez 在magento论坛上发表)

通过cron job运行

00  0 * * *   root /var/www/html/magento/cron_refresh_cache.php >> /var/log/cron_refresh_cache.log 

php文件:

#!/usr/bin/php
<?php
/**
* cron_refresh_cache.php
* 
* @copyright  copyright (c) 2009 toniyecla[at]gmail.com
* @license    http://opensource.org/licenses/osl-3.0.php open software license (OSL 3.0)
*/

( !$_SERVER["HTTP_USER_AGENT"] ) or die ( "Nothing to do\n" ); // to run via local browser use ($_SERVER["SERVER_ADDR"] == $_SERVER["REMOTE_ADDR"]) 

require_once 'app/Mage.php';
umask( 0 );
Mage::app( "default" ); // if getting error change this line to Mage::app(Mage::app()->getStore());  
$ver = Mage::getVersion();
$userModel = Mage::getModel( 'admin/user' );
$userModel -> setUserId( 0 );
Mage::getSingleton( 'admin/session' )->setUser( $userModel );

echo "Refreshing cache...\n";
Mage::app()->cleanCache();
$enable = array();
foreach ( Mage::helper( 'core' )->getCacheTypes() as $type => $label ) {
    $enable[$type] = 1;
    } 

Mage::app()->saveUseCache( $enable ); 
refresh_cache(); // call refresh function 

function refresh_cache() 
    {    
        $this -> notify( 'Refreshing cache' );
        try {
            Mage :: getSingleton( 'catalog/url' ) -> refreshRewrites();
            $this -> notify( 'Catalog Rewrites was refreshed successfully', 'blank');
            } 
        catch ( Exception $e ) {
            $this -> notify( $e -> getMessage(), 'warning' );
            }
        try {
            Mage :: getSingleton( 'catalog/index' ) -> rebuild();
            $this -> notify( 'Catalog Index was rebuilt successfully', 'blank');
            } 
        catch ( Exception $e ) {
            $this -> notify( $e -> getMessage(), 'warning' );
            }
        try {
            $flag = Mage :: getModel( 'catalogindex/catalog_index_flag' ) -> loadSelf();
            if ( $flag -> getState() == Mage_CatalogIndex_Model_Catalog_Index_Flag :: STATE_RUNNING ) {
                $kill = Mage :: getModel( 'catalogindex/catalog_index_kill_flag' ) -> loadSelf();
                $kill -> setFlagData( $flag -> getFlagData() ) -> save();
                } 
            $flag -> setState( Mage_CatalogIndex_Model_Catalog_Index_Flag :: STATE_QUEUED ) -> save();
            Mage :: getSingleton( 'catalogindex/indexer' ) -> plainReindex();
            $this -> notify( 'Layered Navigation Indices was refreshed successfully', 'blank');
            } 
        catch ( Exception $e ) {
            $this -> notify( $e -> getMessage(), 'warning' );
            }
        try {
            Mage :: getModel( 'catalog/product_image' ) -> clearCache();
            $this -> notify( 'Image cache was cleared successfully', 'blank');
            } 
        catch ( Exception $e ) {
            $this -> notify( $e -> getMessage(), 'warning' );
            }
        try {
            Mage :: getSingleton( 'catalogsearch/fulltext' ) -> rebuildIndex();
            $this -> notify( 'Search Index was rebuilded successfully', 'blank');
            } 
        catch ( Exception $e ) {
            $this -> notify( $e -> getMessage(), 'warning' );
            }
        try {
            Mage :: getSingleton( 'cataloginventory/stock_status' ) -> rebuild();
            $this -> notify( 'CatalogInventory Stock Status was rebuilded successfully', 'blank');
            } 
        catch ( Exception $e ) {
            $this -> notify( $e -> getMessage(), 'warning' );
            }
        try {
            Mage :: getResourceModel( 'catalog/category_flat' ) -> rebuild();
            $this -> notify( 'Flat Catalog Category was rebuilt successfully', 'blank');
            } 
        catch ( Exception $e ) {
            $this -> notify( $e -> getMessage(), 'warning' );
            }
        try {
            Mage :: getResourceModel( 'catalog/product_flat_indexer' ) -> rebuild();
            $this -> notify( 'Flat Catalog Product was rebuilt successfully', 'blank');
            } 
        catch ( Exception $e ) {
            $this -> notify( $e -> getMessage(), 'warning' );
            }
        }  


?>

答案 1 :(得分:3)

我刚刚运行此代码,我收到一条错误消息:

Using $this when not in object context in cron_refresh_cache.php on line 34

也许 amartinez 通过magento cron使用它,而我使用的是os cron?

无论如何,我通过创建一个类来解决它,在其中包含“refresh_cache()”函数,并且还为类提供了一个名为“notify”的函数,该函数接受了两个参数,并且只是回显了它们。

然后我创建了这个类的新实例并调用了它的“refresh_cache”方法,而不是像原始代码一样直接调用“refresh_cache()”。

这是我修改后的代码:

Using $this when not in object context in cron_refresh_cache.php on line 34

答案 2 :(得分:2)

删除/var/cache/

中的所有内容

如果您想要清理数据库:http://www.magentocommerce.com/wiki/groups/227/maintenance_script