In this post I would to share how to install memcached caching system in amazon ec2 cloud server and access it via php codeigniter framework. We can store any data like plain text, json, array & html contents into the server memory.

Stored  values  can be accessible by using  keys .

Memcached in Codeigniter

About Memcached

High-performance, distributed memory object caching system, intended for use in speeding up dynamic web applications.

The use of memcached is to speed-up the websites by using the memory space of our unused domains and store the database query results in the cache and minimize the application-database interaction.

Intall Memcached in Amazon EC2

Below are the configuration used to install memcached in ubuntu and Redhat or CentOS servers in Amazon EC2. There are two memcache extenstions are available. Memcache and Memcached  –  we are going to use Memcached since it is stable and it has more features.

Ubuntu Server

Run the below code in your server to install the latest version of memcached.

sudo apt-get update 
sudo apt-get install memcached

RedHat or CentOS Server

sudo yum update
sudo yum install memcached

check the memcache installation by using below commands

memcached -h 
//output memcached 1.4.5

//start the memcached server 
service memcached start

If everything is fine then cacheing system is running fine in your server. You can also check the installation from  phpinfo() 

Download Codeigniter

Download the codeigniter latest stable framework from here – https://codeigniter.com/

Once you download the CI files and setup everything, just create a new controller and load the cache driver in it as belows

//to load the cache driver 
$this->load->driver('cache');

How to Check Memcached is Intalled in Codeigniter?

After you loaded the cache driver, you need check memcache is installed it or not by using the below code

$memcached_enabled = $this->cache->memcached->is_supported();
if(!$memcached_enabled) 
{ 
 echo "Memcached is not installed"; 
 die; 
}

Next we will see how to stored and retrive values from Memcache in Codeigniter

Storing and Retriving Values – Memcache

How to Store Data in Memcached?

By using  save  function, we can save any values in the key. Save function comes with 4 parameters

 save($id, $data, $ttl = 60, $raw = FALSE) 

$id – Cache ID (Name of the cache)

$data – Data being cached

$ttl – Time to Live (Expiration time) – default is 60 seconds

$raw – Whether to store the raw value

lets see how to store data in memcache now.

$name = "W3lessons"; 
$this->cache->memcached->save('website_name', $name, 86400);

I have just store the value “W3lessons” in the key “website_name” with 1 day expiration time.

If you want to store an array or json, just store it directly as below

//to store an array
$details = array(
      'sitename'=>'W3lessons',
      'siteurl'=> 'https://w3lessons.info',
      'author'=>'Karthikeyan K'
    );
$this->cache->memcached->save('website_details', $details, 86400);

//to store an JSON - convert the array into json
$details_json = json_encode($details);
$this->cache->memcached->save('website_details_json', $details_json, 86400);

How to Retrive / Get data in Memcached?

By using the “key” (cache name) we can get the data from memcache.

$this->cache->memcached->get('website_name');
//output
W3lessons

For the 1st time, we need to query the database and store it in the memcache, 2nd time onwards we need to check the data exists in the memcache and If exists, get it from cache and display. So your application will get the data always from the cache until it expires.

How to Check data already exists in Memcache?

 Get  function is used to check the data exists in memcache or not. If data not exists in cache, then we need to store it again in the memcache.

Get function returns data on success and false on failure

if($this->cache->memcached->get('website_name'))
{
  $name = "W3lessons";
  $this->cache->memcached->save('website_name', $name, 86400);
}

How to Delete the data in Memcache?

You have to use  delete  function to delete the data from memcached. Delete function returns true on success and false on failure

$this->cache->memcached->delete('website_name');

Please feel free ask me anything about memcached in codeigniter via comments, I’m ready to share my knowledge with you since I have more than 6 years of experience in Codeigniter, Apache, Nginx and Amazon Web Services

Please download the Sample Controller about Memcached in Codeigniter

 

You may also like my other posts on Codeigniter

Related Posts