Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^244) CHAPTER 16 ■ ADVANCED ZEND FRAMEWORK
The example in Listing 16-8 uses the page front-end and the file back-end to cache the
entire output of the page. The page front-end takes an array with regular expressions specifying
which pages to cache. Here, you used the default caching options, which say not to cache
requests with variable information for gets, posts, cookies, sessions, and so on.
■Caution By default, the existence of cookies, sessions, and get and post variables will prevent caching!
If you have cookies being sent, then your cache may appear not to work. A common problem you may
encounter is the use of analytics tracking codes that set cookies that are otherwise outside your application.
To ensure that your cache is created when variables are present, you can use a variety of
cachewith_variables and make_idwith_variables options. For example, if you wanted
to cache the result of a GET request, you would use a cacheFrontendOptions array, as shown
Listing 16-9.
Listing 16-9. Caching GET Requests
$cacheFrontendOptions = array(
'debug_header' => true,
'regexps' => array(
'^/$' => array(
'cache_with_get_variables' => true,
'make_id_with_get_variables' => true
)
)
);
The option cache_with_get_variables says that it is OK to cache a page when get variables
are present. The option make_id_with_get_variables says that each page cached must be cached
separately for all of the page’s inputs. Thus, two values for the same get variable result in two
different cache files.
■Caution Enabling make_id_with_cookie_variables and make_id_with_session variables may
have the result of creating a per-user cache. This could be less than desirable, depending on your caching
scenarios. You can use the cachewith_variables option without using make_idwith_
variables, but be careful, because doing so may expose user-specific data to other users. It is critical to
always understand what data your cache will store.
You will also notice the debug_header option is set to true in Listing 16-9. This option
makes the cached page output a header indicating that it has been cached, so that you can tell
when your pages are being cached, which may aid in debugging.
McArthur_819-9C16.fm Page 244 Friday, February 29, 2008 5:07 PM

Free download pdf