Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved memcache(d) drivers plus added some clean-up #127

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 27 additions & 2 deletions lib/Doctrine/Common/Cache/MemcacheCache.php
Expand Up @@ -66,15 +66,29 @@ public function getMemcache()
*/
protected function doFetch($id)
{
return $this->memcache->get($id);
$content = $this->memcache->get($id);

if ($content == 'doctrine__true__') {
$content = true;
} elseif ($content == 'doctrine__false__') {
$content = false;
} elseif ($content == 'doctrine__empty__') {
$content = '';
} elseif ($content == 'doctrine__zero__') {
$content = 0;
}

return $content;
}

/**
* {@inheritdoc}
*/
protected function doContains($id)
{
return (bool) $this->memcache->get($id);
$content = $this->memcache->get($id);

return ($content === 0 || (bool) $content);
}

/**
Expand All @@ -85,6 +99,17 @@ protected function doSave($id, $data, $lifeTime = 0)
if ($lifeTime > 30 * 24 * 3600) {
$lifeTime = time() + $lifeTime;
}

if (true === $data) {
$data = 'doctrine__true__';
} elseif(false === $data) {
$data = 'doctrine__false__';
} elseif ('' === $data) {
$data = 'doctrine__empty__';
} elseif (0 === $data) {
$data = 'doctrine__zero__';
}

return $this->memcache->set($id, $data, 0, (int) $lifeTime);
}

Expand Down
4 changes: 3 additions & 1 deletion lib/Doctrine/Common/Cache/MemcachedCache.php
Expand Up @@ -74,7 +74,9 @@ protected function doFetch($id)
*/
protected function doContains($id)
{
return (false !== $this->memcached->get($id));
$this->memcached->get($id);

return $this->memcached->getResultCode() != Memcached::RES_NOTFOUND;
}

/**
Expand Down
59 changes: 56 additions & 3 deletions tests/Doctrine/Tests/Common/Cache/CacheTest.php
Expand Up @@ -18,15 +18,15 @@ public function testBasics()

// Test fetch
$this->assertEquals('testing this out', $cache->fetch('test_key'));

// Test delete
$cache->save('test_key2', 'test2');
$cache->delete('test_key2');
$this->assertFalse($cache->contains('test_key2'));

// Fetch/save test with objects (Is cache driver serializes/unserializes objects correctly ?)
$cache->save('test_object_key', new \ArrayObject());
$this->assertTrue($cache->fetch('test_object_key') instanceof \ArrayObject);
$this->assertTrue($cache->fetch('test_object_key') instanceof \ArrayObject);
}

public function testDeleteAll()
Expand Down Expand Up @@ -64,6 +64,34 @@ public function testNamespace()
$this->assertFalse($cache->contains('key1'));
}

public function testZeroNotFalse()
{
$cache = $this->_getCacheDriver();

// Test save of int 0
$cache->save('test_key_zero', 0);
$cache->save('test_key_false', false);
$cache->save('test_key_empty', '');

// Test contains on int 0
$this->assertTrue($cache->contains('test_key_zero'));

// Test contains FALSE value
$this->assertTrue($cache->contains('test_key_false'));

// Test contains FALSE value
$this->assertTrue($cache->contains('test_key_empty'));

// Test fetch of int 0
$this->assertEquals(0, $cache->fetch('test_key_zero'));

// Test fetch FALSE value
$this->assertFalse($cache->fetch('test_key_false'));

// Test fetch FALSE value
$this->assertEquals('', $cache->fetch('test_key_empty'));
}

/**
* @group DCOM-43
*/
Expand All @@ -88,4 +116,29 @@ public function testGetStats()
* @return \Doctrine\Common\Cache\CacheProvider
*/
abstract protected function _getCacheDriver();

/**
* Clean-up
*/
protected function tearDown()
{
if (0 != $this->getStatus()) {
return;
}

$cache = $this->_getCacheDriver();

$cache->delete('test_key');
$cache->delete('test_key1');
$cache->delete('test_key2');
$cache->delete('test_object_key');
$cache->delete('test_key_zero');
$cache->delete('test_key_false');
$cache->delete('test_key_empty');

$cache->setNamespace('test_');
$cache->delete('key1');
$cache->setNamespace('test2_');
$cache->delete('key1');
}
}