I have a question…. why doesn’t EE recognize that your third_party add-on is used within some template and create one instance of it’s class and then re-use it instead of creating it each time one tag is found?
For example, let’s say I have a module called “custom_mod” and it calls these tags throughout the templates:
{exp:custom_mod:list}
{exp:custom_mod:foo}
{exp:custom_mod:scooby}My “custom_mod” constructor would be called three times. Why not just one?
Reason I’m asking is that you can’t really have something load in your constructor to use throughout your entire page request since it’ll be called every time a function within the module is called. Let’s say you want to pick up some configuration from the DB.
Is there a good approach around this dilemma?
Thank you EE-community for listening 😊
Take a look at session->cache, which persists throughout a page load: http://ellislab.com/expressionengine/user-guide/development/guidelines/general.html#use_of_sess_cache
So, let’s say you’re loading settings in your constructor:
// this is just an example
function __construct()
{
$this->EE = get_instance();
if ( ! isset($this->EE->session->cache['my_module']['settings']))
{
$this->EE->db->select('settings')
->from('my_module_table')
->where('site_id', $this->EE->config->item('site_id'));
$query = $this->EE->db->get();
$this->EE->session->cache['my_module']['settings'] = unserialize(base64_decode($query->row('settings')));
}
$this->settings = $this->EE->session->cache['my_module']['settings'];
}Nope, you see, it will only call your DB when the session->cache is not set, which it will only be on the first time around.`
Oh sorry I didn’t notice !isset($this->EE->session->cache[‘my_module’][‘settings’].
That to me seems like a workaround though. I mean if you have a pretty big module you’ll get alot of if’s running just to make these checks. Just a thought I’d guess.
Wanted to hear what you guys prefer 😊
Nope, you see, it will only call your DB when the session->cache is not set, which it will only be on the first time around.`
I just read the documentation again and I see EL actually suggest this approach so I’m guessing that goes(?).
If anyone else have a better idea/solution I would love to hear it. Thanks guys.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.