Implementing Memcache in Codeigniter
About Memcache :
How does it Work?
Before start implementing this,
First need to check whether the memcache is enabled in the web server or not. If it is not enabled, then enable it by uncomment the ‘extension=php_memcache.dll‘ in php.ini file in the server
Next, download the memcache executable file to install in server. Download link – http://code.jellycan.com/files/memcached-1.2.6-win32-bin.zip
Unzip the downloaded file, u will get a memcached executable file. Store that in a web server.
Then open the command promp and run this command c:/xampp/htdocs/memcached.exe -d install
then run ‘c:/xampp/htdocs/memcached.exe -d start‘
Download the cache driver from here – http://d45jz936mo8n8.cloudfront.net/wp-content/uploads/2010/01/Cache.php_1.zip
Unzip and place the cache.php in system/libraries/cache.
Create an empty folder named cache on the location system folder.
Load the Cache driver in controller codeigniter application
Add the following lines of code where needed to cache the query result.
$cache = $this->cache->get(‘cache_data’);
$data = $this->cache->get(‘cache_data’);
$data = $this->sample_model->get_data();
$this->cache->save(‘cache_data’, $data, NULL, 3600);
While accessing the webpage, it will query the database for the first time, then the result will be stored in cache upto the refreshing time given in the save() function above.
So until the time exceeds, whenever the user refreshes the page, the application will not interact with the database. instead it will retrieve from cache and display.
This caching is applicable only for the select queries and the static updates.