PHPExcel解決內存佔用過大問題-設置單元格對象緩存

PHPExcel解決內存佔用過大問題-設置單元格對象緩存

PHPExcel是一個很強大的處理Excel的PHP開源類,可是很大的一個問題就是它佔用內存太大,從1.7.3開始,它支持設置cell的緩存方式,可是推薦使用目前穩定的版本1.7.6,由於以前的版本都會不一樣程度的存在bug,如下是其官方文檔:php

 

PHPExcel uses an average of about 1k/cell in your worksheets, so large workbooks can quickly use up available memory. Cell caching provides a mechanism that allows PHPExcel to maintain the cell objects in a smaller size of memory, on disk, or in APC, memcache or Wincache, rather than in PHP memory. This allows you to reduce the memory usage for large workbooks, although at a cost of speed to access cell data.

 

PHPExcel平均下來使用1k/單元格的內存,所以大的文檔會致使內存消耗的也很快。單元格緩存機制可以容許PHPExcel將內存中的小的單元格對象緩存在磁盤或者APC,memcache或者Wincache中,儘管會在讀取數據上消耗一些時間,可是可以幫助你下降內存的消耗。緩存

 

若是仍是不夠,打開php.ini改裏面的memory_limit服務器

默認是128M調大點。app

 

 

 

 

By default, PHPExcel still holds all cell objects in memory, but you can specify alternatives. To enable cell caching, you must call the PHPExcel_Settings::setCacheStorageMethod() method, passing in the caching method that you wish to use.

 

默認狀況下,PHPExcel依然將單元格對象保存在內存中,可是你能夠自定義。你可使用PHPExcel_Settings::setCacheStorageMethod()方法,將緩存方式做爲參數傳遞給這個方法來設置緩存的方式。less

 

 

Php代碼  

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory;  ide

PHPExcel_Settings::setCacheStorageMethod($cacheMethod);  性能

 

setCacheStorageMethod() will return a boolean true on success, false on failure (for example if trying to cache to APC when APC is not enabled).

 setCacheStorageMethod()方法會返回一個BOOL型變量用於表示是否成功設置(好比,若是APC不能使用的時候,你設置使用APC緩存,將會返回false)ui

 

A separate cache is maintained for each individual worksheet, and is automatically created when the worksheet is instantiated based on the caching method and settings that you have configured. You cannot change the configuration settings once you have started to read a workbook, or have created your first worksheet.

 

每個worksheet都會有一個獨立的緩存,當一個worksheet實例化時,就會根據設置或配置的緩存方式來自動建立。一旦你開始讀取一個文件或者你已經建立了第一個worksheet,就不能在改變緩存的方式了。this

 

Currently, the following caching methods are available.

 目前,有如下幾種緩存方式可使用:spa

 

Php代碼  

PHPExcel_CachedObjectStorageFactory::cache_in_memory;  

The default. If you don’t initialise any caching method, then this is the method that PHPExcel will use. Cell objects are maintained in PHP memory as at present.

 默認狀況下,若是你不初始化任何緩存方式,PHPExcel將使用內存緩存的方式。

===============================================

 

Php代碼  

PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized;  

Using this caching method, cells are held in PHP memory as an array of serialized objects, which reduces the memory footprint with minimal performance overhead.

 使用這種緩存方式,單元格會以序列化的方式保存在內存中,這是下降內存使用率性能比較高的一種方案。

===============================================

 

Php代碼  

PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;  

Like cache_in_memory_serialized, this method holds cells in PHP memory as an array of serialized objects, but gzipped to reduce the memory usage still further, although access to read or write a cell is slightly slower.

 與序列化的方式相似,這種方法在序列化以後,又進行gzip壓縮以後再放入內存中,這回跟進一步下降內存的使用,可是讀取和寫入時會有一些慢。

 

===========================================================

 

Php代碼  

PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;  

When using cache_to_discISAM all cells are held in a temporary disk file, with only an index to their location in that file maintained in PHP memory. This is slower than any of the cache_in_memory methods, but significantly reduces the memory footprint.
Php代碼  

PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;  

Like cache_to_discISAM, when using cache_to_phpTemp all cells are held in the php://temp I/O stream, with only an index to their location maintained in PHP memory. In PHP, the php://memory wrapper stores data in the memory: php://temp behaves similarly, but uses a temporary file for storing the data when a certain memory limit is reached. The default is 1 MB, but you can change this when initialising cache_to_phpTemp.

 相似cache_to_discISAM這種方式,使用cache_to_phpTemp時,全部的單元格會還存在php://temp I/O流中,只把他們的位置保存在PHP的內存中。PHP的php://memory包裹器將數據保存在內存中,php://temp的行爲相似,可是當存儲的數據大小超過內存限制時,會將數據保存在臨時文件中,默認的大小是1MB,可是你能夠在初始化時修改它:

Php代碼  

$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;  

$cacheSettings = array( ' memoryCacheSize '  => '8MB'  );  

PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);  

The php://temp file is automatically deleted when your script terminates.

php://temp文件在腳本結束是會自動刪除。

 

 

===========================================================

 

Php代碼  

PHPExcel_CachedObjectStorageFactory::cache_to_apc;  

When using cache_to_apc, cell objects are maintained in APC with only an index maintained in PHP memory to identify that the cell exists. By default, an APC cache timeout of 600 seconds is used, which should be enough for most applications: although it is possible to change this when initialising cache_to_APC.

 當使用cach_to_apc時,單元格保存在APC中,只在內存中保存索引。APC緩存默認超時時間時600秒,對絕大多數應用是足夠了,固然你也能夠在初始化時進行修改:

Php代碼  

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_APC;  

$cacheSettings = array( 'cacheTime'  => 600   );  

PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);  

When your script terminates all entries will be cleared from APC, regardless of the cacheTime value, so it cannot be used for persistent storage using this mechanism.

 當腳本運行結束時,全部的數據都會從APC中清楚(忽略緩存時間),不能使用此機制做爲持久緩存。

 

 

===========================================================

Php代碼  

PHPExcel_CachedObjectStorageFactory::cache_to_memcache  

When using cache_to_memcache, cell objects are maintained in memcache with only an index maintained in PHP memory to identify that the cell exists.
Php代碼  

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_memcache;  

$cacheSettings = array( 'memcacheServer'  => 'localhost',  

                        'memcachePort'    => 11211,  

                        'cacheTime'       => 600  

 );  

PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);  

 從初始化設置的形式上看,MS還不支持多臺memcache服務器輪詢的方式,比較遺憾。

When your script terminates all entries will be cleared from memcache, regardless of the cacheTime value, so it cannot be used for persistent storage using this mechanism.

 當腳本結束時,全部的數據都會從memcache清空(忽略緩存時間),不能使用該機制進行持久存儲。

 

 

===========================================================

Php代碼  

PHPExcel_CachedObjectStorageFactory::cache_to_wincache;  

When using cache_to_wincache, cell objects are maintained in Wincache with only an index maintained in PHP memory to identify that the cell exists. By default, a Wincache cache timeout of 600 seconds is used, which should be enough for most applications: although it is possible to change this when initialising cache_to_wincache.

 使用cache_towincache方式,單元格對象會保存在Wincache中,只在內存中保存索引,默認狀況下Wincache過時時間爲600秒,對絕大多數應用是足夠了,固然也能夠在初始化時修改:

Php代碼  

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_wincache;  

$cacheSettings = array( 'cacheTime'  => 600 );  

PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);  

When your script terminates all entries will be cleared from Wincache, regardless of the cacheTime value, so it cannot be used for persistent storage using this mechanism.

 

 

PHPExcel仍是比較強大的,最大的問題就是內存佔用的問題,PHPExcel啥時候能出一個輕量級的版本,不須要那麼多花哨的功能,只須要導出最普通的數據的版本就行了!

相關文章
相關標籤/搜索