4

I'm trying to make my site faster, because I want it to load as fast as posable. I'm having a bit of trouble with caching though. I was trying to work with this, but it seems that that caches the whole page and my content changes quite a bit. Is it possible for me to only cach certain views that I know will not be changing? Like the header, footer and the main home page.

andrewsi
  • 10,807
  • 132
  • 35
  • 51
zazvorniki
  • 3,512
  • 21
  • 74
  • 122

3 Answers3

4

The point of CI caching is to reduce the number of database queries, any time-consuming PHP calculations, etc... Basically, it will render a plain HTML page from your controller (and all the views it calls, of course). So, it won't really speed up your header and footer, unless you pull the data for them from the database or anythings that dynamic and heavy... but any modern browser will cache that for you unless you specifically disallow caching.

So, the bottom line, CI caching allows caching of the full pages only, no separate parts. Of course, there are alternative ways to achieve what you wanted, for example you can make the header and the footer separate controllers, put caching in them, and call them by AJAX... but I'm not sure it's worth it.

Shomz
  • 37,421
  • 4
  • 57
  • 85
  • Sorry to deviate Abit. Is the cache on or off on CI by default? – Dr3am3rz Jul 15 '20 at 03:02
  • 1
    Oh, it's been 7 years... But the caching is generally off by default: https://codeigniter.com/user_guide/general/caching.html – Shomz Jul 15 '20 at 07:18
  • thank you for your reply. Sorry I forgot to mentioned that I'm asking for codeigniter 4. I guessed all version will be off by default right? – Dr3am3rz Jul 15 '20 at 13:55
  • Yeah, seems like they all follow the same pattern, so it should be off for CI4 as well. Btw. the link I sent you above is for the CI4. – Shomz Jul 15 '20 at 15:33
  • No problem! Techinically, you can always check if caching is on by monitoring what's happening in the cache directory. Good luck! – Shomz Jul 15 '20 at 16:38
2

using extentions this is possible

https://github.com/philsturgeon/codeigniter-cache

usually the displaying isue too much of an issue, its the generation of the data which needs to be displayed which takes longer and benefits most from caching.

exussum
  • 18,275
  • 8
  • 32
  • 65
0

A little late to the conversation, but have you taken a look at database cache? A lot of the web page delay can come from heavy db queries. Caching the results opens up dynamic views.

This is helpful when you are managing sessions.

$this->db->cache_on();

Place this in your model instead of controller. Make sure you have a writable db-cache folder in your app directory as well.

Chris
  • 6,102
  • 5
  • 24
  • 30
  • Yeah, that was the documentation I was linking to. My issue with that is I didn't want to cash the whole page, but rather css files and jquery files I know will not be changing since the content on the page changes quite a bit. – zazvorniki Feb 25 '14 at 21:09
  • Are you creating CSS and JS files dynamically? Generally, they're static files which wouldn't need cacheing. – Chris Feb 26 '14 at 14:31
  • What do you mean by dynamically? I have the sheets there and am using them in the header? It is a good practice to cache them to make the site run faster on the users side. – zazvorniki Feb 26 '14 at 14:52
  • I think we're thinking of different types of cacheing. :-) CI just makes static files to serve, CSS and JS are already static. Does this thread help out? http://stackoverflow.com/questions/2537185/how-to-set-cache-for-css-js-file – Chris Feb 26 '14 at 15:49
  • I actually did solve this issue a few days after I posted the question. :) I found a plug in that both minifies on page load and caches them to the users computer at the same time. :) – zazvorniki Feb 26 '14 at 16:22
  • 1
    @zazvorniki Browsers by default cache all the (external) CSS and JavaScript files after the first load. BTW what was that plugin you used? – Bangash Nov 06 '15 at 18:07