Browse Source

Fix that increment/decrement no longer work for XcacheEngine

chinpei215 10 years ago
parent
commit
07bf6266ab
2 changed files with 29 additions and 2 deletions
  1. 11 2
      src/Cache/Engine/XcacheEngine.php
  2. 18 0
      tests/TestCase/Cache/Engine/XcacheEngineTest.php

+ 11 - 2
src/Cache/Engine/XcacheEngine.php

@@ -77,10 +77,14 @@ class XcacheEngine extends CacheEngine
     {
         $key = $this->_key($key);
 
+        if (!is_numeric($value)) {
+            $value = serialize($value);
+        }
+
         $duration = $this->_config['duration'];
         $expires = time() + $duration;
         xcache_set($key . '_expires', $expires, $duration);
-        return xcache_set($key, serialize($value), $duration);
+        return xcache_set($key, $value, $duration);
     }
 
     /**
@@ -100,7 +104,12 @@ class XcacheEngine extends CacheEngine
             if ($cachetime < $time || ($time + $this->_config['duration']) < $cachetime) {
                 return false;
             }
-            return unserialize(xcache_get($key));
+
+            $value = xcache_get($key);
+            if (is_string($value) && !is_numeric($value)) {
+                $value = unserialize($value);
+            }
+            return $value;
         }
         return false;
     }

+ 18 - 0
tests/TestCase/Cache/Engine/XcacheEngineTest.php

@@ -102,6 +102,7 @@ class XcacheEngineTest extends TestCase
         $expecting = '';
         $this->assertEquals($expecting, $result);
 
+        // String
         $data = 'this is a test of the emergency broadcasting system';
         $result = Cache::write('test', $data, 'xcache');
         $this->assertTrue($result);
@@ -110,6 +111,23 @@ class XcacheEngineTest extends TestCase
         $expecting = $data;
         $this->assertEquals($expecting, $result);
 
+        // Integer
+        $data = 100;
+        $result = Cache::write('test', 100, 'xcache');
+        $this->assertTrue($result);
+
+        $result = Cache::read('test', 'xcache');
+        $this->assertSame(100, $result);
+
+        // Object
+        $data = (object)['value' => 'an object'];
+        $result = Cache::write('test', $data, 'xcache');
+        $this->assertTrue($result);
+
+        $result = Cache::read('test', 'xcache');
+        $this->assertInstanceOf('stdClass', $result);
+        $this->assertEquals('an object', $result->value);
+
         Cache::delete('test', 'xcache');
     }