发新话题
打印

Symfony缓存技术(Caching)<未完成>

Symfony缓存技术(Caching)<未完成>

[注:转载注明PHP开发资源网(http://www.phpres.com)]

       储存PHP生产的HTML代码块乃至整个页面,供以后直接使用,是加快应用程序处理速度的常见方法之一。这就是缓存技术,可以由服务器端和客户端的操作分别实现。 One of the ways to speed up an application is to store chunks of generated HTML code, or even full pages, for future requests. This technique is known as caching, and it can be managed on the server side and on the client side.

      symfony提供一套灵活的服务器端缓存系统。对YAML文件进行的简单直观的设置,就允许将整个页面,动作或局部模板的响应结果,以及模板片断保存到文件中。底层数据改变时,可有选择性得删除部分缓存。删除部分缓存的操作可以用命令行和特殊的动作方法实现。symfony也提供一套简单的方法实现客户端缓存,该方法利用了HTTP头。本章将讲述这些内容,介绍如何监控缓存带来的性能提升。

       Symfony offers a flexible server-caching system. It allows saving the full response, the result of an action, a partial, or a template fragment into a file, through a very intuitive setup based on YAML files. When the underlying data changes, you can easily clear selective parts of the cache with the command line or special action methods. Symfony also provides an easy way to control the client-side cache through HTTP 1.1 headers. This chapter deals with all these subjects, and gives you a few tips on monitoring the improvements that caching can bring to your applications.

TOP

响应的缓存Caching the Response

HTML缓存的原理很简单:由一个请求产生的HTML代码,可以部分或者全部被另一个类似的请求重复使用,这部分HTML代码被保存在一个特殊的目录(在symfony中为cache/目录)。前端控制器在执行动作之前在此目录搜索已有的代码。如果找到缓存的代码版本,则不再执行动作,因此可以大大得提高执行速度。如果没有找到,则执行动作并将其结果(视图)保存在缓存目录,以后后续使用。 The principle of HTML caching is simple: Part or all of the HTML code that is sent to a user upon a request can be reused for a similar request. This HTML code is stored in a special place (the cache/ folder in symfony), where the front controller will look for it before executing an action. If a cached version is found, it is sent without executing the action, thus greatly speeding up the process. If no cached version is found, the action is executed, and its result (the view) is stored in the cache folder for future requests.

由于所有的页面都包含动态信息,HTML缓存默认是关闭的。网站管理员可以将其打开,以提升性能。 As all the pages may contain dynamic information, the HTML cache is disabled by default. It is up to the site administrator to enable it in order to improve performance.

symfony处理三种不同类型的HTML缓存:
动作缓存 (包括或者不包括布局)
局部模版,组件或组件槽缓存
模板片断缓存

Symfony handles three different types of HTML caching:
Cache of an action (with or without the layout)
Cache of a partial, a component, or a component slot
Cache of a template fragment

前两种类型在YAML配置文件中设置,模板片断缓存由模板的辅助函数处理。 The first two types are handled with YAML configuration files. Template fragment caching is managed by calls to helper functions in the template.

TOP

全局缓存设置 Global Cache Settings
HTML缓存机制可以针对项目的每个应用程序的环境,在settings.yml文件中,分别设为开启或关闭(默认为关闭)。开启缓存如列表 12-1
For each application of a project, the HTML cache mechanism can be enabled or disabled (the default), per environment, in the cache setting of the settings.yml file. Listing 12-1 demonstrates enabling the cache.
列表 12-1 - 开启缓存 (myapp/config/settings.yml)
Listing 12-1 - Activating the Cache, in myapp/config/settings.yml
dev:
  .settings:
    cache:                  on

TOP

动作缓存 Caching an Action
显示静态信息以及从数据库读取信息的动作,都很适于缓存。显示静态信息的动作与数据库和会话无关,数据库读取信息的动作,只读取而不修改数据库,如GET请求。图 12-1 表明了这一情况下,动作的结果或者包括其布局都将被缓存。
Actions displaying static information (not depending on database or session-dependent data) or actions reading information from a database but without modifying it (typically, GET requests) are often ideal for caching. Figure 12-1 shows which elements of the page are cached in this case: either the action result (its template) or the action result together with the layout.
图 12-1 - 动作缓存 Figure 12-1 - Caching an action



例如,user/list(用户列表)动作返回网站的所有用户列表。除非用户被修改,添加或删除,否则列表将保持不变,因此很适宜缓存。 For instance, consider a user/list action that returns the list of all users of a website. Unless a user is modified, added, or removed (and this matter will be discussed later in the "Removing Items from the Cache" section), this list always displays the same information, so it is a good candidate for caching.
每个动作的缓存开启,在模块的config目录下的cache.yml文件中。举例如列表12-2: Cache activation and settings, action by action, are defined in a cache.yml file located in the module config/ directory. See Listing 12-2 for an example.
列表 12-2 - 开启动作缓存 (myapp/modules/user/config/cache.yml) Listing 12-2 - Activating the Cache for an Action, in myapp/modules/user/config/cache.yml
list:
  enabled:     on
  with_layout: false   # 默认值
  lifetime:    86400   # 默认值
上述配置开启了list动作的缓存,布局不随动作一起被缓存(默认)。也就是说,即使动作缓存版本存在,布局(包括其局部模板和组件)仍然要执行。如果with_layou被设为true,布局将被缓存而不再重复执行。 This configuration stipulates that the cache is on for the list action, and that the layout will not be cached with the action (which is the default behavior). It means that even if a cached version of the action exists, the layout (together with its partials and components) is still executed. If the with_layout setting is set to true, the layout is cached with the action and not executed again.
测试缓存设置,从浏览器中调用开发环境的list动作: To test the cache settings, call the action in the development environment from your browser.
http://myapp.example.com/myapp_dev.php/user/list

TOP

请勿回贴。未完待续,谢谢支持。

TOP

发新话题