Today I have new article for PHP. Last time I did post about Memcache with PHP. Today we will talking about caching in PHP again. I will show you how you can use Memcache in PHP. We will prepare useful class for working with Memcache for us and several examples. Memcache itself providing procedural and object oriented interface to memcached, highly effective caching daemon, which was especially designed to decrease database load in dynamic web applications.

I don`t have online demo for today, just because haven`t installed Memcache at our hosting. But I will include samples of using our new library in this article too (as I did for our previous APC-related post). Also, pay attention that Memcache extension not bundled with PHP by default. This extension available in PECL. Additional information you can find here

Step 1. PHP

I made this useful class for you. We will use this class to working with memory using Memcache system.

classes/memcache.caching.php

01 <?
02  
03 class CacheMemcache {
04     
05     var $iTtl = 600; // Time To Live
06     var $bEnabled = false; // Memcache enabled?
07     var $oCache = null;
08   
09     // constructor
10     function CacheMemcache() {
11         if (class_exists('Memcache')) {
12             $this->oCache = new Memcache();
13             $this->bEnabled = true;
14             if (! $this->oCache->connect('localhost', 11211))  { // Instead 'localhost' here can be IP
15                 $this->oCache = null;
16                 $this->bEnabled = false;
17             }
18         }
19     }
20  
21     // get data from cache server
22     function getData($sKey) {
23         $vData = $this->oCache->get($sKey);
24         return false === $vData ? null : $vData;
25     }
26  
27     // save data to cache server
28     function setData($sKey, $vData) {
29         //Use MEMCACHE_COMPRESSED to store the item compressed (uses zlib).
30         return $this->oCache->set($sKey, $vData, 0, $this->iTtl);
31     }
32  
33     // delete data from cache server
34     function delData($sKey) {
35         return $this->oCache->delete($sKey);
36     }
37 }
38  
39 ?>

 

I prepared here several necessary functions which we will use: getData, setData and delData. Now, lets check first example file:

index.php

01 <?php
02  
03 $aData = array(
04     'name' => 'table',
05     'color' => 'brown',
06     'size' => array(
07         'x' => 200,
08         'y' => 120,
09         'z' => 150,
10     ),
11     'strength' => 10,
12 );
13  
14 require_once('classes/memcache.caching.php');
15 $oCache = new CacheMemcache();
16  
17 echo 'Initial data: <pre>'; // lets see what we have
18 print_r($aData);
19 echo '</pre>';
20  
21  
22 if ($oCache->bEnabled) { // if Memcache enabled
23  
24     $oCache->setData('my_object', $aData); // saving data to cache server
25     $oCache->setData('our_class_object', $oCache); // saving object of our class into cache server too
26  
27     echo 'Now we saved all in cache server, click <a href="/index2.php">here</a> to check what we have in cache server';
28  
29 } else {
30     echo 'Seems Memcache not installed, please install it to perform tests';
31 }
32  
33 ?>

In this file you can see that I saving 2 objects in memory: some predefined array and class object. Now, lets check second example file:

index2.php

01 <?php
02  
03 require_once('classes/memcache.caching.php');
04 $oCache = new CacheMemcache();
05  
06 if ($oCache->bEnabled) { // if Memcache enabled
07  
08     $aMemData = $oCache->getData('my_object'); // getting data from cache server
09     $aMemData2 = $oCache->getData('our_class_object'); // getting data from cache server about our class
10  
11     echo 'Data from cache server: <pre>'; // lets see what we have from cache server
12     print_r($aMemData);
13     echo '</pre>';
14  
15     echo 'Data from cache server of object of CacheMemcache class: <pre>';
16     print_r($aMemData2);
17     echo '</pre>';
18  
19     echo 'As you can see - all data read successfully, now lets remove data from cache server and check results, click <a href="/index3.php">here</a> to continue';
20  
21 } else {
22     echo 'Seems Memcache not installed, please install it to perform tests';
23 }
24  
25 ?>

Here we only reading data from memory. And, as we see – all data is successfully read from the memory. Now, lets check last example file

index3.php

01 <?php
02  
03 require_once('classes/memcache.caching.php');
04 $oCache = new CacheMemcache();
05  
06 if ($oCache->bEnabled) { // if Memcache enabled
07  
08     $oCache->delData('my_object'); // removing data from cache server
09     $oCache->delData('our_class_object'); // removing data from cache server
10  
11     $aMemData = $oCache->getData('my_object'); // lets try to get data again
12     $aMemData2 = $oCache->getData('our_class_object');
13  
14     echo 'Data from cache server: <pre>'; // lets see what we have from cache server
15     print_r($aMemData);
16     echo '</pre>';
17  
18     echo 'Data from cache server of object of CacheMemcache class: <pre>';
19     print_r($aMemData2);
20     echo '</pre>';
21  
22     echo 'As you can see - all data successfully removed. Great !';
23  
24 } else {
25     echo 'Seems Memcache not installed, please install it to perform tests';
26 }
27  
28 ?>

Вверх