[limb-svn] r6915 - in 3.x/trunk/limb/cache: src tests tests/bench tests/cases

svn at limb-project.com svn at limb-project.com
Thu Apr 10 22:35:05 MSD 2008


Author: vasiatka
Date: 2008-04-10 22:35:05 +0400 (Thu, 10 Apr 2008)
New Revision: 6915
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6915

Added:
   3.x/trunk/limb/cache/src/lmbCacheFileWithMetaBackend.class.php
   3.x/trunk/limb/cache/tests/bench/
   3.x/trunk/limb/cache/tests/bench/bench.php
   3.x/trunk/limb/cache/tests/cases/lmbCacheFileWithMetaBackendTest.class.php
Log:
-- lmbCacheFileWithMetaBackend with tests added

Added: 3.x/trunk/limb/cache/src/lmbCacheFileWithMetaBackend.class.php
===================================================================
--- 3.x/trunk/limb/cache/src/lmbCacheFileWithMetaBackend.class.php	                        (rev 0)
+++ 3.x/trunk/limb/cache/src/lmbCacheFileWithMetaBackend.class.php	2008-04-10 18:35:05 UTC (rev 6915)
@@ -0,0 +1,183 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+lmb_require('limb/cache/src/lmbCacheBackend.interface.php');
+lmb_require('limb/core/src/lmbSerializable.class.php');
+lmb_require('limb/fs/src/lmbFs.class.php');
+
+/**
+ * class lmbCacheFileBackend.
+ *
+ * @package cache
+ * @version $Id$
+ */
+class lmbCacheFileWithMetaBackend implements lmbCacheBackend
+{
+  protected $_cache_dir;
+
+  function __construct($cache_dir)
+  {
+    $this->_cache_dir = lmbFs::normalizePath($cache_dir);
+
+    lmbFs :: mkdir($this->_cache_dir);
+  }
+
+  function getCacheDir()
+  {
+    return $this->_cache_dir;
+  }
+
+  function add($key, $value, $params = array())
+  {
+    $file = $this->getCacheDir() . '/' . $this->_getCacheFileName($key, $params);
+    if (file_exists($file))
+      return false;
+
+    if(array_key_exists("ttl", $params))
+    {
+    	$meta=array('ttl' => $params['ttl']+time());
+    	$this->_setMetaData($key,$meta);
+    }
+
+    if (array_key_exists("raw", $params))
+    {
+      lmbFs :: safeWrite($file, $value);
+      return true;
+    }
+    else
+    {
+      $container = new lmbSerializable($value);
+      lmbFs :: safeWrite($file, serialize($container));
+      return true;
+    }
+  }
+
+  function set($key, $value, $params = array())
+  {
+    $this->delete($key);
+
+    $file = $this->getCacheDir() . '/' . $this->_getCacheFileName($key);
+
+   if(isset($params['ttl']))
+    {
+    	$meta=array('ttl' => $params['ttl']+time());
+    	$this->_setMetaData($key,$meta);
+    }
+
+    if (array_key_exists("raw", $params))
+    {
+      lmbFs :: safeWrite($file, $value);
+      return true;
+    }
+    else
+    {
+      $container = new lmbSerializable($value);
+      lmbFs :: safeWrite($file, serialize($container));
+      return true;
+    }
+  }
+
+  function get($key, $params = array())
+  {
+    if (!$file = $this->_findCacheFile($key))
+      return false;
+
+    if($meta=$this->_getMetaData($key) and isset($meta['ttl']))
+    {
+    	if ($meta['ttl'] - time() < 0)
+        return false;
+    }
+
+    if (array_key_exists("raw", $params))
+    {
+      return file_get_contents($file);
+    }
+    else
+    {
+      $container = unserialize(file_get_contents($file));
+      return $container->getSubject();
+    }
+  }
+
+  function delete($key, $params = array())
+  {
+    $this->_removeFileCache($key);
+  }
+
+  function flush()
+  {
+    $this->_removeFileCache();
+  }
+
+  function stat($params = array())
+  {
+    return array();
+  }
+
+  protected function _removeFileCache($key = false)
+  {
+    if($key === false)
+    {
+      $files = lmbFs :: find($this->getCacheDir(), 'f');
+      foreach($files as $file)
+        @unlink($file);
+    }
+    else
+    {
+      @unlink($this->getCacheDir()."/".$this->_getCacheFileName($key));
+      $this->_removeFileMeta($key);
+    }
+  }
+
+  protected function _findCacheFile($key)
+  {
+  	$file=$this->getCacheDir()."/".$this->_getCacheFileName($key);
+    if(file_exists($file))
+    {
+      return $file;
+    }
+    else
+      return false;
+  }
+
+  protected function _getMetaData($key)
+  {
+    $file=$this->_getMetaFilePath($key);
+    if(file_exists($file))
+    {
+      $data = unserialize(file_get_contents($file));
+      return $data;
+    }
+    else
+      return false;
+  }
+
+  protected function _setMetaData($key,$data)
+  {
+  	$file=$this->_getMetaFilePath($key);
+ 	  lmbFs :: safeWrite($file, serialize($data));
+  }
+
+  protected function _removeFileMeta($key = false)
+  {
+    if($key)
+      @unlink($this->_getMetaFilePath($key));
+  }
+
+  protected function _getMetaFilePath($key)
+  {
+  	$file=$this->getCacheDir()."/".$key.".meta";
+  	return $file;
+  }
+
+  protected function _getCacheFileName($key)
+  {
+    return $key. '_' .'.cache';
+  }
+
+}

Added: 3.x/trunk/limb/cache/tests/bench/bench.php
===================================================================
--- 3.x/trunk/limb/cache/tests/bench/bench.php	                        (rev 0)
+++ 3.x/trunk/limb/cache/tests/bench/bench.php	2008-04-10 18:35:05 UTC (rev 6915)
@@ -0,0 +1,145 @@
+<?php
+
+set_include_path(dirname(__FILE__) . '/../../../../');
+
+require_once('limb/core/common.inc.php');
+require_once('limb/cache/common.inc.php');
+require_once('limb/cache/common.inc.php');
+lmb_require('limb/cache/src/lmbCacheFileBackend.class.php');
+lmb_require('limb/cache/src/lmbCacheFileWithMetaBackend.class.php');
+
+$cache_my=new lmbCacheFileWithMetaBackend(dirname(__FILE__)."/cache1");
+$cache=new lmbCacheFileBackend(dirname(__FILE__)."/cache2");
+
+$cache->flush();
+$cache_my->flush();
+
+echo "All digits is seconds per reading\n";
+
+echo "Reading Same Variable from small cache \n";
+
+$var= array(rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand());
+$cache_my->set('foo',$var);
+$cache->set('foo',$var);
+
+$mark = microtime(true);
+$n=5000; //number of readings
+for($i=0;$i<$n;$i++)
+{
+  $d=$cache_my->get('foo');
+}
+$t1=(microtime(true) - $mark)/$n;
+echo "lmbCacheFileWithMetaBackend: " . $t1 . "  ";
+
+$mark = microtime(true);
+for($i=0;$i<$n;$i++)
+{
+  $d=$cache->get('foo');
+}
+$t2=(microtime(true) - $mark)/$n;
+echo "lmbCacheFileBackend: " . $t2 . "  second/first=".$t2/$t1."\n";
+
+$cache->flush();
+$cache_my->flush();
+
+echo "Reading Same Variable from big cache \n";
+
+$n=500;// number of riadings and files in cache
+$m=10; // number different experiments
+
+
+for($i=0;$i<$n;$i++)
+{
+  $var= array(rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand());
+  $cache_my->set($i,$var);
+  $cache->set($i,$var);
+}
+
+$sredn1=0; $sredn2=0;
+for ($j=0;$j<$m;$j++)
+{
+  $key=rand(0,$n);
+
+  $mark = microtime(true);
+  for($i=0;$i<$n;$i++)
+  {
+    $d1=$cache_my->get($key);
+  }
+  $t=microtime(true) - $mark; $sredn1+=$t;
+  $mark = microtime(true);
+  for($i=0;$i<$n;$i++)
+  {
+    $d2=$cache->get($key);
+  }
+  $t=microtime(true) - $mark; $sredn2+=$t;
+}
+echo "lmbCacheFileWithMetaBackend: " . $sredn1/$m/$n . "     ";
+echo "lmbCacheFileBackend: " . $sredn2/$m/$n . "\n";
+echo "second/first=".$sredn2/$sredn1. "\n";
+$cache->flush();
+$cache_my->flush();
+
+echo "Reading Same Variable from big cache With ttl \n";
+//ñîçäàåì áîëüøîé êåø
+
+for($i=0;$i<$n;$i++)
+{
+  $ttl=rand(3600,80000);
+  $var= array(rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand());
+  $cache_my->set($i,$var,array('ttl'=>$ttl));
+  $cache->set($i,$var,array('ttl'=>$ttl));
+}
+
+$sredn1=0;
+$sredn2=0;
+
+for ($j=0;$j<$m;$j++)
+{
+  $key=rand(0,$n);
+
+  $mark = microtime(true);
+  for($i=0;$i<$n;$i++)
+  {
+    $d1=$cache_my->get($key);
+  }
+  $t=microtime(true) - $mark; $sredn1+=$t;
+
+  $mark = microtime(true);
+  for($i=0;$i<$n;$i++)
+  {
+    $d2=$cache->get($key);
+  }
+  $t=microtime(true) - $mark; $sredn2+=$t;
+}
+echo "lmbCacheFileWithMetaBackend: " . $sredn1/$m/$n . "     ";
+echo "lmbCacheFileBackend: " . $sredn2/$m/$n . "\n";
+echo "second/first=".$sredn2/$sredn1. "\n";
+
+echo "Reading Same Variable from big cache. Variable not found. \n";
+
+$sredn1=0;$sredn2=0;
+for ($j=0;$j<$m;$j++)
+{
+  $mark = microtime(true);
+  for($i=0;$i<$n;$i++)
+  {
+    $d1=$cache_my->get('foo');
+  }
+  $t=microtime(true) - $mark; $sredn1+=$t;
+
+  $mark = microtime(true);
+  for($i=0;$i<$n;$i++)
+  {
+    $d2=$cache->get('foo');
+  }
+  $t=microtime(true) - $mark; $sredn2+=$t;
+}
+echo "lmbCacheFileWithMetaBackend: " . $sredn1/$m/$n . "     ";
+echo "lmbCacheFileBackend: " . $sredn2/$m/$n . "\n";
+echo "second/first=".$sredn2/$sredn1. "\n";
+
+
+$cache->flush();
+$cache_my->flush();
+
+

Added: 3.x/trunk/limb/cache/tests/cases/lmbCacheFileWithMetaBackendTest.class.php
===================================================================
--- 3.x/trunk/limb/cache/tests/cases/lmbCacheFileWithMetaBackendTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/cache/tests/cases/lmbCacheFileWithMetaBackendTest.class.php	2008-04-10 18:35:05 UTC (rev 6915)
@@ -0,0 +1,23 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/cache/src/lmbCacheFileWithMetaBackend.class.php');
+lmb_require(dirname(__FILE__) . '/lmbCacheFileBackendTest.class.php');
+
+class lmbCacheFileWithMetaBackendTest extends lmbCacheFileBackendTest
+{
+  var $cache_dir;
+
+  function _createPersisterImp()
+  {
+    $this->cache_dir = LIMB_VAR_DIR . '/cache';
+    return new lmbCacheFileWithMetaBackend($this->cache_dir);
+  }
+
+}



More information about the limb-svn mailing list