MyBB Hacks

Full Version: How to Cache ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I wrote a plugin, the problem is that iw will do the queries each time the page is visited.

The plugin shows the users lastpost avatar of the laspost in forum lists.

Can somebody, taking into account I have never cached something, explain me how to do it to increase performance? As I want to expand it for threads avatars and so on.

Thanks.

Here is the finished version:
http://mods.mybb.com/view/ougc-avatar-in-forum-list
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:
	$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:
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.

Thanks Zinga, will careful read your post and check all available options.
And what about his?

PHP Code:
		static $forums_avatar_cache = null;
		if(!isset($forums_avatar_cache))
		{
			global $db, $cache;
			$forums = $cache->read('forums');

			$uidlist = '0';
			foreach($forums as $key => $val)
			{
				$f = $forums[$key];
				if($f['type'] == 'f' && $f['active'] == 1 && $f['lastposteruid'])
				{
					$uidlist .= ','.intval($f['lastposteruid']);
				}
			}

			$query = $db->simple_select('users', 'uid, avatar, avatardimensions', "uid IN ({$uidlist})");
			while($user = $db->fetch_array($query))
			{
				$forums_avatar_cache[$user['uid']] = $user;
			}
		}


It will make one query each page load in index, and forum display (but less code because of $tids and considering it gets the latest thread poster avatar too) is used.

I tried merging my data with the forum cache but it was to many hooks, too many code, and too many troubles to be worth it for such a feature.

Maybe I will ask for a review Smile

Well if it does what you want, then there you go.
Hi Sama34 - I downloaded this from the mods site - but it has a small bug and I am not sure how it could be fixed. On Index the last post avatar is always the post of the last poster in the parent forum. This means if there are subforums showing on index then the avatar does not match the last poster name.
Hi Leefish, the mod in the mybb mods site is outdated (I will make a query by forum), I have hided it just right now. I will check the bug you mentioned before releasing the coming version, thanks.
This is a very cool plugin. I've been wanting something like this on my boards for  a while really impressive work. I was curious about the layout of the avatars? Is there a way to make them flush with the text? This is what it looks like when installed.
[Image: 2_19_09_12_1_34_19.png]

But how can I make it look like this?
[Image: 2_19_09_12_1_41_28.png]

and this would be for the index and thread display
thanks! and great work!
It is up to you to edit the code as you like, there is a template for the avatar plus the tempaltes where the variable is inserted.
(09-19-2012 11:52 AM)vega Wrote: [ -> ]This is a very cool plugin. I've been wanting something like this on my boards for  a while really impressive work. I was curious about the layout of the avatars? Is there a way to make them flush with the text? This is what it looks like when installed.
[Image: 2_19_09_12_1_34_19.png]

But how can I make it look like this?
[Image: 2_19_09_12_1_41_28.png]

and this would be for the index and thread display
thanks! and great work!

check

http://www.visionarysoft.com if u like i will give tamplet code
Pages: 1 2
Reference URL's