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

[eluser]al pacino[/eluser]
hi there,
I have implemented Database caching in my web application.
I've created writable folder in my application directory & set path in database.php,
also set the parameter $db['default']['cache_on'] = TRUE;
in the same file.

Now,when i sign up on my site,index function from controller named "homepage" is called.
So,in cache folder,i can see folder named "homepage+index" being created,with number of files.

But,next time when i hit the same URL no difference,it takes same amount of time to load the page as it was taking before without cashing.

I checked this by Enabling the Profiler ,number of queries is also not reduced.So,how does cashing exactly work in boosting performance of web application.
#2

[eluser]jalalski[/eluser]
It's possible that the database itself is handling caching faster than a file based system could.
For example, when MySQL has cached a query in memory and you call the same query again, it is retrieved from memory.
Whereas, in your case, you are opening a file, reading it then closing the file. This may take as long, or longer, than retrieving the result from MySQL's memory cache.

Just an idea...
#3

[eluser]al pacino[/eluser]
If the database itself is handling caching faster than a file based system could,then what is purpose of using "Database Caching Class"?
"When caching is enabled, the first time a web page is loaded, the query result object will be serialized and stored in a text file on your server. The next time the page is loaded the cache file will be used instead of accessing your database. Your database usage can effectively be reduced to zero for any pages that have been cached."
It is given in codeigniter user guide.
When I opened these cache files,i can see result object stored in it.So,this is working fine.
But,next time when page loads i don't think it is using cache files.Because,changes made to the database are reflecting on that web page,which should show me the old data stored in cache files.
Thanks for the reply...
#4

[eluser]xwero[/eluser]
Did you add a cache deletion function somewhere?
#5

[eluser]al pacino[/eluser]
No,i did not add cache deletion function.
I know it will delete old file & rewrite the file with new data when web page is again loaded.

But when,I checked this by Enabling the Profiler,I can see the query s for which i have used caching, executed once again.

I guess,cache file will not be updated once it's created.Unless i delete it using cache delete function.
So,how come i can see updated data on my web page?
#6

[eluser]freakylp[/eluser]
I think that there is no need of this type of caching at all.
The only one reason is to reduce persistant connections.

The best alternative is memcache.I wrote class which is using memcache to store SQL results.
If there is someone interested I will post some source.

And remember that HDD speed is a lot of slower than RAM. // sorry for my bad english


CI query caching works as fast as ordinary query to db.
Memcache works 10 times faster Smile
#7

[eluser]Shrike67[/eluser]
[quote author="freakylp" date="1231924306"]The best alternative is memcache.I wrote class which is using memcache to store SQL results.
If there is someone interested I will post some source.[/quote]

I'm really interested :lol:
#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.
#9

[eluser]bikuta[/eluser]
is it possible to store the table in the database instead of single queries? because my queries will be dynamically changing and so I don't think this method will be as effective as having the actual table stored in memcache.
#10

[eluser]whobutsb[/eluser]
@freakylp Thanks for posting that memcached library. I'm going to be using it and I think its going to help out my applications tremendously!

I just have two questions is there any way to use your Memcache script with CIs Active Record Class. Like if I wanted to write a model like this:

Code:
$this->db->select('*');
$query = $this->db->get('tblCustomers');
if($this->memcached->get($query) == false){
$q = $this->db->query($query);
$this->memcached->set($query, $q->result());
return $q->result();
}
return $this->memcached->get($query);

I keep getting the error from memcached script :
Quote:Severity: Warning

Message: md5() expects parameter 1 to be string, object given

Filename: libraries/Memcached.php

Line Number: 33

Also lets say I updated my a customer record and I wanted to skip the memcache. I see there is magic word variable in your script. How do I use this?

Thank you for the help!




Theme © iAndrew 2016 - Forum software by © MyBB