How to Cache ?
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #1
How to Cache ?
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

Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
(This post was last modified: 11-15-2012 03:38 PM by Sama34.)
03-04-2012 02:55 PM
Visit this user's website Find all posts by this user Quote this message in a reply
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
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #3
RE: How to Cache ?
Thanks Zinga, will careful read your post and check all available options.

Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
03-06-2012 02:33 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #4
RE: How to Cache ?
And what about his?

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
		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


Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
05-20-2012 01:58 PM
Visit this user's website Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #5
RE: How to Cache ?
Well if it does what you want, then there you go.

My Blog
05-20-2012 03:49 PM
Find all posts by this user Quote this message in a reply
leefish Offline
Hamster
*****
Posts: 1,009
Joined: Apr 2010
Post: #6
RE: How to Cache ?
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.


[Image: leelink.gif]
MYBB1.6 & XThreads
(This post was last modified: 05-29-2012 06:09 AM by leefish.)
05-29-2012 06:08 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #7
RE: How to Cache ?
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.

Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
05-30-2012 01:43 PM
Visit this user's website Find all posts by this user Quote this message in a reply
vega Offline
Junior Member
**
Posts: 3
Joined: Sep 2012
Post: #8
RE: How to Cache ?
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!
09-19-2012 11:52 AM
Find all posts by this user Quote this message in a reply
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #9
RE: How to Cache ?
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.

Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
(This post was last modified: 09-19-2012 04:30 PM by Sama34.)
09-19-2012 04:29 PM
Visit this user's website Find all posts by this user Quote this message in a reply
jack3935 Offline
Junior Member
**
Posts: 7
Joined: Oct 2012
Post: #10
RE: How to Cache ?
(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
10-29-2012 02:25 PM
Find all posts by this user Quote this message in a reply

« Next Oldest | Next Newest »

 Standard Tools
Forum Jump: