Skip to content

Commit 07bf626

Browse files
committed
Fix that increment/decrement no longer work for XcacheEngine
1 parent 6105714 commit 07bf626

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/Cache/Engine/XcacheEngine.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,14 @@ public function write($key, $value)
7777
{
7878
$key = $this->_key($key);
7979

80+
if (!is_numeric($value)) {
81+
$value = serialize($value);
82+
}
83+
8084
$duration = $this->_config['duration'];
8185
$expires = time() + $duration;
8286
xcache_set($key . '_expires', $expires, $duration);
83-
return xcache_set($key, serialize($value), $duration);
87+
return xcache_set($key, $value, $duration);
8488
}
8589

8690
/**
@@ -100,7 +104,12 @@ public function read($key)
100104
if ($cachetime < $time || ($time + $this->_config['duration']) < $cachetime) {
101105
return false;
102106
}
103-
return unserialize(xcache_get($key));
107+
108+
$value = xcache_get($key);
109+
if (is_string($value) && !is_numeric($value)) {
110+
$value = unserialize($value);
111+
}
112+
return $value;
104113
}
105114
return false;
106115
}

tests/TestCase/Cache/Engine/XcacheEngineTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public function testReadAndWriteCache()
102102
$expecting = '';
103103
$this->assertEquals($expecting, $result);
104104

105+
// String
105106
$data = 'this is a test of the emergency broadcasting system';
106107
$result = Cache::write('test', $data, 'xcache');
107108
$this->assertTrue($result);
@@ -110,6 +111,23 @@ public function testReadAndWriteCache()
110111
$expecting = $data;
111112
$this->assertEquals($expecting, $result);
112113

114+
// Integer
115+
$data = 100;
116+
$result = Cache::write('test', 100, 'xcache');
117+
$this->assertTrue($result);
118+
119+
$result = Cache::read('test', 'xcache');
120+
$this->assertSame(100, $result);
121+
122+
// Object
123+
$data = (object)['value' => 'an object'];
124+
$result = Cache::write('test', $data, 'xcache');
125+
$this->assertTrue($result);
126+
127+
$result = Cache::read('test', 'xcache');
128+
$this->assertInstanceOf('stdClass', $result);
129+
$this->assertEquals('an object', $result->value);
130+
113131
Cache::delete('test', 'xcache');
114132
}
115133

0 commit comments

Comments
 (0)