访问模型中的memcached对象(Phalcon)

时间:2014-05-04 11:58:04

标签: phalcon

document之后,我正在尝试在模型中的find()方法中检索memcached密钥(为了获得模型和关系的缓存版本)。

有人知道我应该如何访问我在DI设置的memcached对象?

class Tags extends Phalcon\Mvc\Model {

protected  function _getCache($key)
{
    // how do i retreive memcached object?

}

protected static function _setCache($key)
{
    // stores data in the cache
}

这些是DI中的设置:

$di->set('modelsCache', function()
    {
        //Cache data for one day by default
        $frontCache = new \Phalcon\Cache\Frontend\Data(array(
                "lifetime" => 86400
        ));

        //Memcached connection settings
        $cache = new \Phalcon\Cache\Backend\Memcache($frontCache, array(
                "host" => "localhost",
                "port" => "11211"
        ));
        return $cache;

    });

1 个答案:

答案 0 :(得分:1)

首先,如果它是可重复使用的“服务”,请在getShared()上使用setShared() / DI,否则每次访问时都会创建一个新实例。

要从应用中的任何位置实际检索它:

class Tags extends Phalcon\Mvc\Model {

protected  function _getCache($key)
{
    // how do i retreive memcached object?

    $modelsChache = $this->di->getShared('modelsCache');

    // Or if DI is not set on the model, though in 99.9% it will be unless you are doing something unusual.

    $modelsChache = DI::getDefault()->getShared('modelsCache');
}

protected static function _setCache($key)
{
    // stores data in the cache
    // Same as above…
}