Php Phalcon\Mvc\Models-模型关系和缓存

Php Phalcon\Mvc\Models-模型关系和缓存,php,phalcon,Php,Phalcon,在Phalcon文档中有这样的内容: 假设我有这样一个代码: public function initialize() { $this->hasMany("id", "RobotsParts", "robots_id"); } /** * Return the related "robots parts" * * @return \RobotsParts[] */ public function getRobotsParts($parameters=null) {

在Phalcon文档中有这样的内容:

假设我有这样一个代码:

public function initialize()
{
    $this->hasMany("id", "RobotsParts", "robots_id");
}

/**
 * Return the related "robots parts"
 *
 * @return \RobotsParts[]
 */
public function getRobotsParts($parameters=null)
{
    return $this->getRelated('RobotsParts', $parameters);
}
我想知道缓存“->getRelated()”查找产生的内容的最佳方法是什么?也就是说,若它被多次调用,就不应该进入数据库


谢谢

假设您已在服务容器中定义了缓存机制,则可以执行以下操作:

public function getRobotsParts($parameters=null)
{
    $di  = \Phalcon\DI::getDefault();
    $key = 'cache_robots_parts_' . $this->id;

    $cache = $di->cache->get($key);

    if (null == $cache) {
        $results = $this->getRelated('RobotsParts', $parameters);
    } else {
        $results = $cache;
    }

    return $results;
}

它可以写在短路上:

public function getRobotsParts($parameters=null)
{
    $parameters['cache'] = array(
        'lifetime' => 123,
        'key'      => 'cache_robots_parts_' . $this->id,
    );

    return $this->getRelated('RobotsParts', $parameters);
}

或者更简短地说,如果在方法中设置了
$parameters['cache']
,这会导致这种情况

谢谢,这是一件显而易见的事情。我想知道$obj->\u modelsManager是否具有内置的缓存功能。缓存策略存在,但在请求终止后会被释放。要了解更多信息,您可以看看这个