How to Cache ?
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #2
RE: How to Cache ?
It's a matter of design and can vary depending on how you wish to approach it.
Basically, you want to avoid making queries, so your focus would be to investigate ways of:
a) reducing queries, or
b) completely eliminating them

(looking at your code, you may be performing multiple queries whenever there's multiple forums to display, making (a) viable)

Note that you can't generate data from nothing, so it ultimately probably will come from a query, meaning that you should focus on reducing queries.  (b) may be possible if we can leverage existing MyBB queries (ie piggy back our data on them).

So what's causing all the queries?

PHP Code:
1
2
3
4
5
6
7
	$plugins->add_hook("build_forumbits_forum", "ougc_aofl");
...
function ougc_aofl(&$forum)
{
...
		$thread = get_thread(intval($forum['lastposttid']));
		$user = get_user(intval($thread['lastposteruid']));

So for every forum, you're pulling a thread and a user (potentially 2 queries, depending on how MyBB manages to cache it).

One idea which may come to mind is to pull all needed threads and users in a single query, cache the result, then serve from there, eg:

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
static $thread_cache=null;
static $user_cache=null;
if(!isset($thread_cache)) {
  $thread_cache = array();
  $query = $db->simple_select('threads', '*', 'tid IN ('.$list_of_tids.')');
  while($thread=$db->fetch_array($query))
    $thread_cache[$thread['tid']] = $thread;
}
// do the same for $user_cache

...
$thread = $thread_cache[intval($forum['lastposttid'])];
$user = $user_cache[intval($thread['lastposteruid'])];


The above simple modification caches the result so you don't need to perform multiple queries (only 2 queries total on every page).  However, it requires you knowing all the threads and users before making it.


(b) may be possible to do here as well.  MyBB caches information about the last post already, so take a look how it does that, and just extend it to include caching the avatar.
Note that this is a cache-on-update type behaviour, which means you'll have to patch (or trigger on) routines which potentially cause post updates.  That is, you'll probably need to hook into newpost, newthread, deletepost, mergepost etc routines to ensure the cache is updated.
This method is much more difficult, but probably is the best in this situation.


My Blog
03-04-2012 10:55 PM
Find all posts by this user Quote this message in a reply

« Next Oldest | Next Newest »

Messages In This Thread
How to Cache ? - Sama34 - 03-04-2012, 02:55 PM
RE: How to Cache ? - ZiNgA BuRgA - 03-04-2012 10:55 PM
RE: How to Cache ? - Sama34 - 03-06-2012, 02:33 PM
RE: How to Cache ? - Sama34 - 05-20-2012, 01:58 PM
RE: How to Cache ? - ZiNgA BuRgA - 05-20-2012, 03:49 PM
RE: How to Cache ? - leefish - 05-29-2012, 06:08 AM
RE: How to Cache ? - Sama34 - 05-30-2012, 01:43 PM
RE: How to Cache ? - vega - 09-19-2012, 11:52 AM
RE: How to Cache ? - jack3935 - 10-29-2012, 02:25 PM
RE: How to Cache ? - Sama34 - 09-19-2012, 04:29 PM
RE: How to Cache ? - Sama34 - 10-29-2012, 02:48 PM

 Standard Tools
Forum Jump: