Welcome Guest, Not a member yet? Register   Sign In
Caching of dynamic pages - or any pages for that matter
#1

[eluser]Flemming[/eluser]
Maybe this isn't the best place for this question/discussion but I use CodeIgniter for all my projects and I've been a fairly active forum user over the years so I thought I'd give it a try...

I know that browser caching is really important for speeding up page loads, reducing the amount of requests to the server, reducing bandwidth etc. BUT ... it's really annoying too! The fact that visitors have to refresh the page to see new content means that often they won't be seeing the latest version of a page.

Is this just the way of the world and that's all there is to it? Is there no way to intelligently have the browser check whether the cached version of the page is now out of date? Is that what the Last-Modified header is for?

I've been playing around with different headers but I haven't found an authorative article on the subject so I would really welcome some input from you guys!!!!
#2

[eluser]jairoh_[/eluser]
caching really depends what type of web application you are making.

it should not be applied in a website that the main goal of the user is to see the latest comments, updates, post or etc.
#3

[eluser]Flemming[/eluser]
after a bit more experimentation, this is what seems to work for me (I see the latest version of the page without having to do a manual refresh)

Code:
$this->output->set_header('Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT');
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");

$this->load->view('my_view);
#4

[eluser]autefrum[/eluser]
Where do you think is the best place to put this code:

Code:
$this->output->set_header('Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT');
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");

I am trying out putting it in the
Code:
function __construct() {
of my controller that
Code:
extends CI_Controller
.

Is there a better place if I don't want to cache ANY of the views loaded by that controller? That controller's model is a fairly dynamic database, and my server can cope with the load of refreshing the views.
#5

[eluser]Flemming[/eluser]
good question! If you want to disable caching on ALL methods in a controller I'm not sure whether putting the headers in the constructor would work -what happens when you do put it in the constructor? If you check the headers using developer tools extension -> view response headers for firefox or chrome you can see whether the no-cache stuff is being applied. Here's what mine look like:

Code:
Pragma: no-cache
Date: Tue, 23 Jul 2013 08:30:22 GMT
Via: 1.1 NEASHMSISA1
Last-Modified: Tue, 23 Jul 2013 08:29:52 GMT
Server: Apache
Age: 0
X-Powered-By: PHP/5.3.20
Vary: Accept-Encoding
Content-Type: text/html
Cache-Control: post-check=0, pre-check=0, max-age=604800
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Expires: Tue, 30 Jul 2013 08:30:22 GMT

200 OK




Theme © iAndrew 2016 - Forum software by © MyBB