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

Phalcon3/PHP7 Mongo Backend Cache support #12074

Closed
Akhundzada opened this issue Aug 2, 2016 · 8 comments
Closed

Phalcon3/PHP7 Mongo Backend Cache support #12074

Akhundzada opened this issue Aug 2, 2016 · 8 comments
Labels
discussion Request for comments and discussion

Comments

@Akhundzada
Copy link

Akhundzada commented Aug 2, 2016

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

@Jurigag
Copy link
Contributor

Jurigag commented Aug 2, 2016

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.

@Akhundzada
Copy link
Author

There is no adapter for mongo cache in incubator

@Jurigag
Copy link
Contributor

Jurigag commented Aug 2, 2016

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.

@sergeyklay sergeyklay added this to the 3.1.0 milestone Aug 12, 2016
@sergeyklay sergeyklay modified the milestones: 3.1.0, 3.2.0 Mar 2, 2017
@sergeyklay sergeyklay modified the milestones: 4.0.0, 3.2.0 Apr 13, 2017
@sergeyklay sergeyklay added Mongo discussion Request for comments and discussion labels Jul 17, 2017
@hisune
Copy link

hisune commented Apr 16, 2018

any update on this issue?

@sergeyklay
Copy link
Contributor

As you can see this is marked to resolve for v4.0.0

@vietlib
Copy link

vietlib commented Dec 18, 2018

when can v 4.0 release please ?

@niden
Copy link
Member

niden commented Feb 23, 2019

Closing in favor of #13855. Will revisit if the community votes for it, or in later versions.

@niden niden closed this as completed Feb 23, 2019
@iforp
Copy link
Contributor

iforp commented Mar 15, 2019

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);

@niden niden removed this from the 4.0.0 milestone Dec 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Request for comments and discussion
Projects
None yet
Development

No branches or pull requests

7 participants