-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from jasny/2.0-mongo
Added Mongo support.
- Loading branch information
Showing
4 changed files
with
225 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Cache package. | ||
* | ||
* Copyright (c) Daniel González | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Daniel González <daniel@desarrolla2.com> | ||
*/ | ||
|
||
namespace Desarrolla2\Cache\Adapter; | ||
|
||
use Desarrolla2\Cache\Exception\CacheException; | ||
|
||
/** | ||
* Mongo | ||
*/ | ||
class Mongo extends AbstractAdapter implements AdapterInterface | ||
{ | ||
/** | ||
* @var MongoCollection|MongoDB\Collection | ||
*/ | ||
protected $collection; | ||
|
||
/** | ||
* @param MongoDB|MongoDB\Database|MongoCollection|MongoDB\Collection $backend | ||
*/ | ||
public function __construct($backend = null) | ||
{ | ||
if (!isset($backend)) { | ||
$client = class_exist('MongoCollection') ? new \MongoClient() : new \MongoDB\Client(); | ||
$backend = $client->selectDatabase('cache'); | ||
} | ||
|
||
if ($backend instanceof \MongoCollection || $backend instanceof \MongoDB\Collection) { | ||
$this->collection = $backend; | ||
} elseif ($backend instanceof \MongoDB || $backend instanceof \MongoDB\Database) { | ||
$this->collection = $backend->selectCollection('items'); | ||
} else { | ||
$type = (is_object($database) ? get_class($database) . ' ' : '') . gettype($database); | ||
throw new CacheException("Database should be a database (MongoDB or MongoDB\Database) or " . | ||
" collection (MongoCollection or MongoDB\Collection) object, not a $type"); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function del($key) | ||
{ | ||
$tKey = $this->getKey($key); | ||
$this->collection->remove(array('_id' => $tKey)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc } | ||
*/ | ||
public function get($key) | ||
{ | ||
$tKey = $this->getKey($key); | ||
$tNow = $this->getTtl(); | ||
$data = $this->collection->findOne(array('_id' => $tKey, 'ttl' => array('$gte' => $tNow))); | ||
if (isset($data)) { | ||
return $this->unPack($data['value']); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc } | ||
*/ | ||
public function has($key) | ||
{ | ||
$tKey = $this->getKey($key); | ||
$tNow = $this->getTtl(); | ||
return $this->collection->count(array('_id' => $tKey, 'ttl' => array('$gte' => $tNow))) > 0; | ||
} | ||
|
||
/** | ||
* {@inheritdoc } | ||
*/ | ||
public function set($key, $value, $ttl = null) | ||
{ | ||
$tKey = $this->getKey($key); | ||
$tValue = $this->pack($value); | ||
if (!$ttl) { | ||
$ttl = $this->ttl; | ||
} | ||
$item = array( | ||
'_id' => $tKey, | ||
'value' => $tValue, | ||
'ttl' => $this->getTtl($ttl), | ||
); | ||
$this->collection->update(array('_id' => $tKey), $item, array('upsert' => true)); | ||
} | ||
|
||
/** | ||
* Get TTL as Date type BSON object | ||
* | ||
* @param int $ttl | ||
* @return MongoDate|MongoDB\BSON\UTCDatetime | ||
*/ | ||
protected function getTtl($ttl = 0) | ||
{ | ||
return $this->collection instanceof \MongoCollection ? | ||
new \MongoDate((int) $ttl + time()) : | ||
new \MongoDB\BSON\UTCDatetime(((int) $ttl + time() * 1000)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Cache package. | ||
* | ||
* Copyright (c) Daniel González | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Daniel González <daniel@desarrolla2.com> | ||
*/ | ||
|
||
namespace Desarrolla2\Test\Cache\Adapter; | ||
|
||
use Desarrolla2\Cache\Cache; | ||
use Desarrolla2\Cache\Adapter\Mongo; | ||
|
||
/** | ||
* MongoTest | ||
*/ | ||
class MongoTest extends AbstractCacheTest | ||
{ | ||
public function setUp() | ||
{ | ||
parent::setup(); | ||
if (!extension_loaded('mongo')) { | ||
$this->markTestSkipped( | ||
'The mongo extension is not available.' | ||
); | ||
} | ||
|
||
$client = new \MongoClient($this->config['mongo']['dsn']); | ||
$database = $client->selectDB($this->config['mongo']['database']); | ||
|
||
$this->cache = new Cache( | ||
new Mongo($database) | ||
); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function dataProviderForOptions() | ||
{ | ||
return [ | ||
['ttl', 100], | ||
]; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function dataProviderForOptionsException() | ||
{ | ||
return [ | ||
['ttl', 0, '\Desarrolla2\Cache\Exception\CacheException'], | ||
['file', 100, '\Desarrolla2\Cache\Exception\CacheException'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters