diff --git a/src/Cache/Engine/XcacheEngine.php b/src/Cache/Engine/XcacheEngine.php index 4d6171383f8..c3e23b9ae3d 100644 --- a/src/Cache/Engine/XcacheEngine.php +++ b/src/Cache/Engine/XcacheEngine.php @@ -77,10 +77,14 @@ public function write($key, $value) { $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 @@ public function read($key) 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; } diff --git a/tests/TestCase/Cache/Engine/XcacheEngineTest.php b/tests/TestCase/Cache/Engine/XcacheEngineTest.php index a5c6f59655e..56e4588d249 100644 --- a/tests/TestCase/Cache/Engine/XcacheEngineTest.php +++ b/tests/TestCase/Cache/Engine/XcacheEngineTest.php @@ -102,6 +102,7 @@ public function testReadAndWriteCache() $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 @@ public function testReadAndWriteCache() $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'); }