Say i have a list of categories (or something like that) which i want to display on all pages of a site which a user will visit.
Now my list is stored in my database and although it will/can change, it is very unlikely to change very often. So ideally, i dont want to have to fetch the data from a database everytime the user loads a page. so what is the best way of going about this?
i have come up with the following possible solutions:
1.) Caching.
This is probibly the best way to go about doing it...but unfortunately i dont know much about server side caching and have been too lazy to really get into it (maybe i should).
If anyone would like to give an explanation of caching and an example, that would be nice . Please also note that i dont want to go the PEAR cache route..
2.) Manually (or automatically) updating an included html file when categories are added. -> build this into the CMS / site administration.
3.) Storing the variables in a $_SESSION, so you only read in the list of categories once per users' session. Although obviosly this method will be a bit dodgy for people who dont have sessions enabled in their browsers.
in ASP one can use the application variable for this type of storage...
Im sure im not the first to come across this problem. what is the standard way of doing it? does anyone have any ideas which i might not have mentioned?
Option 2 is probably your best bet. I do something like that with the RSS feeds on my site. Those change maybe once a week, so I created semi-dynamic pages to keep my RSS feeds updated. Just write your content to a file rather than a web page, and your problem is solved.
A fourth option, that a lot of CMS's would use, is to put these in your database. I like your option #2 much better though, if you don't have a separate caching option.
An 'drop-in' caching solution: see http://www.jpcache.com/ - download and follow the 'quick usage' instructions on the front page of their site.
Unless you have thousands of categories, you're probably best off just to leave them in the database and pull them as required. Particularly if you'll be opening a connection to the database anyway.
Unless you want to avoid making a connection to the database at all, and want to cache whole pages without having to reconstruct them every time...in which case I do recommend looking into Smarty or PEAR:Cache, although it's not too hard to roll your own thing.
caching is a generic term which covers many scenarios , 1 of those indeed is item 2 on your list.
like viny-junkie I keep static copies of arrays of such data which are included at runtime , when the data in the DB changes then the script that handles the DB update also rebuilds the included file so there is no need to to anything manually apart from writing the original script.
In some cases the DB update script will also build complete javascript menus etc save building them at runtime.
Bookmarks