Cakephp Classroom image

pankaj
 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23|Last
Lessons:-Controllers

Apart from using CakeResponse::cache() you can also use many other methods to fine tune HTTP
cache headers to take advantage of browser or reverse proxy caching.
The Cache Control header
New in version 2.1. Used under the expiration model, this header contains multiple indicators which can
change the way browsers or proxies use the cached content. A Cache-Control header can look like this:
Cache-Control: private, max-age=3600, must-revalidate
CakeResponse class helps you set this header with some utility methods that will produce a final
valid Cache-Control header. First of them is CakeResponse::sharable() method, which indicates
whether a response in to be considered sharable across different users or clients or users. This method actually
controls the public or private part of this header. Setting a response as private indicates that all or part
of it is intended for a single user. To take advantage of shared caches it is needed to set the control directive
as public
Second parameter of this method is used to specify a max-age for the cache, which is the number of seconds
after which the response is no longer considered fresh.:
public function view() {
...
// set the Cache-Control as public for 3600 seconds
$this->response->sharable(true, 3600);
}
public function my_data() {
...
// set the Cache-Control as private for 3600 seconds
$this->response->sharable(false, 3600);
}
CakeResponse exposes separate methods for setting each of the components in the Cache-Control header.
The Expiration header
New in version 2.1. Also under the cache expiration model, you can set the Expires header, which according
to the HTTP specification is the date/time after which the response is no longer considered fresh. This
header can be set using the CakeResponse::expires() method:
public function view() {
$this->response->expires(’+5 days’);
}
This method also accepts a DateTime or any string that can be parsed by the DateTime class.The Etag header
New in version 2.1. Cache validation in HTTP is often used when content is constantly changing, and asks
the application to only generate the response contents if the cache is no longer fresh. Under this model, the
client continues to store pages in the cache, but instead of using it directly, it asks the application every time
whether the resources changed or not. This is commonly used with static resources such as images and other
assets.
The Etag header (called entity tag) is string that uniquely identifies the requested resource. It is very much
like the checksum of a file, caching will compare checksums to tell whether they match or not.
To actually get advantage of using this header you have to either call manually
CakeResponse::checkNotModified() method or have the RequestHandlerComponent
included in your controller:
public function index() {
$articles = $this->Article->find(’all’);
$this->response->etag($this->Article->generateHash($articles));
if ($this->response->checkNotModified($this->request)) {
return $this->response;
}
...
}
The Last Modified header
New in version 2.1. Also under the HTTP cache validation model, you can set the Last-Modified header to
indicate the date and time at which the resource was modified for the last time. Setting this header helps
CakePHP respond to caching clients whether the response was modified or not based on the client cache.
To actually get advantage of using this header you have to either call manually
CakeResponse::checkNotModified() method or have the RequestHandlerComponent
included in your controller:
public function view() {
$article = $this->Article->find(’first’);
$this->response->modified($article[’Article’][’modified’]);
if ($this->response->checkNotModified($this->request)) {
return $this->response;
}
...
}
The Vary header
In some cases you might want to serve different contents using the same url. This is often the case when
you have a multilingual page or respond with different HTML according to the browser that is requesting
the resource. For such circumstances, you use the Vary header:

 
 
 

pankaj

Skills    Cakephp

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)