Welcome Guest, Not a member yet? Register   Sign In
Store Model Object In Memory?
#1

[eluser]Eric Brown[/eluser]
My web application creates a profile for visitors. I'd like to store the object in memory so that it doesn't have to be loaded and calculated each time. I'm using Memcached.

Most solutions that I have seen call for storing individual object attributes in memory. For instance using a hashed sql query as a key or another unique identifier I can save a person's name, number of visits, their status as active or inactive. That seems inefficient to me as I would have to run through this check each time the model is loaded at application runtime.


What I would like to do is check to see if the user profile object exists for the user making a request, if not, I would load the model which would kick off the constructor. It would then populate the other attributes and before the app stops executing I'd like to store the entire compiled object in Memcached so that the next request has the information ready to go. How do I go about doing this? Is it possible?

Example logic?
Code:
<?php
class Homepage extends CI_Controller {

function __construct()
    {
            parent::__construct();
            // Load Database
            $this->db = $this->load->database('default',TRUE);
}

function index(){
$this->load->model('Profile','',TRUE);
$profile_key = 'profile_id_' . $this->user_id;
$profile_object = $this->memcache->get($profile_key);
            if(!$profile_object){
                $ths->memcache->add($this->Profile,0,0);
                $this->first_name = $this->Profile->get_first_name($this->user_id);
            }
            else{
                $this->first_name = $profile_object->get_first-name($this->user_id);
            }
echo $this->first_name;
}

}
?>




Theme © iAndrew 2016 - Forum software by © MyBB