doujuan2688 2016-02-28 10:48
浏览 120

Yii2缓存活动记录

I need help with caching Active Record data in Yii2.

In documentation has example:

$result = Customer::getDb()->cache(function ($db) {
    return Customer::find()->where(['id' => 1])->one();
});

I understand that it need to implement the method in model class such a:

public static function getByPk($pk)
{
    $result = self::getDb()->cache(function ($db) use ($pk) {
        return self::find()->where(['id' => $pk])->one();
    });

    return $result;
}

If this query result was already cached in $result will set value from cache, otherwise will execute query, correctly?

Also I have question about dependency, what dependency I can use for invalidation this cache value?

  • 写回答

1条回答 默认 最新

  • douju5062 2016-02-28 13:01
    关注

    As an opinion! You know it depends on data that you add to cache storage. If it's some static data such as status, position etc. you can set the second param duration. You can read about at official doc.
    If you make a few same queries on the page you can set 1 or 2 seconds and it helps you a little.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退