MyBB Hacks

Full Version: Caching templates if they exists.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Well, I asked for this some days ago in the official support community, and nobody answered.

Somebody can help me in this?
http://community.mybb.com/thread-117605.html

I want to cache templates only if they exists, as there may be a lot of them.

Thinking about it right now, may be reading the groups cache at global start and caching a template by each group is the solution.

Nonetheless, hope somebody push me a little in this one.
Just add it to the templatelist directly.  Don't worry about checking if it exists beforehand.  To do that, you'd need to query, so you're not really saving processing time performing the check (in fact, the opposite).  Loading templates is pretty fast, so just do it.

In theory, you could dynamically construct a template list in the ACP and cache it somewhere, but I don't think it's worth the effort.
XThreads just pulls all the templates across regardless of whether they exist.


Or if you can narrow things down a lot later on, just use your own $templates->cache() call, if you think it's worth it.

For regex, you'd have to manually query the database.
Yes, the thing is that there could be a template by each group, so unless I can use something like template_name_%GID% I will need to add a big list, like:

Code:
template_name_1, template_name_2, template_name_3, template_name_4, template_name_5, template_name_6, template_name_7, template_name_8, template_name_9, ...


I suppose I will need to cache only some list, and if users add to much groups to their forums, they will need to edit the file (or maybe a setting to add extra templates, now that the plugin already use settings.).

You do realise that $templatecache isn't the only way to approach things, right?
You can use $templates->cache function too (though will invoke an extra query).

For groups, you can simply loop over the groups cache during global_start and build the list there - there's no need to hard code in a long list.
Thanks Zinga, I use that method to cache one template by each gid reading the cache using $tempaltelist.

What I was really looking for was this:

PHP Code:
if($templates->cache['template_name_'.$gid])
{
	eval("\$val[$gid] = \"".$templates->get('template_name_'.$gid)."\";");
}


So that there will be no queries if template doesn't exists (wasn't cached at 'global_start').

Thanks.

You need to query regardless of whether it exists or not.

You can only know its existence through a query, so you may as well load it all in one.

If you can't figure it out, perhaps think of approaching your problem in a different way.
I think I'm lost now. If I add the list of templates to the $templatelist at "global_start", those templates will be cached if they exists in the DB, right?

Then, the code above check if the template exists when running at "member_profile_end", because if I just eval the variable it will make the same check with the difference of a extra query to attempt (again) to get it from the DB.

The idea was to add my template list at global_start, and then eval() the variable only if the template was cached there, otherwise the variable was suppose to do nothing.
(05-21-2012 02:54 PM)Sama34 Wrote: [ -> ]I think I'm lost now. If I add the list of templates to the $templatelist at "global_start", those templates will be cached if they exists in the DB, right?
Yes.

It may help if you expand on what you're trying to do more.
Ok, I will try.

What my plugin does is show all additional usergroup images in profile, just like any other plugin already does, using the {$memprofile['agi']}.

The thing is, admins can put {$memprofile['agis']['1']}, {$memprofile['agis']['5']}, etc..

{$memprofile['agis']['1']} is suppose to work just like {$memprofile['agi']} but using the "member_profile_groupimage_1" template for GoupID = 1 (guest image).

If template "member_profile_groupimage_1" was no cached at global_start it means it was not created by the admin (doesn't exists), since the plugin doesn't create those additional templates automatically.


But if I do:

PHP Code:
eval("\$memprofile['agis'][$gid] = \"".$templates->get('member_profile_groupimage_'.$gid)."\";");


MyBB will try to re-get the template (since it was not cached at global_Start because it doesn't exists) and if it was not cached it will make a query to get it from the DB.

The thing is, we already know it doesn't exists, because, again, it was no cached at global_start, so I want to avoid MyBmaking that extra query.

Seems like a weird way to approach the problem.
One would imagine that you don't specify arbitrary groups - for additional groups, don't you grab the list of user groups from the user, not a template?

Or, do you even really need to use templates?  What about sticking an extra field in the groups table for it?
Pages: 1 2
Reference URL's