Cache
Вернуться к: File_Archive
Cache
File_Archive 1.4 introduce the possibility to use a cache to store intermediate result of a zip compression. It uses the Cache_Lite PEAR package to do so.
A zip file is made of compressed files, one after the others. So if you generate an archive that contains files A, B and C and then another archive that contains A and C, you will compress twice the files A and C. The use of the cache will allow to save the compressed version of files A, B and C on the first compression, to use them again in the second compression.
Usage examples
The cache can be (and should be) used if you dynamically create some zip archive that contains frequently the same files. For example, you may want to allow the user to select some images, videos or other files from your gallery and allow them to download a compressed zip archive that contains these files.
If you do so without cache, your server will answer very slowly if a lot of users ask the files. With the cache, the files will be compressed only once.
On my machine (a thinkpad T42P with default factory equipment), generating a 200MB zip archive takes around 30s of CPU without the cache, 32s of CPU with an empty cache and 2s of CPU if all the files to compress are already in cache.
How to use the cache
The cache is a Cache_Lite object. So you must have installed the package. Then all you have to do is use the File_Archive::setOption() function with the cache parameter.
Set up the cache
<?php
require_once "File/Archive.php";
require_once "Cache/Lite.php";
//Create the cache object
$cache = new Cache_Lite(
array(
//See the doc of Cache_Lite for its constructor parameters
)
);
//Ask File_Archive to use the cache object we just created
File_Archive::setOption('cache', $cache);
//And then create your archives as usual
//Generate a file called archive.zip in the working folder
File_Archive::extract(
'folderToCompress',
'archive.zip'
);
//Send an archive to the user
File_Archive::extract(
'folderToCompress',
File_Archive::toArchive(
'archive.zip',
File_Archive::toOutput()
)
);
?>
Вернуться к: File_Archive