Codeigniter Caching Driver Library


Codeigniter Caching Driver Library – This library provide various functions that are used to store, update and delete a value of variable in cache. $this->load->driver(‘cache’); is used to load the library. Here in this tutorial, we are going to explain how to use caching driver library.


Codeigniter caching driver class library.

Let us first see how to load caching driver library and then use its function-

Load codeigniter caching driver library

How to load codeigniter caching driver library:

 
$this->load->driver('cache');

Functions:-

There are following functions available in caching driver library. Now we will explain one by one with example.

  • 1. Create cache
  • 2. Delete cache
  • 3. Info cache
  • 4. increment cache
  • 5. Decrement cache
  • 6. Clean cache
  • 7. Metadata cache

1. Create cache

Syntax of create cache function is

Syntax of create cache function is:-

get($id)
$this->cache->get('id');

    Parameters :-

  • $id (string) : Cache item name
  • Returns : Item value or FALSE if not found
  • Return type : Mixed

EXAMPLE

Here is simple example of create cache.

Create cache in codeigniter example:-

//Controller
public function createCache()
	{
	   $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));

		if ( ! $text = $this->cache->get('myVariable'))
		{
				echo "Caching Demo<br />";
				$text = 'Welcome to tutorialsplane.com!';
				// Save into the cache for 5 minutes
				$this->cache->save('myVariable', $text, 300);
		}

		echo $text;
	}

When we will run this program. File will be create in cache folder.

The output will be like this –

Caching Driver library

2. Delete cache

Syntax of delete cache function is

Syntax of delete cache function is:-

delete($id)
$this->cache->delete('id');

    Parameters :-

  • $id (string) : Cache item name
  • Returns : TRUE on success, FALSE on failure
  • Return type : Bool

EXAMPLE

Here is simple example of delete cache.

Delete cache in codeigniter example:-

//Controller
public function deleteCache()
	{
      $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
	  $del = $this->cache->delete('myVariable');
	  if($del==true){
		  echo "Deleted succesfully";
	  }

When we will run this program. File will be delete from cache folder.

The output will be like this –

Caching Driver library

3. Info cache

Syntax of info cache function is

Syntax of info cache function is:-

cache_info()
var_dump($this->cache->cache_info());

    Parameters :-

  • Returns : Information on the entire cache database
  • Return type : Mixed

EXAMPLE

Here is simple example of info cache.

Info cache in codeigniter example:-

//Controller
public function getInfo()
	{
		$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
		echo "<pre>";
		var_dump($this->cache->cache_info());
		
	}

This function will return all information about cache file which is placed in cache folder.

The output will be like this –

Codeigniter Caching Driver Library

4. Increment cache

Syntax of increment cache function is

Syntax of increment cache function is:-

increment($id[$offset = 1])

    Parameters :-

  • $id (string) : Cache ID
  • $offset (int) : Step/value to add
  • Returns : New value on success, FALSE on failure
  • Return type : Mixed

EXAMPLE

Here is simple example of increment cache.

Increment cache in codeigniter example:-

//Controller
public function getIncrement()
	{
		$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
		echo $this->cache->increment('TutorialsPlane');
		
		echo $this->cache->increment('SolidCoupon', 10);
		
	}

The output will be like this –

Caching Driver library

5. Decrement cache

Syntax of decrement cache function is

Syntax of decrement cache function is:-

decrement($id[$offset = 1])

    Parameters :-

  • $id (string) : Cache ID
  • $offset (int) : Step/value to reduce by
  • Returns : New value on success, FALSE on failure
  • Return type : Mixed

EXAMPLE

Here is simple example of decrement cache.

Decrement cache in codeigniter example:-

//Controller
public function getDecrement()
	{
		$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
		echo $this->cache->decrement('TutorialsPlane'); 
		echo $this->cache->decrement('SolidCoupon', 2);
		
	}

The output will be like this –

Caching Driver library

6. Clean cache

Syntax of clean cache function is

Syntax of clean cache function is:-

clean()

    Parameters :-

  • Returns : TRUE on success, FALSE on failure
  • Return type : Bool

EXAMPLE

Here is simple example of clean cache.

Clean cache in codeigniter example:-

//Controller
public function cleancache()
	{
		$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
		$this->cache->clean();	
	}

This method will clean all cache folder

7. Metadata cache

Syntax of metadata cache function is

Syntax of metadata cache function is:-

get_metadata($id)

    Parameters :-

  • $id (string) : Cache item name
  • Returns : Metadata for the cached item
  • Return type : mixed

EXAMPLE

Here is simple example of metadata cache.

Metadata cache in codeigniter example:-

//Controller
public function getmetadata()
	{
		$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
		var_dump($this->cache->get_metadata('myVariable'));
		
		
	}

This method will return all information about cache file in array format.

The output will be like this –


Advertisements

Add Comment

📖 Read More