Phalcon3/PHP7 Mongo Backend Cache support
So we are having following code at services.php:
$di['cache'] = function()
{
$ultraFastFrontend = new Phalcon\Cache\Frontend\Data(array(
"lifetime" => 1
));
$cache = new \Phalcon\Cache\Multiple(array(
new Phalcon\Cache\Backend\**Mongo**($ultraFastFrontend, array(
'server' => "mongodb://localhost",
'db' => 'ia_cache',
'collection' => 'some_backend_cache'
)),
));
return $cache;
};
Then we are trying to use it in our trait:
// ...
public function getTranslationByParent ($parentId)
{
$key = get_class($this) . 'Translation_parent_' . $parentId;
if ($this->getDI()->get('cache')->get($key) === null)
// ...
And getting error at last string:
Fatal error: Class 'mongoclient' not found
If I am changing Phalcon\Cache\Backend*Mongo* to Phalcon\Cache\Backend*Redis* or any other adapter everything works.
@sergeyklay
There is no mongoclient class for php 7, you need to use adapter from incubator, but as far as i know it's not working too.
There is no adapter for mongo cache in incubator
Then someone needs to create it, just right now old phalcon class is using mongoclient class which is only in php 5.x. In php 7 mongo class was changed and phalcon classes needs to be rewrite.
any update on this issue?
As you can see this is marked to resolve for v4.0.0
when can v 4.0 release please ?
Closing in favor of #13855. Will revisit if the community votes for it, or in later versions.
Quick dirty workaround (should not be used in production).
First of all install PHP Library for MongoDB (following instructions).
Add fake MongoClient class to your project:
class MongoClient
{
private $_connString;
public function __construct(string $server = "mongodb://localhost:27017")
{
$this->_connString = $server;
}
public function selectDB(string $name)
{
return new DirtyMongoDB($this->_connString, $name);
}
}
class DirtyMongoDB
{
private $_connString;
private $_dbName;
public function __construct(string $connString, string $dbName)
{
$this->_connString = $connString;
$this->_dbName = $dbName;
}
public function selectCollection(string $name)
{
return new DirtyMongoCollection($this->_connString, $this->_dbName, $name);
}
}
class DirtyMongoCollection
{
private $_client;
private $_collection;
public function __construct(string $connString, string $dbName, string $collectionName)
{
$this->_client = new \MongoDB\Client($connString);
$this->_collection = $this->_client->$dbName->$collectionName;
}
public function findOne(array $conditions = array())
{
$result = $this->_collection->findOne($conditions);
if($result)
{
$result = $result->getArrayCopy();
}
return $result;
}
public function insert(array $document)
{
return $this->_collection->insertOne($document);
}
public function update(array $criteria, array $document)
{
return $this->_collection->updateOne($criteria, ['$set' => $document]);
}
public function find(array $query = array(), array $fields = array())
{
return $this->_collection->find($query, $fields);
}
public function remove(array $query = array())
{
return $this->_collection->deleteMany($query);
}
public function count(array $query = array())
{
return $this->_collection->count($query);
}
}
Further proceed as usual
$frontCache = new \Phalcon\Cache\Frontend\None([
'lifetime' => 3600,
]);
$cache = new \Phalcon\Cache\Backend\Mongo(
$frontCache,
[
'server' => 'mongodb://localhost:27017',
'db' => 'robots',
'collection' => 'cache',
]
);
$key = 'some-cache-key';
$data = [
'a' => 'a',
'b' => [
(object) ['asd' => 123]
]
];
$cache->delete($key);
$cache->save($key, $data);
$cache->get($key);
$cache->exists($key);
$cache->flush($key);