Cache/zh-tw: Difference between revisions

From Joomla! Documentation

FuzzyBot (talk | contribs)
Updating to match new version of source page
FuzzyBot (talk | contribs)
Updating to match new version of source page
Line 1: Line 1:
<noinclude><languages /></noinclude>
<noinclude><languages /></noinclude>
<br />
<br />
<div class="mw-translate-fuzzy">
;Joomla 使用不同的方式來快取"內容"。以下是關於管理員以及開發者何時、如何、何處以及有哪些快取的總覽。
;Joomla 使用不同的方式來快取"內容"。以下是關於管理員以及開發者何時、如何、何處以及有哪些快取的總覽。
</div>


=系統管理員=
=系統管理員=
Line 12: Line 14:
# 來自joomla 模組,顯示於該頁面上的輸出 – 我們稱為模組快取 (Module cache)
# 來自joomla 模組,顯示於該頁面上的輸出 – 我們稱為模組快取 (Module cache)
您有好幾個快取設定可以讓您控制要建立哪些快取:
您有好幾個快取設定可以讓您控制要建立哪些快取:
<div class="mw-translate-fuzzy">
# 系統外掛 "System – Page Cache"
# 系統外掛 "System – Page Cache"
# 全站設定,系統頁籤,快取設定。在此您可以設定系統快取選項
# 全站設定,系統頁籤,快取設定。在此您可以設定系統快取選項
Line 18: Line 21:
#* ON – 啟用累進快取
#* ON – 啟用累進快取
# 在許多模組的設定頁面中,有進階設定頁籤可以設定是否要使用全站設定,或是停用快取
# 在許多模組的設定頁面中,有進階設定頁籤可以設定是否要使用全站設定,或是停用快取
As described below, there are also rules for caching which are implemented within the Joomla code, and over which you have no control.
</div>
<span lang="en" dir="ltr" class="mw-content-ltr">As described below, there are also rules for caching which are implemented within the Joomla code, and over which you have no control.</span>


您可以從管理員選單項目「系統」➞「清除快取」來清除快取。
<span class="mw-translate-fuzzy">您可以從管理員選單項目「系統」➞「清除快取」來清除快取。</span>
<div class="mw-translate-fuzzy">
一般而言,您可以想像成Joomla 有三個層級的快取原則,愈底下愈積極
一般而言,您可以想像成Joomla 有三個層級的快取原則,愈底下愈積極
</div>
# 保守快取
# 保守快取
# 累進快取
# 累進快取
Line 27: Line 33:
接著我們會詳細地來看看這三個項目
接著我們會詳細地來看看這三個項目


此外,joomla開發者可以使用快取機制來儲存...舉例來說,資料庫的檢索結果,以提升網站的反應速度,然而這已經超出系統管理員的範圍了。  
<span class="mw-translate-fuzzy">此外,joomla開發者可以使用快取機制來儲存...舉例來說,資料庫的檢索結果,以提升網站的反應速度,然而這已經超出系統管理員的範圍了。</span>


== 頁面快取 ==
== 頁面快取 ==
To switch this on, go to administrator Extensions /  Plugins, find the System – Page Cache plugin, and enable it. This means that site pages will now be cached, and whenever they're requested again the cached page will be served, rather than it being generated by Joomla from the information in the database. The cached page will continue to be served until it's expired – as defined by the Cache Time parameter in the Global Configuration / System / Cache Settings.  
<span lang="en" dir="ltr" class="mw-content-ltr">To switch this on, go to Administrator → Extensions → Plugins. Then find the System – Page Cache plugin, and enable it. This means that site pages will now be cached and whenever they're requested again, the cached page will be served, rather than it being generated by Joomla from the information in the database. The cached page will continue to be served until it's expired – as defined by the ''Cache Time'' parameter in the Administrator → Global Configuration → System tab → Cache Settings.</span>


If you're reading this page as a tutorial and want to test the page caching, then it's best to set the Global Configuration cache settings to  
<span lang="en" dir="ltr" class="mw-content-ltr">If you're reading this page as a tutorial and want to test the page caching, it's best to set the Global Configuration cache settings to:</span>
<div lang="en" dir="ltr" class="mw-content-ltr">
* Cache Handler – File
* Cache Handler – File
* Path to Cache Folder – leave blank
* Path to Cache Folder – leave blank
* Cache Time – 15 (the default of 15 minutes)
* Cache Time – 15 (the default of 15 minutes)
* Platform Specific Caching - No
* System Cache – OFF – Caching disabled
* System Cache – OFF – Caching disabled
To check page caching is working, go to a site webpage which displays an article. After you display that page you should find in the file system a <tt>cache/page</tt> directory with a file in it which has a filename like <tt><string of hex digits>-cache-page-<string of hex digits>.php</tt>. (Joomla has to store separate cache pages for separate URLs so the second string of hex digits is a hash of the URL of the site webpage, to make the filename unique to that page).  
</div>
<span lang="en" dir="ltr" class="mw-content-ltr">To verify that page caching is working, go to a website page that displays an article. After you display that page you should find in the file system a <tt>cache/page</tt> directory with a file in it which has a filename like <tt><string of hex digits>-cache-page-<string of hex digits>.php</tt>. (Joomla has to store separate cache pages for separate URLs so the second string of hex digits is a hash of the URL of the site web page, to make the filename unique to that page).</span>


Then use the administrator functionality to change the text of that article, and redisplay the site webpage. You should find the cached version, not your modified text.
<span lang="en" dir="ltr" class="mw-content-ltr">Then use the Administrator functionality to change the text of that article, and redisplay the site web page. You should find the cached version, not your modified text.</span>


Changing an article (or other Joomla item) does not clear the page cache for the webpage(s) where that article is displayed.  
<div lang="en" dir="ltr" class="mw-content-ltr">
To clear the page cache go to administrator System / Clear Cache. Click on the checkbox next to the Cache Group called "page", and press the Delete button. When you redisplay your web page it should now show your amended text.
Changing an article (or other Joomla item) does not clear the page cache for the web page(s) where that article is displayed.  
To clear the page cache go to Administrator → System → Clear Cache. Click on the checkbox next to the ''Cache Group'' called "page", and press the Delete button. When you redisplay your web page it should now show your amended text.
</div>


If your site has a function like a shopping basket then applying page caching will cause problems, as pages have to show what the customer has already selected, rather than displaying a cached page which is common to everyone. However, you can configure the System Page Cache plugin to exclude caching specified Menu Items or specified URLs and URL ranges (in the Advanced tab), so that only truly static pages are cached.  
<span lang="en" dir="ltr" class="mw-content-ltr">If your site has a function like a shopping basket, applying page caching will cause problems, as pages have to show what the customer has already selected, rather than displaying a cached page which is common to everyone. However, you can configure the System - Page Cache plugin to exclude caching specified Menu Items or specified URLs and URL ranges (in the Advanced tab), so that only truly static pages are cached.</span>


== 保守快取 ==
== 保守快取 ==
With Conservative Caching you can cache the View output from components and the output from those Modules which allow caching. But note that this will work only on pages which are not cached using the Page Cache, as for those pages the whole webpage is cached, and Conservative Caching isn't even considered.
<span lang="en" dir="ltr" class="mw-content-ltr">With Conservative Caching you can cache the View output from components and the output from those Modules which allow caching. But note that this will work only on pages which are not cached using the Page Cache. For those pages the whole web page is cached, and Conservative Caching isn't even considered.</span>


To switch on Conservative Caching:
<span lang="en" dir="ltr" class="mw-content-ltr">To switch on Conservative Caching:</span>
# Go to administrator Global Configuration / System and within Cache Settings set System Cache to ON – Conservative caching
<div lang="en" dir="ltr" class="mw-content-ltr">
# Go to Extensions / Modules and select the modules which you'd like to be cached. If that module permits caching then under the Advanced tab you should be able to set Caching to  
# Go to Administrator → System → Global Configuration → System tab and within Cache Settings, set System Cache to ON – Conservative caching
# Go to Administrator → Extensions → Modules and select the modules which you'd like to be cached. If that module permits caching then under the Advanced tab you should be able to set Caching to
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* Use Global – this module will be cached (with the Global option now having been set to Conservative caching)
* Use Global – this module will be cached (with the Global option now having been set to Conservative caching)
* No caching – this module will not be cached.
* No caching – this module will not be cached.
(Note that the Cache Time in the Global Configuration is in minutes but the Cache Time in the Module settings is in seconds.)   
</div>
<span lang="en" dir="ltr" class="mw-content-ltr">(Note that the Cache Time in the Global Configuration is in minutes but the Cache Time in the Module settings is in seconds.)</span>  


To check it's working, go to your site, '''ensure that you are logged out''', and navigate to a web page which displays an article. Check your file system and you should find a folder <tt>cache/com_content</tt> containing a cache file.  
<span lang="en" dir="ltr" class="mw-content-ltr">To check it's working, go to your site, '''ensure that you are logged out''', and navigate to a web page which displays an article. Check your file system and you should find a folder <tt>cache/com_content</tt> containing a cache file.</span>


You'll also find other directories such as <tt>cache/com_languages</tt> (as displaying the page involves loading the current language, and this will be cached as well) and also directories relating to module cache, eg <tt>cache/com_modules</tt>. These result from the use of cache which developers have coded within the Joomla application.  
<span lang="en" dir="ltr" class="mw-content-ltr">You'll also find other directories such as <tt>cache/com_languages</tt> (as displaying the page involves loading the current language, and this will be cached as well) and also directories relating to module cache, e.g. <tt>cache/com_modules</tt>. These result from the use of cache which developers have coded within the Joomla application.</span>


If you edit and save that article, and then refresh the site page you will find that the site displays the updated text this time. This is because whenever the edit is saved, Joomla clears the cache for that article.  
<span lang="en" dir="ltr" class="mw-content-ltr">If you edit and save that article then refresh the site page, you will find that the site displays the updated text this time. This is because whenever the edit is saved, Joomla clears the cache for that article.</span>


However, you can demonstrate that the cache is working if you edit the cache file in the <tt>cache/com_content</tt> directory using a basic text editor. Using the editor change one letter within the article text in the cache file and save the file. Then when you refresh the webpage you should see the update which you made to the cache file.  
<span lang="en" dir="ltr" class="mw-content-ltr">However, you can demonstrate that the cache is working if you edit the cache file in the <tt>cache/com_content</tt> directory using a basic text editor. Using the editor, change one letter within the article text in the cache file and save the file. Then when you refresh the web page you should see the change that you made to the cache file.</span>


How can you select which component views get cached, and under what circumstances? Alas, you can't do this. This is determined by the Joomla core component developers and coded in the component php code. And the criteria are different for each component. However, you can easily discover what criteria are used because for each of the site components they are coded in the site <tt>controller.php</tt> file. For example, at time of writing (Joomla version 3.9.2) for the contacts component we find in <tt>components/com_contact/controller.php</tt>
<span lang="en" dir="ltr" class="mw-content-ltr">How can you select which component views get cached, and under what circumstances? Alas, you can't do this. This is determined by the Joomla core component developers and coded in the component PHP code. The criteria are different for each component. However, you can easily discover what criteria are used because for each of the site components they are coded in the site <tt>controller.php</tt> file. For example, at the time of this writing (Joomla version 3.9.2) for the Contacts component we find in <tt>components/com_contact/controller.php</tt></span>


<pre>
<pre>
Line 73: Line 88:
</pre>
</pre>


This means that the views associated with contacts will be cachable unless there is session data keyed by com_contact.contact.data – which will be the case if in the user session the user has displayed a contact form (eg on a page pointed to by a menuitem of type Contacts / Single Contact).
<span lang="en" dir="ltr" class="mw-content-ltr">This means that the views associated with contacts will be cachable unless there is session data keyed by com_contact.contact.data – which will be the case if in the user session the user has displayed a contact form (e.g. on a page pointed to by a menu item of type Contacts → Single Contact).</span>


The equivalent file for articles <tt>components/com_content/controller.php</tt> contains:
<span lang="en" dir="ltr" class="mw-content-ltr">The equivalent file for articles <tt>components/com_content/controller.php</tt> contains:</span>
<pre>
<pre>
$cachable = true;
$cachable = true;
Line 84: Line 99:
</pre>
</pre>


The expression <tt>$user->get('id')</tt> is true if this is a logged in user, so this means that articles are never cached for logged in users. The subsequent expressions relate to other conditions when the caching is not performed, even if the user is not logged in.  
<span lang="en" dir="ltr" class="mw-content-ltr">The expression <tt>$user->get('id')</tt> is true if this is a logged-in user. This means that articles are never cached for logged-in users. The subsequent expressions relate to other conditions when the caching is not performed, even if the user is not logged in.</span>


So in this way you can discover the circumstances under which caching is performed, but changing these is not advisable.  
<span lang="en" dir="ltr" class="mw-content-ltr">So in this way you can discover the circumstances under which caching is performed, but changing these is not advisable.</span>
You can also demonstrate that modules are being cached by using the Joomla Breadcrumbs module, ensuring it's displayed in some module position on the webpage, setting its Caching option and manually editing the cached file in cache/mod_breadcrumbs.
<div lang="en" dir="ltr" class="mw-content-ltr">
You can also demonstrate that modules are being cached by using the Joomla Breadcrumbs module, ensuring it's displayed in some module position on the web page, setting its Caching option and manually editing the cached file in cache/mod_breadcrumbs.
</div>


==  Progressive Caching ==  
<div lang="en" dir="ltr" class="mw-content-ltr">
Like Conservative Caching, Progressive Caching also caches the output from component views and from modules. The functional difference between the two is that with Progressive Caching '''for logged-off users all modules are always cached'''. In this case setting the No caching option for a module has no effect. If the caching storage option is to File then you can find the modules cache file (the output from all modules is stored within the same file) within the <tt>cache/com_modules</tt> directory.   
==  Progressive Caching ==
</div>
<span lang="en" dir="ltr" class="mw-content-ltr">Like Conservative Caching, Progressive Caching also caches the output from component views and from modules. The functional difference between the two is that with Progressive Caching '''for logged-off users all modules are always cached'''. In this case, setting the ''No Caching'' option for a module has no effect. If the caching storage option is to ''File'', you can find the modules cache file (the output from all modules is stored within the same file) within the <tt>cache/com_modules</tt> directory.</span>  


To switch on progressive caching go to administrator Global Configuration / System and within Cache Settings set System Cache to ON – Progressive caching.
<span lang="en" dir="ltr" class="mw-content-ltr">To switch on Progressive Caching, go to Administrator → System → Global Configuration → System tab and within ''Cache Settings'' set ''System Cache'' to ''ON – Progressive caching''.</span>


As regards the conditions for caching of Joomla core component views '''there is no difference between conservative and progressive caching'''. Despite what you may read on some websites and responses to stack overflow questions, it is not the case that conservative caching relates to when the user is not logged on and progressive caching to when the user is logged on.  
<span lang="en" dir="ltr" class="mw-content-ltr">As regards the conditions for caching of Joomla core component views, '''there is no difference between conservative and progressive caching'''. Despite what you may read on some websites and responses to Stack Overflow questions, it is not the case that Conservative Caching relates to when the user is not logged on and Progressive Caching to when the user is logged on.</span>


== 摘要 ==
== 摘要 ==
Line 100: Line 119:


===頁面快取===
===頁面快取===
*'''Configuration''': Built-in Plugin (Extensions -> Plugin Manager -> System - Page Cache)
<div lang="en" dir="ltr" class="mw-content-ltr">
*'''Configuration''': Built-in Plugin (Administrator → Extensions → Plugin Manager → System - Page Cache)
*'''Caches''': each whole page of your site
*'''Caches''': each whole page of your site
*'''Based on''': URL
*'''Based on''': URL
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
*'''More info''':
*'''More info''':
**Optional browser caching: Also caches on your visitors' browser/computer
**Optional browser caching: Also caches on your visitors' browser/computer
**Only caches pages for guest visitors (not for logged in visitors). Be careful using this plugin if you have an interactive site where you want to server content based on session/cookie information rather than on the plain URL only. Features like a shopping cart will not work.
**Only caches pages for guest visitors (not for logged in visitors). Be careful using this plugin if you have an interactive site where you want to server content based on session/cookie information rather than on the plain URL only. Features like a shopping cart will not work.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
===View Caching===
===View Caching===
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
*'''Configuration''': Global Config -> Cache
*'''Configuration''': Global Config -> Cache
*'''Caches''': each view of a component
*'''Caches''': each view of a component
*'''Based on''': URL, view, parameters, ...
*'''Based on''': URL, view, parameters, ...
*'''More info''': Component developers have to include this in their code to work. Mostly this is not done. The Joomla main content component uses this, but only for guest visitors of your site though this is not obligated for every component.  
*'''More info''': Component developers have to include this in their code to work. Mostly this is not done. The Joomla main content component uses this, but only for guest visitors of your site though this is not obligated for every component.
</div>
===模組快取===
===模組快取===
<div lang="en" dir="ltr" class="mw-content-ltr">
*'''Configuration''': Global Config -> Cache
*'''Configuration''': Global Config -> Cache
*'''Caches''': each module (individually customized via each module's Advanced Parameters)
*'''Caches''': each module (individually customized via each module's Advanced Parameters)
*'''Based on''': the module id, the user's view levels and the Itemid parameter in the HTTP request
*'''Based on''': the module id, the user's view levels and the ''Itemid'' parameter in the HTTP request
*'''More info''': You must disable it on some modules to avoid problems
*'''More info''': You must disable it on some modules to avoid problems
</div>


===進階快取===
===進階快取===
假如您還想要看看關於快取還有什麼其他的可能性,您可以查看第三方擴充套件中關於快取的項目。
假如您還想要看看關於快取還有什麼其他的可能性,您可以查看第三方擴充套件中關於快取的項目。


<div lang="en" dir="ltr" class="mw-content-ltr">
===Caching engines or storages===
===Caching engines or storages===
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
*'''Configuration''': Global Config -> Cache
*'''Configuration''': Global Config -> Cache
Here you can choose which system you want your site to use for all caching. Current options are: APC, Eaccelorator, File, Memcache, Redis, XCache.
Here you can choose which system you want your site to use for all caching. Some options are: APC, Eaccelorator, File, Memcache, Redis, XCache.
</div>


<div class="mw-translate-fuzzy">
例如,APC也會快取您的php opcode
例如,APC也會快取您的php opcode
</div>


=開發者=
=開發者=
class '''JCache''' 允許多種不同種類以及層級的快取。其下的 sub-classes 用於特定的內容,然而您可以添加您自己的,或是在不同的應用方式中,使用主要項目。
<span class="mw-translate-fuzzy">class '''JCache''' 允許多種不同種類以及層級的快取。其下的 sub-classes 用於特定的內容,然而您可以添加您自己的,或是在不同的應用方式中,使用主要項目。</span>


Don't forget that the first level of cache encountered, will override any deeper caching. I suppose that too many levels is also counterproductive (''to be verified though'').
<div lang="en" dir="ltr" class="mw-content-ltr">
*'''JCacheView''' caches and returns the output of a given view (in MVC). A cache id is automatically generated from the URI, specific view and its specific method, or you can give your own.
Don't forget that the first level of cache encountered, will override any deeper caching. I suppose that too many levels is also counterproductive (''to be verified though'').
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
*'''JCacheView''' caches and returns the output of a given view (in MVC). A cache id is automatically generated from the URI, specific view and its specific method, or you can give your own.
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
This can automatically be done via the base controller's display function. For example in the controller of your component:
This can automatically be done via the base controller's display function. For example in the controller of your component:
</div>


<source lang="php">class DeliciousController extends JController {
<source lang="php">class DeliciousController extends JController {
Line 140: Line 181:
}</source>
}</source>


<div lang="en" dir="ltr" class="mw-content-ltr">
There are also some urlparams to consider. Check this [http://joomla.stackexchange.com/questions/5781/how-can-i-use-joomlas-cache-with-my-components-view/7000#7000 "joomla stack"]
There are also some urlparams to consider. Check this [http://joomla.stackexchange.com/questions/5781/how-can-i-use-joomlas-cache-with-my-components-view/7000#7000 "joomla stack"]
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
Also, be aware that any updates (such as hits or visit counts) will NOT be updated (unless you add this outside this method and thus any deeper MVC-part.)
Also, be aware that any updates (such as hits or visit counts) will NOT be updated (unless you add this outside this method and thus any deeper MVC-part.)
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
*'''JCachePage''' caches and returns the body of the page.
*'''JCachePage''' caches and returns the body of the page.
*'''JCacheCallback''' caches and returns the output and results of functions or methods.
*'''JCacheCallback''' caches and returns the output and results of functions or methods.
If you want to cache queries, this is a good class for it, as illustrated here: [[S:MyLanguage/Using_caching_to_speed_up_your_code|Using caching to speed up your code]]
If you want to cache queries, this is a good class for it, as illustrated here: [[S:MyLanguage/Using_caching_to_speed_up_your_code|Using caching to speed up your code]]
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
*'''JCacheOutput''' caches and returns output.
*'''JCacheOutput''' caches and returns output.
This is rather meant for caching a specific part of php code. It acts like an output buffer, but cached.
This is rather meant for caching a specific part of PHP code. It acts like an output buffer, but cached.
</div>


==參考==
==參考==
* [https://forum.joomla.org/viewtopic.php?f=428&t=326990&start=0 Better performance with joomla System Cache plugin (Joomla Forum)]
<div lang="en" dir="ltr" class="mw-content-ltr">
* [https://forum.joomla.org/viewtopic.php?f=428&t=326990&start=0 Better performance with Joomla System Cache plugin (Joomla Forum)]
* [https://api.joomla.org/cms-3/classes/Joomla.CMS.Cache.Cache.html JCache]
* [https://api.joomla.org/cms-3/classes/Joomla.CMS.Cache.Cache.html JCache]
* [https://joomla.stackexchange.com/questions/5781/how-can-i-use-joomlas-cache-with-my-components-view/7000#7000 How can I use Joomla's Cache with my components view? (joomla stackexchange beta)]
* [https://joomla.stackexchange.com/questions/5781/how-can-i-use-joomlas-cache-with-my-components-view/7000#7000 How can I use Joomla's Cache with my components view? (joomla stackexchange beta)]
</div>


<noinclude>
<noinclude>

Revision as of 03:47, 10 June 2022


Joomla 使用不同的方式來快取"內容"。以下是關於管理員以及開發者何時、如何、何處以及有哪些快取的總覽。

系統管理員

作為系統管理員,Joomla 提供了快取功能,您可以選擇全站快取,或是僅快取部分頁面。以下介紹如何進行。

在 Joomla 網站中有三種內容可以被快取:

  1. 整個頁面本身 – 頁面快取
  2. 來自joomla 元件,顯示於該頁面上的輸出 – 我們稱為視覺快取 (View cache)
  3. 來自joomla 模組,顯示於該頁面上的輸出 – 我們稱為模組快取 (Module cache)

您有好幾個快取設定可以讓您控制要建立哪些快取:

  1. 系統外掛 "System – Page Cache"
  2. 全站設定,系統頁籤,快取設定。在此您可以設定系統快取選項
    • OFF – 停用快取
    • ON – 啟用保守快取
    • ON – 啟用累進快取
  3. 在許多模組的設定頁面中,有進階設定頁籤可以設定是否要使用全站設定,或是停用快取

As described below, there are also rules for caching which are implemented within the Joomla code, and over which you have no control.

您可以從管理員選單項目「系統」➞「清除快取」來清除快取。

一般而言,您可以想像成Joomla 有三個層級的快取原則,愈底下愈積極

  1. 保守快取
  2. 累進快取
  3. 頁面快取

接著我們會詳細地來看看這三個項目

此外,joomla開發者可以使用快取機制來儲存...舉例來說,資料庫的檢索結果,以提升網站的反應速度,然而這已經超出系統管理員的範圍了。

頁面快取

To switch this on, go to Administrator → Extensions → Plugins. Then find the System – Page Cache plugin, and enable it. This means that site pages will now be cached and whenever they're requested again, the cached page will be served, rather than it being generated by Joomla from the information in the database. The cached page will continue to be served until it's expired – as defined by the Cache Time parameter in the Administrator → Global Configuration → System tab → Cache Settings.

If you're reading this page as a tutorial and want to test the page caching, it's best to set the Global Configuration cache settings to:

  • Cache Handler – File
  • Path to Cache Folder – leave blank
  • Cache Time – 15 (the default of 15 minutes)
  • Platform Specific Caching - No
  • System Cache – OFF – Caching disabled

To verify that page caching is working, go to a website page that displays an article. After you display that page you should find in the file system a cache/page directory with a file in it which has a filename like <string of hex digits>-cache-page-<string of hex digits>.php. (Joomla has to store separate cache pages for separate URLs so the second string of hex digits is a hash of the URL of the site web page, to make the filename unique to that page).

Then use the Administrator functionality to change the text of that article, and redisplay the site web page. You should find the cached version, not your modified text.

Changing an article (or other Joomla item) does not clear the page cache for the web page(s) where that article is displayed. To clear the page cache go to Administrator → System → Clear Cache. Click on the checkbox next to the Cache Group called "page", and press the Delete button. When you redisplay your web page it should now show your amended text.

If your site has a function like a shopping basket, applying page caching will cause problems, as pages have to show what the customer has already selected, rather than displaying a cached page which is common to everyone. However, you can configure the System - Page Cache plugin to exclude caching specified Menu Items or specified URLs and URL ranges (in the Advanced tab), so that only truly static pages are cached.

保守快取

With Conservative Caching you can cache the View output from components and the output from those Modules which allow caching. But note that this will work only on pages which are not cached using the Page Cache. For those pages the whole web page is cached, and Conservative Caching isn't even considered.

To switch on Conservative Caching:

  1. Go to Administrator → System → Global Configuration → System tab and within Cache Settings, set System Cache to ON – Conservative caching
  2. Go to Administrator → Extensions → Modules and select the modules which you'd like to be cached. If that module permits caching then under the Advanced tab you should be able to set Caching to
  • Use Global – this module will be cached (with the Global option now having been set to Conservative caching)
  • No caching – this module will not be cached.

(Note that the Cache Time in the Global Configuration is in minutes but the Cache Time in the Module settings is in seconds.)

To check it's working, go to your site, ensure that you are logged out, and navigate to a web page which displays an article. Check your file system and you should find a folder cache/com_content containing a cache file.

You'll also find other directories such as cache/com_languages (as displaying the page involves loading the current language, and this will be cached as well) and also directories relating to module cache, e.g. cache/com_modules. These result from the use of cache which developers have coded within the Joomla application.

If you edit and save that article then refresh the site page, you will find that the site displays the updated text this time. This is because whenever the edit is saved, Joomla clears the cache for that article.

However, you can demonstrate that the cache is working if you edit the cache file in the cache/com_content directory using a basic text editor. Using the editor, change one letter within the article text in the cache file and save the file. Then when you refresh the web page you should see the change that you made to the cache file.

How can you select which component views get cached, and under what circumstances? Alas, you can't do this. This is determined by the Joomla core component developers and coded in the component PHP code. The criteria are different for each component. However, you can easily discover what criteria are used because for each of the site components they are coded in the site controller.php file. For example, at the time of this writing (Joomla version 3.9.2) for the Contacts component we find in components/com_contact/controller.php

if (JFactory::getApplication()->getUserState('com_contact.contact.data') === null)
{
	$cachable = true;
}

This means that the views associated with contacts will be cachable unless there is session data keyed by com_contact.contact.data – which will be the case if in the user session the user has displayed a contact form (e.g. on a page pointed to by a menu item of type Contacts → Single Contact).

The equivalent file for articles components/com_content/controller.php contains:

$cachable = true;
if ($user->get('id') || ($this->input->getMethod() === 'POST' && (($vName === 'category' && $this->input->get('layout') !== 'blog') || $vName === 'archive' )))
{
	$cachable = false;
}

The expression $user->get('id') is true if this is a logged-in user. This means that articles are never cached for logged-in users. The subsequent expressions relate to other conditions when the caching is not performed, even if the user is not logged in.

So in this way you can discover the circumstances under which caching is performed, but changing these is not advisable.

You can also demonstrate that modules are being cached by using the Joomla Breadcrumbs module, ensuring it's displayed in some module position on the web page, setting its Caching option and manually editing the cached file in cache/mod_breadcrumbs.

Progressive Caching

Like Conservative Caching, Progressive Caching also caches the output from component views and from modules. The functional difference between the two is that with Progressive Caching for logged-off users all modules are always cached. In this case, setting the No Caching option for a module has no effect. If the caching storage option is to File, you can find the modules cache file (the output from all modules is stored within the same file) within the cache/com_modules directory.

To switch on Progressive Caching, go to Administrator → System → Global Configuration → System tab and within Cache Settings set System Cache to ON – Progressive caching.

As regards the conditions for caching of Joomla core component views, there is no difference between conservative and progressive caching. Despite what you may read on some websites and responses to Stack Overflow questions, it is not the case that Conservative Caching relates to when the user is not logged on and Progressive Caching to when the user is logged on.

摘要

以下是各種類型的快取。

頁面快取

  • Configuration: Built-in Plugin (Administrator → Extensions → Plugin Manager → System - Page Cache)
  • Caches: each whole page of your site
  • Based on: URL
  • More info:
    • Optional browser caching: Also caches on your visitors' browser/computer
    • Only caches pages for guest visitors (not for logged in visitors). Be careful using this plugin if you have an interactive site where you want to server content based on session/cookie information rather than on the plain URL only. Features like a shopping cart will not work.

View Caching

  • Configuration: Global Config -> Cache
  • Caches: each view of a component
  • Based on: URL, view, parameters, ...
  • More info: Component developers have to include this in their code to work. Mostly this is not done. The Joomla main content component uses this, but only for guest visitors of your site though this is not obligated for every component.

模組快取

  • Configuration: Global Config -> Cache
  • Caches: each module (individually customized via each module's Advanced Parameters)
  • Based on: the module id, the user's view levels and the Itemid parameter in the HTTP request
  • More info: You must disable it on some modules to avoid problems

進階快取

假如您還想要看看關於快取還有什麼其他的可能性,您可以查看第三方擴充套件中關於快取的項目。

Caching engines or storages

  • Configuration: Global Config -> Cache

Here you can choose which system you want your site to use for all caching. Some options are: APC, Eaccelorator, File, Memcache, Redis, XCache.

例如,APC也會快取您的php opcode

開發者

class JCache 允許多種不同種類以及層級的快取。其下的 sub-classes 用於特定的內容,然而您可以添加您自己的,或是在不同的應用方式中,使用主要項目。

Don't forget that the first level of cache encountered, will override any deeper caching. I suppose that too many levels is also counterproductive (to be verified though).

  • JCacheView caches and returns the output of a given view (in MVC). A cache id is automatically generated from the URI, specific view and its specific method, or you can give your own.

This can automatically be done via the base controller's display function. For example in the controller of your component:

class DeliciousController extends JController {
	function display() {
		parent::display(true); //true asks for caching.
	}
}

There are also some urlparams to consider. Check this "joomla stack"

Also, be aware that any updates (such as hits or visit counts) will NOT be updated (unless you add this outside this method and thus any deeper MVC-part.)

  • JCachePage caches and returns the body of the page.
  • JCacheCallback caches and returns the output and results of functions or methods.

If you want to cache queries, this is a good class for it, as illustrated here: Using caching to speed up your code

  • JCacheOutput caches and returns output.

This is rather meant for caching a specific part of PHP code. It acts like an output buffer, but cached.

參考