-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
271 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
language: php | ||
|
||
sudo: false | ||
# sudo: false | ||
sudo: required | ||
|
||
services: | ||
- redis-server | ||
|
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
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
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,94 @@ | ||
<?php | ||
|
||
namespace Apix\Cache\Mongo; | ||
|
||
use MongoDB\Collection; | ||
|
||
/** | ||
* allows to use \MongoDB\Collection in the code as \MongoCollection | ||
*/ | ||
class CollectionAdapter /*extends \MongoCollection*/ | ||
{ | ||
/** | ||
* @var Collection | ||
*/ | ||
private $collection; | ||
|
||
/** | ||
* CollectionAdapter constructor. | ||
* @param Collection $collection | ||
*/ | ||
public function __construct(Collection $collection) | ||
{ | ||
$this->collection = $collection; | ||
} | ||
|
||
public function ensureIndex(array $keys, array $options = array()) | ||
{ | ||
$this->collection->createIndex($keys, $options); | ||
} | ||
|
||
public function insert($a, array $options = array()) | ||
{ | ||
try { | ||
$this->collection->insertOne($a, $options); | ||
return ['ok' => 1]; | ||
} catch (\Exception $e) { | ||
return ['ok' => 0, 'error' => $e->getMessage()]; | ||
} | ||
} | ||
|
||
public function update(array $criteria, array $newobj, array $options = array()) | ||
{ | ||
try { | ||
$this->collection->updateOne($criteria, array('$set' => $newobj), $options); | ||
return ['ok' => 1]; | ||
} catch (\Exception $e) { | ||
return ['ok' => 0, 'error' => $e->getMessage()]; | ||
} | ||
} | ||
|
||
public function remove(array $criteria = array(), array $options = array()) | ||
{ | ||
try { | ||
$result = $this->collection->deleteMany($criteria, $options); | ||
return ['ok' => 1, 'n' => $result->getDeletedCount()]; | ||
} catch (\Exception $e) { | ||
return ['ok' => 0, 'error' => $e->getMessage()]; | ||
} | ||
} | ||
|
||
public function find(array $query = array(), array $fields = array()) | ||
{ | ||
$options = ['projection' => array_fill_keys($fields, 1) + ['_id' => 0]]; | ||
|
||
return $this->collection->find($query, $options); | ||
} | ||
|
||
public function findOne(array $query = array(), array $fields = array()) | ||
{ | ||
$options = ['projection' => array_fill_keys($fields, 1) + ['_id' => 0]]; | ||
|
||
$result = $this->collection->findOne($query, $options); | ||
|
||
// mimic \MongoDate->sec | ||
if (!empty($result['expire'])) { | ||
$sec = $result['expire']->toDateTime()->getTimestamp(); | ||
$result['expire'] = new \stdClass(); | ||
$result['expire']->sec = $sec; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function drop() | ||
{ | ||
$this->collection->drop(); | ||
return ['ok' => 1]; | ||
} | ||
|
||
public function count($query = array()) | ||
{ | ||
return $this->collection->count($query); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace Apix\Cache\Mongo; | ||
|
||
use MongoDB\Database; | ||
|
||
/** | ||
* allows to use \MongoDB\Database in the code as \MongoDB | ||
*/ | ||
class DatabaseAdapter /*extends \MongoDB*/ | ||
{ | ||
/** | ||
* @var Database | ||
*/ | ||
private $db; | ||
|
||
/** | ||
* @param Database $db | ||
*/ | ||
public function __construct(Database $db) | ||
{ | ||
$this->db = $db; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param array $options | ||
* @return CollectionAdapter | ||
*/ | ||
public function createCollection($name, $options) | ||
{ | ||
return new CollectionAdapter($this->db->selectCollection($name, $options)); | ||
} | ||
} |
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
Oops, something went wrong.