Просмотр исходного кода

Cache_Backend_TwoLevels should return true, even with dumb backends. fixes ZF-9855 incl test

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22736 44c647ce-9c0f-0410-b52a-842ac1e357ba
andyfowler 15 лет назад
Родитель
Сommit
d9f8269a7b

+ 5 - 0
library/Zend/Cache/Backend/TwoLevels.php

@@ -201,6 +201,11 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
             $boolSlow = $this->_slowBackend->save($preparedData, $id, $tags, $lifetime);
             if ($boolSlow === true) {
                 $boolFast = $this->_fastBackend->remove($id);
+                if (!$boolFast && !$this->_fastBackend->test($id)) {
+                    // some backends return false on remove() even if the key never existed. (and it won't if fast is full)
+                    // all we care about is that the key doesn't exist now
+                    $boolFast = true;
+                }
             }
         }
 

+ 35 - 3
tests/Zend/Cache/TwoLevelsBackendTest.php

@@ -107,6 +107,8 @@ class Zend_Cache_TwoLevelsBackendTest extends Zend_Cache_CommonExtendedBackendTe
         $fastBackend->expects($this->at(1))
             ->method('getFillingPercentage')
             ->will($this->returnValue(90));
+
+
         $slowBackendOptions = array(
             'cache_dir' => $this->_cache_dir
         );
@@ -118,12 +120,42 @@ class Zend_Cache_TwoLevelsBackendTest extends Zend_Cache_CommonExtendedBackendTe
         ));
 
         $id = 'test'.uniqid();
-        $cache->save(10, $id); //fast usage at 0%
-
-        $cache->save(100, $id); //fast usage at 90%
+        $this->assertTrue($cache->save(10, $id)); //fast usage at 0%
+        
+        $this->assertTrue($cache->save(100, $id)); //fast usage at 90%
         $this->assertEquals(100, $cache->load($id));
     }
+    
+    /**
+     * @group ZF-9855
+     */
+    public function testSaveReturnsTrueIfFastIsFullOnFirstSave()
+    {
+        $slowBackend = 'File';
+        $fastBackend = $this->getMock('Zend_Cache_Backend_Apc', array('getFillingPercentage'));
+        $fastBackend->expects($this->any())
+            ->method('getFillingPercentage')
+            ->will($this->returnValue(90));
 
+        $slowBackendOptions = array(
+            'cache_dir' => $this->_cache_dir
+        );
+        $cache = new Zend_Cache_Backend_TwoLevels(array(
+            'fast_backend' => $fastBackend,
+            'slow_backend' => $slowBackend,
+            'slow_backend_options' => $slowBackendOptions,
+            'stats_update_factor' => 1
+        ));
+
+        $id = 'test'.uniqid();
+        
+        $this->assertTrue($cache->save(90, $id)); //fast usage at 90%, failing for 
+        $this->assertEquals(90, $cache->load($id));
+                
+        $this->assertTrue($cache->save(100, $id)); //fast usage at 90%
+        $this->assertEquals(100, $cache->load($id));
+    }
+    
 }