📜 ⬆️ ⬇️

Memcached in PHP Kohana and its testing

Much has already been described about memcache, but I suffered before I found the best option for one PHP project, which is quite resource-intensive with a large amount of calculations in Kohana.

Memcache had to be kicked off right away, because when there are a couple of hundred keys, it’s impossible to track down when and which key you need to kill. I looked in the direction of MemcacheTag, where the use of tags for combining several keys was used, but it turned out to be too raw and very inconvenient for work. In the end, the most optimal, in my opinion, option to work with memcached was found.

The description of the principle of operation of this technology is best to look here or in the original source .
')
I will write how to connect and use in the Kohana framework, how to track the 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:

class Cache_Memcacheimp extends Kohana_Cache_Memcacheimp {} 


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

Below is a file to download.

Copy MODPATH / cache / config / cache.php, paste it into the application config and add the following code 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 standard caching elements are installed by default, except for the 'statistics' key that I added for testing. It turns on and off testing our memcached.

Everything can now work.

Method call:

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

where key_tag is a combining tag for several keys

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


Thus, we create a cache in the model:
  public function get($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 kill the cache with the keys combined with 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 is inherited from Cache.

In order to trace the workflow of our caching and understand what keys we create, let's change a little the config:
 'statistics' => TRUE, 


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

file contents:
MLWHHHHH

M - cache is missing
L - key lock is set
H - successful cache request

That is, our cache works great.

Well, the result before caching:

image

Caching is enabled:

image

As you can see, a pretty heavy page with a lot of calls to the database loads in 0.455 seconds. against 1.7 seconds before caching

Download Memcacheimp.php here .

Source: https://habr.com/ru/post/233643/


All Articles