如何清理 Magento 缓存?

发布于 2024-10-27 05:24:50 字数 59 浏览 2 评论 0原文

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

I need a simple script that will refresh every single element of the Magento cache.
I'm running 1.3.2.3 and can't upgrade.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦断已成空 2024-11-03 05:24:50

刷新 magento 缓存的 cron 作业脚本示例:
(该脚本由 amartinez 在 magento 论坛中发布)

通过 cron 作业运行

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


?>

Example cron job script to refresh magento cache :
( the script was published in magento forum from amartinez )

Run via cron job

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

The php file :

#!/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' );
            }
        }  


?>
七秒鱼° 2024-11-03 05:24:50

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


不在 cron_refresh_cache.php 第 34 行的对象上下文中时使用 $this

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

不管怎样,我通过创建一个类,在其中包装“refresh_cache()”函数,并为该类提供一个名为“notify”的函数来解决这个问题,该函数接受两个参数,并且只是将它们回显出来。

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

这是我修改后的代码:

#!/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 
class RefreshCacheCron{
    function notify($message,$type){
        echo date("d/m/Y G:i:s"). " $type: $message\r\n";
    }
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' );
            }
        }  

}

$refresher=new RefreshCacheCron();
$refresher->refresh_cache();
?>

I just ran this code and I got an error saying:


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

Maybe amartinez was using it via the magento cron whereas I was using from the os cron?

Anyways, I solved it by creating a class, wrapping the "refresh_cache()" function in it, and also giving the class a function called "notify" which accepted two parameters, and which just echoed them out.

Then I created a new instance of this class and called its "refresh_cache" method instead of directly calling "refresh_cache()" like the original code.

Here's my modified code:

#!/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 
class RefreshCacheCron{
    function notify($message,$type){
        echo date("d/m/Y G:i:s"). " $type: $message\r\n";
    }
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' );
            }
        }  

}

$refresher=new RefreshCacheCron();
$refresher->refresh_cache();
?>
零度℉ 2024-11-03 05:24:50

删除 /var/cache/ 中的所有内容

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

Delete everything inside /var/cache/

If you want to clean up your db as well: http://www.magentocommerce.com/wiki/groups/227/maintenance_script

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文