Memcached in PHP Kohana and its testing

It has already been described a lot about memcache, however, I tormented myself before I found the best option for one PHP project, quite resource-intensive with a lot of calculations in Kohana.

Memcache had to kick off right away, since when a couple of hundred keys hit it, it's unrealistic to track when and which key needs to be killed. I looked towards MemcacheTag, where tags were used to combine several keys, but it turned out to be too raw and very inconvenient for work. In the end, the most, in my opinion, optimal option for working with memcached was found.

A description of the principle of operation of this technology is best seen here or in the original source .

I’ll write how to connect and use Kohana in the framework, how to track cache keys and actually test how caching works in the project.

So, let's begin:

In bootstrap, uncomment 'cache' => MODPATH.'cache ', add the file MODPATH / cache / classes / Cache / Memcacheimp.php
like this:

classCache_MemcacheimpextendsKohana_Cache_Memcacheimp{}


Add the file MODPATH / cache / classes / Kohana / Cache / Memcacheimp.php.

Below is the file to download.

Copy MODPATH / cache / config / cache.php, paste it into application config and add the following code there, as the last element of the array:

'memcacheimp' => array(
		'driver'             => 'memcacheimp',
		'default_expire'     => 3600,
		'compression'        => FALSE,              
		'servers'            => array(
			'local' => array(
				'host'             => 'localhost', 
				'port'             => 11211, 
				'persistent'       => FALSE,  
				'weight'           => 1,
				'timeout'          => 1,
				'retry_interval'   => 15,
				'status'           => TRUE,
			),
		),
		'instant_death'      => TRUE,  
		'statistics'      => FALSE,
	)


where the standard default elements for caching are set, except for the 'statistics' key, which I added for testing. It turns on and off the testing of our memcached.

Everything can now work.

Method call:

Model_SomeClass::factory('table')->get($id, 'key_tag');

where key_tag is the unifying tag for several keys.

Create an object:
$this->cache = Cache::instance('memcacheimp');


Thus, we create a cache in the model:
publicfunctionget($id, $tags = null){
	$cOne = $this->cache->get("get_{$this->_table}_{$id}");
	if (!is_array($cOne)) 
	{
		$query = DB::query(Database::SELECT, "SELECT * FROM $this->_table WHERE id='$id'");
		$cOne = $query->execute()->as_array();
		$this->cache->set("get_{$this->_table}_{$id}", $cOne, array($tags));
	}
   return $cOne;
}


So we kill cache with keys united by the key_tag tag:
$this->cache->delete_tag('key_tag');


To kill the entire cache or delete by key, we use the standard cache methods, since our class inherits from Cache.

In order to trace the process of our caching and understand what keys are being created, we’ll change the config a bit:
'statistics'      => TRUE,


Now we can see what happens with our cache. Open APPPATH / cache / statistics / and look at the contents of the get_someTable_someId.txt file, where get_someTable_someId is the name of our key.

file contents:
MLWHHHHH

M - no cache
L - key lock set
H - successful cache request

That is, our cache works fine.

Well, the result is before caching:

image

Caching is enabled:

image

As you can see, a pretty heavy page with many hits to the database loads in 0.455 seconds. vs 1.7 sec before caching

Download Memcacheimp.php here .

Also popular now: