📜 ⬆️ ⬇️

Symfony

Love this framework.
I will translate some of the information as it is read. I think that it will be useful not for me alone. In turn, I propose to discuss and supplement all those to whom this topic and framework is not indifferent

Chapter 12 - Caching


One of the ways to speed up a web-based application is to memorize some or all of the HTML document, for re-issuing it with the following requests. This technology is known as caching. It can be used both on the server side and on the client side.

Symfony offers flexible server-side caching. Allows you to save complete server responses, the result of any actions, or a template fragment to a file described in accordance with the YAML format (http://www.yaml.org/). When the data is updated, you can clear part of the cache via the command line or special methods (for example, I assume that we are talking about the methods specifically reserved for this purpose in ramework). Symfony also offers easy client-side caching using HTTP 1.1. (http://www.w3.org/Protocols/)This chapter will cover the caching capabilities mentioned above, provide hints for practical use in your applications.
')
Response caching

The principle of HTML caching is simple: a part or all of the HTML code that is sent in response to the user to his request can be used many times with similar requests. HTML code for this purpose is stored in a special place ( cache / folder). Before processing the request from the client to the server, the controller first tries to look at this particular folder for the presence of data. If the cached data is found, they will be sent without performing additional actions by the server (approx. Statics), thus significantly reducing the load on the server's CPU time. But if the data is not found, the handler of the triggered event will work, generate the data for the response and write it to the cache / folder. In the future, the whole operation is repeated.

By default, the cache is turned off. To use it, you must have administrator rights. (?)

Symfony supports three types of caching:
1. Event Cache
2. Partial cache of components
3. Template snippet cache

The first two types of caching are managed using YAML files. The caching of template fragments is controlled by auxiliary (?) Template functions.

Global cache settings

For each application in the project (in the context of symfony), the HTML caching mechanism can be applied or not (the cache is disabled by default) by defining instructions in the settings.yml environment.

Here is an example showing the activation of the caching mechanism:
frontend / config / settings.yml
dev:
[tab] .settings:
[tab] [tab] cache: [tab] [tab] [tab] [tab] on


[approx. learn YAML syntax]

Event caching

Events that display static information (exclude dynamically generated data from the database and session dependencies) or actions to read non-changing information from the database (usually GET requests) are ideal for caching. In a visual form (image 12-1) it is shown which page elements are cached, depending on the case: the result of the action (this is a template) or the action of the result along with the format.

Images 12-1 - Event Caching


For example, consider the user / list event, which returns all site users. If there are no modifications, deletions or additions () with user data, as well as this information is often displayed on the site, then this is the case when you need to think about caching.

Enabling and configuring the cache for events is defined in cache.yml , which is located in the directory of the config module . See for example Listing 12-2

Listing 12-2
list:
[tab] enabled: [tab] on
[tab] with_layout: [tab] false [tab] # Default value
[tabulation] lifetime: [tabulation] 86400 [tabulation] # Default value


to be continued…

Source: https://habr.com/ru/post/31650/


All Articles