We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Question about re-using your module class throughout one page request

Development and Programming

esset's avatar
esset
191 posts
15 years ago
esset's avatar esset

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 😊

       
Rob Sanchez's avatar
Rob Sanchez
335 posts
15 years ago
Rob Sanchez's avatar Rob Sanchez

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'];
}
       
esset's avatar
esset
191 posts
15 years ago
esset's avatar esset

Yeah but every time I would call a function in my module that DB call will fire.

So if I call three functions, like above, my settings will be pushed into cache three times with 3 mySQL calls.

       
Rob Sanchez's avatar
Rob Sanchez
335 posts
15 years ago
Rob Sanchez's avatar Rob Sanchez

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.`

       
esset's avatar
esset
191 posts
15 years ago
esset's avatar esset

… or maybe extensions are design they way to work with modules so if you need this behaviour you would bundle your module with an extension that handles all the “common” stuff your module would need at the session_end hook?

Thoughts?

       
esset's avatar
esset
191 posts
15 years ago
esset's avatar esset
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 😊

       
esset's avatar
esset
191 posts
15 years ago
esset's avatar esset
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.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.