Caching templates if they exists.
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #1
Caching templates if they exists.
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.

Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
05-07-2012 01:50 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: Caching templates if they exists.
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.

My Blog
05-07-2012 02:02 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: Caching templates if they exists.
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.).


Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
(This post was last modified: 05-11-2012 01:26 PM by Sama34.)
05-11-2012 01:25 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: #4
RE: Caching templates if they exists.
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.

My Blog
05-12-2012 09:29 AM
Find all posts by this user Quote this message in a reply
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #5
RE: Caching templates if they exists.
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.


Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
05-20-2012 01:57 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: #6
RE: Caching templates if they exists.
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.

My Blog
05-20-2012 03:45 PM
Find all posts by this user Quote this message in a reply
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #7
RE: Caching templates if they exists.
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.

Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
(This post was last modified: 05-21-2012 02:55 PM by Sama34.)
05-21-2012 02:54 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: #8
RE: Caching templates if they exists.
(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.

My Blog
05-21-2012 07:07 PM
Find all posts by this user Quote this message in a reply
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #9
RE: Caching templates if they exists.
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.


Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
(This post was last modified: 05-22-2012 02:23 PM by Sama34.)
05-22-2012 02:19 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: #10
RE: Caching templates if they exists.
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?

My Blog
05-22-2012 05:43 PM
Find all posts by this user Quote this message in a reply

« Next Oldest | Next Newest »

 Standard Tools
Forum Jump: