Skip to content

Commit

Permalink
Merge pull request jpfuentes2#549 from fetch/cache-exception
Browse files Browse the repository at this point in the history
Cache exception
  • Loading branch information
Rican7 authored Jul 11, 2016
2 parents f56c77e + e7c8b49 commit d9d8786
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
9 changes: 8 additions & 1 deletion lib/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ class ExpressionsException extends ActiveRecordException {}
*/
class ConfigException extends ActiveRecordException {}

/**
* Thrown for cache problems.
*
* @package ActiveRecord
*/
class CacheException extends ActiveRecordException {}

/**
* Thrown when attempting to access an invalid property on a {@link Model}.
*
Expand Down Expand Up @@ -133,4 +140,4 @@ class RelationshipException extends ActiveRecordException {}
*
* @package ActiveRecord
*/
class HasManyThroughAssociationException extends RelationshipException {}
class HasManyThroughAssociationException extends RelationshipException {}
10 changes: 8 additions & 2 deletions lib/cache/Memcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ public function __construct($options)
$this->memcache = new \Memcache();
$options['port'] = isset($options['port']) ? $options['port'] : self::DEFAULT_PORT;

if (!$this->memcache->connect($options['host'],$options['port']))
throw new CacheException("Could not connect to $options[host]:$options[port]");
if (!@$this->memcache->connect($options['host'], $options['port'])) {
if ($error = error_get_last()) {
$message = $error['message'];
} else {
$message = sprintf('Could not connect to %s:%s', $options['host'], $options['port']);
}
throw new CacheException($message);
}
}

public function flush()
Expand Down
15 changes: 12 additions & 3 deletions test/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,18 @@ public function test_cache_expire()

public function test_namespace_is_set_properly()
{
Cache::$options['namespace'] = 'myapp';
$this->cache_get();
$this->assert_same("abcd", Cache::$adapter->read("myapp::1337"));
Cache::$options['namespace'] = 'myapp';
$this->cache_get();
$this->assert_same("abcd", Cache::$adapter->read("myapp::1337"));
}

/**
* @expectedException ActiveRecord\CacheException
* @expectedExceptionMessage Connection refused
*/
public function test_exception_when_connect_fails()
{
Cache::initialize('memcache://127.0.0.1:1234');
}
}
?>

0 comments on commit d9d8786

Please sign in to comment.