Why use cache?

Everything is simple. The retrieval of previously stored information is much faster (10 times or more) than its initial generation. If you want a radical example, a book is a long-term cache of the knowledge of its authors. Accumulating this knowledge can take years, while reading this cache is much faster.

But let's get back to Joomla, and to be more precise, here we are dealing with web application caching - creating temporary static copies dynamic pages(or smaller units of information). Caching is inherent in web applications because a typical website renders the same content over and over again, and without a cache, everything would have to be regenerated for each page view. The caching system temporarily stores code-generated information in a cache object, and gives it back when it is requested by the next user.

Cache Types in Joomla

Page cache

Takes pictures whole page, including everything: components, modules, plugins and a template. This is the fastest yet least flexible caching approach. To activate it, you need to enable the "System - Cache" plugin.

Progressive cache

This type of cache was introduced in Joomla 1.6. It takes snapshots of each unique set of modules (usually each page). Affects all modules and works as a layer above the module cache, overriding their settings. Shows content individually for each visitor. Enabled when the cache level is set to "Progressive" in the general site settings.

Standard cache (Conservative cache)

If you need finer control over each module cache individually, then use the standard caching level. Another difference from progressive cache is that it shows the same cached content to all website visitors.

Module and Component View Cache

They form a kind of group, since both create a static copy of the complete output of a component or module. This is the most common type of cache and is sometimes equated with caching in Joomla in general.

His positive side is that he performs well in terms of speed. And the negative side is that it disables any user interaction<->extension<->framework until the cached copy expires. This means that it is not suitable for components or modules that respond to user actions or display content that changes frequently.

Since the cached copy of a module or component contains only its own output, any external file that is called using methods such as $document->addStyleSheet() will not be included. Various workarounds have been devised, but all of them require additional computing resources, which reduces the effect of using caching.

Component view caching

The component view is cached using the controller's display($cachable, $safeurlparams) method. To do this, the $cachebale parameter must be set to true , and an array of URL parameters and their filter types must be passed through the $urlparams parameter:

$cachable = true; $safeurlparams = array("catid" => "INT", "id" => "INT", "cid" => "ARRAY", "limit" => "UINT", "limitstart" => "UINT", "filter_order" => "CMD", "filter_order_Dir" => "CMD", "filter-search" => "STRING", "lang" => "CMD", "Itemid" => "INT"); parent::display($cachable, $safeurlparams);

In this case, the array of URL parameters forms a unique cache id.

Module cache modes

There are 5 different modes module caching. Three of them are activated via the cachemode field in the module's manifest XML file:

  • static - one cache file will be used for all pages with the same module parameters. Recommended for modules that do not change.
  • itemid - changes when changing the Itemid. This mode is most suitable for dynamic modules that change from page to page, such as menus, content images, etc.
  • oldstatic - backward compatible with Joomla 1.5 mode.

Two more caching modes must be called directly from the module:

  • safeuri - The cache id is generated from an array of URL parameters, just like in a component. Use this mode if the module depends on URL parameters rather than Itemid (for example, a module that displays an image depending on a category). The $cacheparams->modeparams property is an array of URL parameters and their filter types.
  • Id - the module sets its own cache depending on its own formula, which is passed via $cacheparams->modeparams

To use these two modes, instead of the cachemode field, use the owncache field in the manifest XML file:

The object properties that are passed to the moduleCache() method are self-explanatory. An example is the related items module (mod_related_items), which uses the safeuri mode and replaces the non-cached modRelatedItemsHelper::getList($params) function:

$cacheparams = new stdClass; $cacheparams->cachemode = "safeuri"; $cacheparams->class = "ModRelatedItemsHelper"; $cacheparams->method = "getList"; $cacheparams->methodparams = $params; $cacheparams->modeparams = array("id" => "int", "Itemid" => "int"); $list = JModuleHelper::moduleCache($module, $params, $cacheparams);

Callback cache

This type of cache allows different parts of an extension to be differentiated and cached only for those that need to be cached, while leaving the dynamic parts uncached. Caches the results of a function call in code.

Output cache

Caches the output of some part of the script. This is mainly output buffering with caching, and is used quite rarely.

Raw cache

Caches any data elements. Completely controlled by the developer - what to save, when to save and how to classify the saved items (cache id). This type of cache is often used by Joomla core for various operations: list of components, list of modules, menu tree, available languages, user groups, etc.

Workarounds

Workarounds work around known caching limitations, such as the inability to include style files or javascript from a module or component. When using component view cache and cache callback, you can enable or disable certain workarounds. They should only be used when the cached function manipulates the header or path.

setWorkarounds

The JCache::setWorkarounds($data,$options=array()) method prepares the data to be cached along with the workarounds and accepts the following options:

  • nopathway - do not save pathway data
  • nohead - do not save header data
  • nomodules - do not save module data
  • modulemode - if nohead is 0, don't store the following module data: title , description , link , metaTags

getWorkarounds

The JCache::getWorkarounds($data,$options=array()) method applies workarounds on data saved with workarounds. It recreates the header, pathway and modules and returns the main saved data. Works automatically - a particular workaround is only executed if its data has been saved separately.

Difference between using callback cache and raw cache

A view cache is usually sufficient when developing a component, especially for simple components. But if the component creates content on the fly, then it's best to choose a different type of cache. In such a situation, a callback cache or raw cache would be handy.

A function callback cache will return exactly the same result as an uncached function would return, only that result will be cached. The function will be called directly only the first time it is called.

We invoke caching with JFactory::getCache() , and if we use a callback cache, our code should look like this:

$cache = JFactory::getCache("somegroup"); $result = $cache->get(array("someclass", "somemethod"), $methodparams, $id, $workarounds, $workaroundoptions);

The last three parameters in the get() method are optional.

Note that this is not the same get() method that we use in raw-cache. The callback cache automatically fetches the data if it exists. If there are none, then it executes the callback, returns the data from the callback, and stores it until the next call. The callback cache does not have a store() method!

When using a raw cache, the developer has direct access to get methods() and store() , and the developer is responsible for the logic. This cache is useful when you need to save information, but not a function call (for example: xml data, pictures, product descriptions, etc.), or when you need to transfer a large amount of data from one page to another.

Below is the code for using the raw cache:

$cache = JFactory::getCache("somegroup", ""); if (!$somevariable = $cache->get("cacheId")) ( // perform actions and store the result in $somevariable // store $somevariable in the cache $cache->store($somevariable, "cacheId"); )

To use the raw cache, we must pass an empty string as the second parameter to the getCache() method.

What happens if several caches work at the same time?

The cache types work as opaque layers on top of each other (excluding the component's module/view cache, which runs in parallel). To understand this, we must imagine that we are looking at a web page from above, where the page cache represents the top level, while the callback, output, and raw caches represent the bottom level. It turns out that the upper levels overlap the lower ones, and for example, when using the page cache, the rest of the cache ceases to operate.

But there is an exception when we use different times for different types cache. For example, we can cache pages for a short amount of time and cache images for a long time. In this case, when rendering the page, the image will be pulled from the cache.

Cache Handlers

Cache handlers are laborers. It is they who perform all the rough work and save information on the selected media. To use the cache, at least one cache handler must be available.

JotCache is free component caching Joomla, which can speed up the site and reduce server load. In the previous article "Overview of JotCache. Caching Joomla 3 » we reviewed the main features of the extension, the general “algorithm” for setting it up, got acquainted with the tests and results of the site with / without JotCache, and began a more detailed review of its interface. In this article, we'll talk about exclusions of "elements" of the site from caching and about the settings of the Joomla cache plugin.

Joomla 3.6 has some improvements in terms of the cache system: Joomla's one-button cache flush and reverse caching support. But even with this in mind, JotCache has much more features, especially in terms of fine tuning and solving caching issues for dynamic pages and site elements.

Exclude URL

In chapter Exclude URL(picture below) JotCache allows you to enable or disable caching of pages created by certain components. Please note that in the settings of the JotCache system plugin you set what this section is for - to be included in caching or excluded from it (Plugin tab, option URL caching section). In connection with this setting, the section may be called Include URL. Then all the rules specified in it will mean that these pages need to be included in the Joomla cache. In the future, when I write “exclude”, it is understood that it is possible to include it, depending on the plugin settings.

It is not necessary to exclude caching of the entire component. In my example, Ccomment Pro comments and SEF component sh404SEF are completely excluded from the AJAX cache. Ccomment Pro excluded due to a problem with duplicating pages, but sh404SEF excluded due to the fact that when a non-existent page was requested for the first time, the server returned a 404 response, and on subsequent requests for this non-existent page - 200, since the page with the wrong URL got into Joomla cache.

You can exclude from caching not all pages of a component, but its individual pages, the URL of which contains certain queries. For example, this was done for caching JoomShopping 4 (figure below). To do this, in the column Viewing Exceptions and Query Options opposite "jshopping" query parameters were added separated by commas: "controller=cart,controller=user,controller=checkout,controller=wishlist,controller=search" (without quotes). Plus, exclude the template position where the cart is placed.


For correct caching of VirtueMart 3, you need to set "cart,user,orders,askquestion,invoice,pluginresponse,state" as exceptions. Plus, exclude the template position where the cart is placed.

Query parameters can be viewed in the URL of the page where the cache issue occurs. If you have CNC enabled, or disable them to see non-CNC URLs, or on the tab Review in JotCache, click on the eye icon next to the "problem" page.

In the 404 page and sh404SEF example, setting the query parameter exceptions to "error404" is sufficient.

Exclude position

If your site uses modules whose content is critical to caching, for example, the currency switcher module for online stores, then in this section(Figure below) you can exclude from caching the position of the template, in which there is a similar module.

All of the caching discussed so far is server-side caching. It significantly increases the download speed and reduces the load on the server (by reducing the page generation time).

But caching in the browser on the client side can significantly increase the speed of user access to the page, and without loading the site. Pages cached in the browser are not loaded from the site server. Such a cache is only acceptable for very static pages. For example, company information, payment and delivery terms, and so on. The bottom line is that after visiting such a page, the user's browser will not contact the site's server to receive this page for the entire lifetime of the browser cache that you set in the JotCache settings on the tab Basic. There is an exception - the user cleared their browser's cache or forced a page refresh.

For this option to work in the JotCache plugin settings on the tab option must be enabled Browser caching (see popup!).

Considering that in reality very few pages of sites can be cached in the browser, the following principle works in JotCache: do not cache pages in the browser if their address or part of the address is not specified in the section Enable browser cache(picture below).


JotCache caching plugin

Most of the settings are collected in the JotCache system plugin. The settings are divided into five tabs.

  1. Plugin.
  2. Description. There is a brief text description of JotCache and a link to the help system on the developer's website (on English language).
  3. Cache modes.

Let's look at these tabs in more detail. Let's dwell only on those options that, in my opinion, will be useful to most webmasters. Let me remind you that the options are equipped with tooltips in Russian and extended English-language documentation is available for them.

plugin

On this tab (picture below), you can set the server cache lifetime (option Caching Time), activate Joomla browser cache (option Browser cache lifetime) and enable automatic cache cleaning.

JotCache allows you to use different modes how the Joomla cache works depending on the browser/device (figure below). This can be useful for resolving conflicts in the display of a site. Especially in the case of Internet Explorer.

  • Exception. The site will load without using the cache.
  • General. The content of the page for this type of browser is stored in one shared cache.
  • Individual. Page content is stored in its own dedicated cache storage.

Of particular note is the option Exclude bots. By activating it, you force the return search engines site pages without using the cache. This helped me get rid of the problems of duplicating the content of pages that have AJAX comments. If search robots do not create a tangible load on your site, then, in my opinion, it is better to give them the most "fresh" content not from the cache.

JotCache allows you to store cache files both directly on your hosting's hard or SSD drive - "File", and in random access memory servers - "Memcache" or "Memcached" (picture below). To be able to use "Memcache" or "Memcached", you need to have such an opportunity on your server / hosting. If you have a VPS or a dedicated server, you can install and configure desired module.

If you're hosting on SSDs, I don't think you'll see much of a difference between File caching and Memcache or Memcached. But for hosting owners on regular discs there may be positive differences.

When I tested "Memcached" caching, not in Joomla 3.5.1 and not with JotCache, but with the standard Joomla cache, I did not notice any speed improvement or load reduction. On the contrary, the speed has decreased. The amount of consumed memory (RAM) has increased and the load on the processor has increased. This happened as a result of the introduction of a new "Memcached" process. At first I suggested that perhaps I simply did not configure the Memcached module on the server itself. Or my VPS (2x2.8 GHz processor, 1024 MB RAM, SSD) is not enough to see positive result. In addition, the load on the server / site is not very large, and storing the cache in a file on an SSD is already good (compared to "regular" hard drives). Some time later, in the article “Memcached and PHP, educational program” found an explanation: “... it is worth using caching only on highly loaded resources. After all, every time you connect to the Memcached server, you waste precious time, which most likely will not be justified. … Also, don't forget about memory usage! Keep in mind that by putting 300 megabytes in the cache, you took away 300 megabytes of RAM from yourself ... "


On the tab (picture below) you can enable the cache of the Joomla browser (the option of the same name).

Browser caching (see popup!). If this option is set Yes, then JotCache will use the browser's cache storage mechanism. Use very carefully. In the JotCache component, on the page Browser cache lifetime, You can designate which pages of the site should be cached in the browser. Selected pages are cached in the user's browser without being reloaded from the site server for the duration of the browser's cache expiration. This expiration time can be set separately for each given URL. The default cache expiration time can be set in the component settings.

Clean up edited pages. Select Yes to automatically remove edited pages from the cache. Users who edit content from the front of the site must have at least permissions to create.

Exclude URLs containing query. All pages that have a query in their URL (the part of the URL after the "?") will be excluded from caching. Enable this option only when the option Enable SEF (CNC) in the general settings of Joomla is set Yes.

JS and CSS integration. Allows you to configure JotCache integration with JCH Optimize , Rokbooster or Scriptmerge website speed optimization components to correctly cache optimized pages and clear their cache.

If you decide to install one of the mentioned site loading speed optimization components, first disable caching, configure the optimization component, check everything. If you are satisfied with the result, enable caching. Integration allows you to get rid of possible problems compatibility.

As you can see JotCache, unlike standard system caching, allows you to very finely tune the Joomla cache, achieving an increase in performance, reducing the load on the server and retaining the functionality and appearance site. I note that for projects that use a large amount of dynamic content, the setup process can be difficult, it will take a lot of time and careful study of the documentation. With more dynamic site content, the effect of caching is less noticeable.

  • Official website of the developer (in English).
  • Documentation (in English).

From all of the above we understand that enabling caching helps in speeding up Joomla. But what if we regularly add materials to the site, but no one sees changes and additions of new articles, because all pages are taken from the cache? There are two solutions to this problem:

1. You can manually delete the cache from your site after each update. To do this, go to the administrative panel of your site and find the tools tab at the top right and click "Clear cache":

Now select all the elements whose cache you want to clear and click "Delete" at the top right:

In addition, it is also desirable to delete the "Obsolete Cache" to delete those entries that are no longer relevant. To do this, go to "Tools">>"Delete obsolete cache" and in the window that opens on the right click on the button "Delete obsolete cache":

2. The second way is more convenient and reasonable. In the general settings, you enable caching yourself and set the cache lifetime in minutes. The cache lifetime should be set depending on how often changes occur on your site (for example, articles are added, etc.). If you write one article a day, then the lifetime can be set to 1440 minutes (24 hours). After this time, the cache will be deleted on its own and all added articles will become visible to users. To enable the cache, go to "Site">>"General settings">>"System" and there on the right side of the screen find "Cache settings":

Automatic clear cache joomla will greatly simplify your life and help you avoid unnecessary conversations with your server about a heavy load.

Caching Joomla Modules

Your site may have modules installed that it would not be desirable to cache, for example, the "Most read" module. After all, the information in this module can change much more often than new articles are added, so we need to turn off caching in this module. To do this, go to the administrative panel of your site, go to "extensions">> "module manager", select the desired module and go to its settings. On the right side, we look for the "Advanced Options" tab and disable caching in it or set a shorter cache lifetime for more frequent updates of information in this module.

In this article, we'll look at " Cache settings" in the "General Settings" of the Joomla 3 admin panel, which are located on tab « System» . These settings allow you to save site pages once requested by users and give them immediately from a file, rather than making requests to the database every time. This significantly reduces the load on the server and allows you to quickly give content to users. A properly configured cache is a guarantee fast work site and one of the tools for SEO-promotion of the site in the top search queries.

In order to understand how the "Cache Settings" look like in the Joomla admin panel, let's look at the picture below:

"Cache settings" in Joomla 3 admin panel

"Cache settings" in the Joomla admin panel consists of several fields, the values ​​​​of which can be configured:

  • Field " caching handler» - Select the caching method. The standard caching mechanism is file-based. Please make sure the cache file directories are writable.
  • Field " Path to cache directory» - Please specify a folder to store the cache files.
  • Field " Caching Time» - Maximum lifetime of cache files, in minutes. After this time, the cache will be updated.
  • Field " Platform specific caching» - Enable or disable platform-specific caching. Enable when HTML output on mobile differs from other devices. (Disabled by default)
  • Field " System cache» - Enables or disables caching, as well as determines its level. Standard level: Smaller system cache, Progressive level (default): Faster, Larger system cache because it includes module caching. Not suitable for very large sites.

To make changes, you need to change the data in these fields and use the "Save" or "Save and close" button in top row control buttons of the "General settings" of the admin panel. To leave General settings without making changes, you can use the "Cancel" button.

CMS Joomla has the ability to cache materials, which allows you to reduce the number of queries to the database, and therefore - speed up the generation of pages and reduce the load on the server. This is especially important for sites with high traffic.

The principle of operation is approximately the following: when you first access the material, it is not only generated and transmitted to the browser of the user who requested it, but also stored in a specific folder on your site (cache folder). The next time you access the same material, it will no longer be re-generated, taking server resources, but simply taken from the cache.

The question is brewing: "But what about updating the site then? After all, the user will not notice the changes that we have made, because the outdated information from the cache is displayed to him?"

Exactly. The user will see the changes made only after a certain time, which is called caching time. This is the time during which the cached information is stored. It is then deleted and cached again.

It is very important to choose the correct cache lifetime. The key factor here is the frequency of information updates. If, for example, you update the information on the site once a day, then it would be reasonable to set the caching time to a little less than 24 hours, for example - 22 hours (1320 minutes). For the remaining couple of hours, it is quite possible to have time to update the materials.

If you make changes once every two days, set the time to 46 hours (2760 minutes). In general, I think the meaning is clear.

If you suddenly need to make an unscheduled update, and you need the changes to take effect immediately, you can simply clear the cache manually.

To enable caching, go to the admin panel, select SITE - GENERAL SETTINGS - SYSTEM. On the right side you can see the cache settings.

Set the radio switch to "Yes" and set the caching time. Do not forget to click on the "Save" button for the changes to take effect :)

In this case, we asked the so-called. global options, i.e. enabled caching for the entire site. However, for some modules, it is better not to use it at all. These can be modules that display frequently updated information, such as news, weather, etc.

Go to EXTENSIONS - MODULES MANAGER - click on the one you need, and get into the settings. We are interested in the Parameters item (on the right).

As you can see, you can either use the global settings, or not use the cache at all for a particular module.

To manually clear the cache, go to TOOLS - CLEAR CACHE

Summary: caching technology can significantly speed up the Joomla site and improve its performance. When using this technology, a situation may arise when a visitor sees updates on the site with a certain delay. To prevent this from happening, you need to choose the right caching time. The main criterion for choosing this time is the frequency of updating the site. For some modules, for example, various informers, it is better to disable caching altogether.