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

svn at limb-project.com svn at limb-project.com
Wed Apr 9 09:15:18 MSD 2008


Author: svk
Date: 2008-04-09 09:15:18 +0400 (Wed, 09 Apr 2008)
New Revision: 6902
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6902

Added:
   3.x/trunk/limb/cache/src/wact/
   3.x/trunk/limb/cache/src/wact/lmbCacheBlockTag.tag.php
Modified:
   3.x/trunk/limb/cache/src/lmbCacheApcBackend.class.php
   3.x/trunk/limb/cache/src/lmbCacheBackend.interface.php
   3.x/trunk/limb/cache/src/lmbCacheFileBackend.class.php
   3.x/trunk/limb/cache/src/lmbCacheGroupDecorator.class.php
   3.x/trunk/limb/cache/src/lmbCacheMemcacheBackend.class.php
   3.x/trunk/limb/cache/src/lmbCacheMemoryBackend.class.php
   3.x/trunk/limb/cache/tests/cases/lmbCacheBackendTest.class.php
   3.x/trunk/limb/cache/tests/cases/lmbCacheGroupDecoratorTest.class.php
Log:
-- cache interface appended with add() and stat() methods
-- raw parameter for add(), set() and get() introduced
-- <cache:block> tag added

Modified: 3.x/trunk/limb/cache/src/lmbCacheApcBackend.class.php
===================================================================
--- 3.x/trunk/limb/cache/src/lmbCacheApcBackend.class.php	2008-04-08 06:42:51 UTC (rev 6901)
+++ 3.x/trunk/limb/cache/src/lmbCacheApcBackend.class.php	2008-04-09 05:15:18 UTC (rev 6902)
@@ -17,20 +17,47 @@
  */
 class lmbCacheApcBackend implements lmbCacheBackend
 {
-  function set($key, $value, $params = array()) 
+  function add($key, $value, $params = array()) 
   {
-    $container = new lmbSerializable($value);
+    if (array_key_exists("raw", $params))
+    {
+      return apc_add($key, $value, $this->_getTtl($params));
+    }
+    else
+    {
+      $container = new lmbSerializable($value);
+      return apc_add($key, serialize($container), $this->_getTtl($params));
+    }
 
-    apc_store($key, serialize($container), $this->_getTtl($params));
   }
+  
+  function set($key, $value, $params = array()) 
+  {
+    if (array_key_exists("raw", $params))
+    {
+      return apc_store($key, $value, $this->_getTtl($params));
+    }
+    else
+    {
+      $container = new lmbSerializable($value);
+      return apc_store($key, serialize($container), $this->_getTtl($params));
+    }
+  }
 
   function get($key, $params = array())
   {
     if (!$value = apc_fetch($key))
       return false;
 
-    $container = unserialize($value);
-    return $container->getSubject();
+    if (array_key_exists("raq", $params))
+    {
+      return $value;
+    }
+    else
+    {
+      $container = unserialize($value);
+      return $container->getSubject();
+    }
   }
 
   function delete($key, $params = array())
@@ -43,6 +70,14 @@
     apc_clear_cache('user');
   }
   
+  function stat($params = array())
+  {
+    return apc_cache_info(
+        isset($params['cache_type']) ? $params['cache_type'] : "user",
+        isset($params['limited']) ? (bool) $params['limited'] : true
+    );
+  }
+  
   protected function _getTtl($params)
   {
     if (!isset($params['ttl']))

Modified: 3.x/trunk/limb/cache/src/lmbCacheBackend.interface.php
===================================================================
--- 3.x/trunk/limb/cache/src/lmbCacheBackend.interface.php	2008-04-08 06:42:51 UTC (rev 6901)
+++ 3.x/trunk/limb/cache/src/lmbCacheBackend.interface.php	2008-04-09 05:15:18 UTC (rev 6902)
@@ -15,8 +15,10 @@
  */
 interface lmbCacheBackend
 {
+  function add ($key, $value, $params = array());
   function set ($key, $value, $params = array());
   function get ($key, $params = array());
   function delete($key, $params = array());
   function flush();
+  function stat($params = array());
 }

Modified: 3.x/trunk/limb/cache/src/lmbCacheFileBackend.class.php
===================================================================
--- 3.x/trunk/limb/cache/src/lmbCacheFileBackend.class.php	2008-04-08 06:42:51 UTC (rev 6901)
+++ 3.x/trunk/limb/cache/src/lmbCacheFileBackend.class.php	2008-04-09 05:15:18 UTC (rev 6902)
@@ -32,14 +32,42 @@
     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("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, $params);
 
-    $container = new lmbSerializable($value);
-    lmbFs :: safeWrite($file, serialize($container));
+    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())
@@ -54,8 +82,15 @@
         return false;
     }
 
-    $container = unserialize(file_get_contents($file));
-    return $container->getSubject();
+    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())
@@ -67,6 +102,11 @@
   {
     $this->_removeFileCache();
   }
+  
+  function stat($params = array())
+  {
+    return array();
+  }
 
   protected function _removeFileCache($key = false)
   {

Modified: 3.x/trunk/limb/cache/src/lmbCacheGroupDecorator.class.php
===================================================================
--- 3.x/trunk/limb/cache/src/lmbCacheGroupDecorator.class.php	2008-04-08 06:42:51 UTC (rev 6901)
+++ 3.x/trunk/limb/cache/src/lmbCacheGroupDecorator.class.php	2008-04-09 05:15:18 UTC (rev 6902)
@@ -29,13 +29,26 @@
       $this->_groups = $groups;
   }
 
+  function add($key, $value, $params = array())
+  {
+    $group = $this->_getGroup($params);
+    $result = $this->_cache->add($this->_generateKey($key, $group), $value, $params);
+
+    if (!$this->_groupKeyExists($key, $group))
+      $this->_groups[$group][] = $key;
+      
+    return $result;
+  }
+  
   function set($key, $value, $params = array())
   {
     $group = $this->_getGroup($params);
-    $this->_cache->set($this->_generateKey($key, $group), $value, $params);
+    $result = $this->_cache->set($this->_generateKey($key, $group), $value, $params);
 
     if (!$this->_groupKeyExists($key, $group))
       $this->_groups[$group][] = $key;
+      
+    return $result;
   }
 
   function get($key, $params = array())
@@ -71,6 +84,11 @@
     $this->_groups = array();
   }
   
+  function stat($params = array())
+  {
+    return $this->_cache->stat();
+  }
+  
   protected function _getGroup($params)
   {
     if(isset($params['group']) and $params['group'])

Modified: 3.x/trunk/limb/cache/src/lmbCacheMemcacheBackend.class.php
===================================================================
--- 3.x/trunk/limb/cache/src/lmbCacheMemcacheBackend.class.php	2008-04-08 06:42:51 UTC (rev 6901)
+++ 3.x/trunk/limb/cache/src/lmbCacheMemcacheBackend.class.php	2008-04-09 05:15:18 UTC (rev 6902)
@@ -25,11 +25,30 @@
     $this->_memcache->connect($host, $port);
   }
 
+  function add($key, $value, $params = array()) 
+  {
+    if (array_key_exists("raw", $params))
+    {
+      return $this->_memcache->add($key, $value, null, $this->_getTtl($params));
+    }
+    else
+    {
+      $container = new lmbSerializable($value);
+      return $this->_memcache->add($key, serialize($container), null, $this->_getTtl($params));
+    }
+  }
+  
   function set($key, $value, $params = array()) 
   {
-    $container = new lmbSerializable($value);
-
-    $this->_memcache->set($key, serialize($container), null, $this->_getTtl($params));
+    if (array_key_exists("raw", $params))
+    {
+      return $this->_memcache->set($key, $value, null, $this->_getTtl($params));
+    }
+    else
+    {
+      $container = new lmbSerializable($value);
+      return $this->_memcache->set($key, serialize($container), null, $this->_getTtl($params));
+    }
   }
 
   function get($key, $params = array())
@@ -37,8 +56,15 @@
     if (!$value = $this->_memcache->get($key))
       return false;
         
-    $container = unserialize($value);
-    return $container->getSubject();
+    if (array_key_exists("raw", $params))
+    {
+      return $value;
+    }
+    else
+    {
+      $container = unserialize($value);
+      return $container->getSubject();
+    }
   }
 
   function delete($key, $params = array())
@@ -51,6 +77,15 @@
     $this->_memcache->flush();
   }
   
+  function stat($params = array())
+  {
+    return $this->_memcache->getStats(
+      isset($params['cache_type']) ? $params['cache_type'] : null,
+      isset($params['slabid']) ? $params['slabid'] : null,
+      isset($params['limit']) ? $params['limit'] : 100
+    );
+  }
+  
   protected function _getTtl($params)
   {
     if (!isset($params['ttl']))

Modified: 3.x/trunk/limb/cache/src/lmbCacheMemoryBackend.class.php
===================================================================
--- 3.x/trunk/limb/cache/src/lmbCacheMemoryBackend.class.php	2008-04-08 06:42:51 UTC (rev 6901)
+++ 3.x/trunk/limb/cache/src/lmbCacheMemoryBackend.class.php	2008-04-09 05:15:18 UTC (rev 6902)
@@ -18,9 +18,19 @@
 {
   protected $_cache = array();
 
+  function add($key, $value, $params = array())
+  {
+    if (array_key_exists($key, $this->_cache))
+      return false;
+      
+    $this->_cache[$key] = $value;
+    return true;
+  }
+  
   function set($key, $value, $params = array())
   {
     $this->_cache[$key] = $value;
+    return true;
   }
 
   function get($key, $params = array())
@@ -40,5 +50,10 @@
   {
     $this->_cache = array();
   }
+  
+  function stat($params = array())
+  {
+    return array();
+  }
 }
 

Added: 3.x/trunk/limb/cache/src/wact/lmbCacheBlockTag.tag.php
===================================================================
--- 3.x/trunk/limb/cache/src/wact/lmbCacheBlockTag.tag.php	                        (rev 0)
+++ 3.x/trunk/limb/cache/src/wact/lmbCacheBlockTag.tag.php	2008-04-09 05:15:18 UTC (rev 6902)
@@ -0,0 +1,31 @@
+<?php
+/**
+ * @tag cache:block
+ * @req_attributes ttl key group
+ *
+ */
+class lmbCacheBlockTag extends WactCompilerTag
+{
+  function generateTagContent($code)
+  {
+		
+  		$code -> writePHP('$toolkit = lmbToolkit::instance();');
+  		$code -> writePHP('$cache = $toolkit->getCache();');
+  		
+  		$code->writePHP('$cache_key = ');
+  		$code->writePHP($this->attributeNodes['key']->generateExpression($code));
+  		$code->writePHP(';');
+  		$code->writePHP('if ($cached = $cache->get($cache_key, array("group" => "'.$this->attributeNodes['group']->getValue().'", "raw" => 1))) {');
+  		
+  		$code->writePHP('echo $cached;');
+  		//$code->writePHP('echo "cached output";');
+  		$code->writePHP('} else {');
+  		
+  		$code->writePHP('ob_start();');
+    	parent :: generateTagContent($code);
+    	$code->writePHP('$cache->set($cache_key, ob_get_flush(), array("ttl" => "'.$this->attributeNodes['ttl']->getValue().'", "group" => "'.$this->attributeNodes['group']->getValue().'", "raw" => 1));');
+    	//$code->writePHP('echo "stored in cache";');
+
+	    $code->writePHP('}');
+  }
+}

Modified: 3.x/trunk/limb/cache/tests/cases/lmbCacheBackendTest.class.php
===================================================================
--- 3.x/trunk/limb/cache/tests/cases/lmbCacheBackendTest.class.php	2008-04-08 06:42:51 UTC (rev 6901)
+++ 3.x/trunk/limb/cache/tests/cases/lmbCacheBackendTest.class.php	2008-04-09 05:15:18 UTC (rev 6902)
@@ -39,6 +39,25 @@
     $var = $this->cache->get(1);
     $this->assertEqual($v, $var);
   }
+  
+  function testAddLock()
+  {
+    $set = $this->cache->set(1, $v = 'value');
+    $add = $this->cache->add(1, $v = 'value');
+    
+    $this->assertTrue($set);
+    $this->assertFalse($add);
+    
+    $add2 = $this->cache->add(2, $v2 = 'value2');
+    $this->assertTrue($add2);
+    $this->assertEqual($v2, 'value2');
+    
+    $var = $this->cache->get(1);
+    $this->assertEqual($v, $var);
+    
+    $this->cache->set(2, 'new value');
+    $this->assertEqual($this->cache->get(2), 'new value');
+  }
 
   function testSetToCache()
   {

Modified: 3.x/trunk/limb/cache/tests/cases/lmbCacheGroupDecoratorTest.class.php
===================================================================
--- 3.x/trunk/limb/cache/tests/cases/lmbCacheGroupDecoratorTest.class.php	2008-04-08 06:42:51 UTC (rev 6901)
+++ 3.x/trunk/limb/cache/tests/cases/lmbCacheGroupDecoratorTest.class.php	2008-04-09 05:15:18 UTC (rev 6902)
@@ -20,7 +20,11 @@
   {
     $key = 1;
     $this->cache->set($key, $v1 = 'value1');
-    $this->cache->set($key, $v2 = 'value2', array('group' => 'test-group'));
+    $set_value = $this->cache->set($key, $v2 = 'value2', array('group' => 'test-group'));
+    $add_value = $this->cache->add($key, $v2 = 'value2', array('group' => 'test-group'));
+    
+    $this->assertTrue($set_value);
+    $this->assertFalse($add_value);
 
     $cache_value = $this->cache->get($key);
     $this->assertEqual($cache_value, $v1);
@@ -29,6 +33,24 @@
     $this->assertEqual($cache_value, $v2);
   }
 
+  function testRawPutToCacheWithGroup()
+  {
+    $key = 1;
+    $this->cache->set($key, $v1 = 'value1', array('raw' => 1));
+    $set_value = $this->cache->set($key, $v2 = 'value2', array('group' => 'test-group', 'raw' => 1));
+    $add_value = $this->cache->add($key, $v2 = 'value2', array('group' => 'test-group', 'raw' => 1));
+    
+    $this->assertTrue($set_value);
+    $this->assertFalse($add_value);
+
+    $cache_value = $this->cache->get($key, array('raw' => 1));
+    $this->assertEqual($cache_value, $v1);
+
+    $cache_value = $this->cache->get($key, array('group' => 'test-group', 'raw' => 1));
+    $this->assertEqual($cache_value, $v2);
+  }
+
+  
   function testFlushGroup()
   {
     $key = 1;



More information about the limb-svn mailing list