Get Smarty

Donate

Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Advertisement

Bab 14. Caching

Caching is used to speed up a call to display() or fetch() by saving its output to a file. If a cached version of the call is available, that is displayed instead of regenerating the output. Caching can speed things up tremendously, especially templates with longer computation times. Since the output of display() or fetch() is cached, one cache file could conceivably be made up of several template files, config files, etc.

Since templates are dynamic, it is important to be careful what you are caching and for how long. For instance, if you are displaying the front page of your website that does not change its content very often, it might work well to cache this page for an hour or more. On the other hand, if you are displaying a page with a timetable containing new information by the minute, it would not make sense to cache this page.

Menyiapkan Cache

Hal pertama yang dilakukan adalah menghidupkan cache dengan menyetel $caching = 1 (atau 2).

Teladan 14-1. Menghidupkan cache

caching = 1; $smarty->display(\'index.tpl\'); ?>'); ?>

Dengan menghidupkan cache, fungsi panggil ke display('index.tpl') akan membuat template seperti biasa, tapi juga menyimpan duplikat dari outputnya ke sebuah file (duplikat cache) dalam $cache_dir. Panggilan berikutnya ke display('index.tpl'), duplikat cache akan dipakai daripada membuat template kembali.

Catatan Teknis: File dalam $cache_dir diberi nama mirip dengan nama template. Meskipun berakhir dalam ekstensi .php, ini tidak dimaksudkan dijalankan secara langsung. Jangan edit file ini!

Setiap halaman yang di-cache memiliki batasan usia yang ditentukan oleh $cache_lifetime. Nilai standarnya adalah 3600 detik atau satu jam. Setelah itu waktu berakhir, cache dibuat ulang. Dimungkinkan untuk memberikan cache individual memiliki waktu berakhirnya dengan menyetel $caching=2. Lihat $cache_lifetime untuk lebih jelasnya.

Teladan 14-2. Menyetel $cache_lifetime per cache

caching = 2; // usia per cache // set cache_lifetime untuk index.tpl ke 5 menit $smarty->cache_lifetime = 300; $smarty->display(\'index.tpl\'); // set cache_lifetime untuk home.tpl ke 1 jam $smarty->cache_lifetime = 3600; $smarty->display(\'home.tpl\'); // CATATAN: setelan $cache_lifetime berikut tidak akan bekerja saat $caching = 2. // Usia cache untuk home.tpl sudah disetel ke 1 jam, dan tidak akan memperhatikan // nilai lagi $cache_lifetime. // Cache home.tpl masih akan berakhir setelah 1 jam. $smarty->cache_lifetime = 30; // 30 seconds $smarty->display(\'home.tpl\'); ?>'); ?>

Jika $compile_check dihidupkan, setiap file template dan file config yang terkait dengan file cache diperiksa modifikasinya. Jika setiap file sudah dimodifikasi sejak cache dibuat, cache segera dibuat ulang. Ini adalah beban kecil untuk performansi optimal, set $compile_check ke FALSE.

Teladan 14-3. Menghidupkan $compile_check

caching = 1; $smarty->compile_check = true; $smarty->display(\'index.tpl\'); ?>'); ?>

Jika $force_compile dihidupkan, file cache akan selalu dibuat. Ini efektif mematikan cache. $force_compile biasanya hanya untuk keperluan debugging, cara yang lebih efisien mematikan cache adalah menyetel $caching = 0.

Fungsi is_cached() dapat digunakan untuk menguji jika template memiliki cache yang benar atau tidak. Jika anda memiliki template yang di-cache yang memerlukan hal seperti pengambilan database, anda bisa menggunakan ini untuk melewati proses itu.

Teladan 14-4. Menggunakan is_cached()

caching = 1; if(!$smarty->is_cached(\'index.tpl\')) { // Cache tidak tersedia, lakukan penempatan variabel di sini. $contents = get_database_contents(); $smarty->assign($contents); } $smarty->display(\'index.tpl\'); ?>'); ?>

Anda dapat memelihara bagian halaman dinamis dengan fungsi template {insert}. Katakanlah seluruh halaman dapat di-cache kecuali untuk iklan yang ditampilkan di bawah halaman. Dengan menggunakan fungsi {insert} untuk iklan, anda bisa memelihara elemen ini dinamis dalam konten yang di-cache. Lihat dokumentasi pada {insert} untuk lebih jelas dan contohnya.

Anda bisa membersihkan semua file cache dengan fungsiclear_all_cache(), atau file cache individual dan grup dengan fungsi clear_cache().

Teladan 14-5. Membersihkan cache

caching = 1; // bersihkan hanya cache untuk index.tpl $smarty->clear_cache(\'index.tpl\'); // bersihkan semua file cache $smarty->clear_all_cache(); $smarty->display(\'index.tpl\'); ?>'); ?>
Comments
No comments for this page.

Advertisement

Sponsors [info]

Sponsors