Welcome Guest, Not a member yet? Register   Sign In
Database Caching Class
#8

[eluser]freakylp[/eluser]
Code:
load this to library using autoload
<?php



    class Memcached{
        
        
        
        public $memcache;
        
        
        
        
        
        public function __construct()
        {
            $this->memcache     =     new Memcache;
            $this->conn         =     $this->memcache->connect('localhost', 11211);
            $this->time            =    300; //cache time in seconds
            $this->method_stop    =    "add_comment";//dont cache query if you have in uri this
        }
        
        
        
        
        
        
        public function set( $key , $value  )
        {
            $this->memcache->set( md5( $key ).$_SERVER['SCRIPT_FILENAME'] , $value , 0 , $this->time );
        }
        
        
        
        
        public function get( $key )
        {
            return $this->memcache->get( md5( $key ).$_SERVER['SCRIPT_FILENAME'] );
        }
        
        
        
    }




?>
extend Model using this:
Code:
<?php




    class My_Model extends  Model{
        
        
        
        public function __construct()
        {
            parent::__construct();
            
        }
        
        
        
        
        
        
        
        
        public function dbquery( $query )
        {
            if( $this->memcached->get( $query ) == false )
            {
                $this->load->database();
                    
                
                $q            =    $this->db->query( $query );
                $this->memcached->set( $query , $q->result()  );
                
                return $q->result();
                
            }
            return $this->memcached->get( $query );
        }
        
        
        
        
    }




?>
dont autoload database library // it will be loaded if cache expire

And thats it.
You can create config file for memcache settings.
It doesnt work with active_record queries.
It works with only sql queries.


Messages In This Thread
Database Caching Class - by El Forum - 12-19-2008, 06:27 AM
Database Caching Class - by El Forum - 12-19-2008, 07:41 AM
Database Caching Class - by El Forum - 12-22-2008, 02:53 AM
Database Caching Class - by El Forum - 12-22-2008, 03:49 AM
Database Caching Class - by El Forum - 12-22-2008, 04:17 AM
Database Caching Class - by El Forum - 01-13-2009, 09:11 PM
Database Caching Class - by El Forum - 01-15-2009, 03:09 AM
Database Caching Class - by El Forum - 01-15-2009, 04:01 AM
Database Caching Class - by El Forum - 03-14-2009, 03:13 PM
Database Caching Class - by El Forum - 05-08-2009, 08:02 AM
Database Caching Class - by El Forum - 05-08-2009, 08:30 AM



Theme © iAndrew 2016 - Forum software by © MyBB