PageCache – 永夜 https://www.shuijingwanwq.com 没有不值得去解决的问题,也没有不值得去学习的技术! Sun, 17 May 2026 06:06:51 +0000 zh-Hans hourly 1 https://wordpress.org/?v=7.0 在 Yii2 中,报错:Call to undefined method Closure::evaluateDependency() https://www.shuijingwanwq.com/2025/07/17/9225/ https://www.shuijingwanwq.com/2025/07/17/9225/#respond Thu, 17 Jul 2025 02:43:05 +0000 https://www.shuijingwanwq.com/?p=9225 浏览量: 97 1、在 Yii2 中,报错:Call to undefined method Closure::evaluateDependency()。如图1
在 Yii2 中,报错:Call to undefined method Closure::evaluateDependency()

图1

2、给 view 方法所对应的详情页面添加了页面缓存。代码实现如下


        // 新增 fileCache 组件,用于页面缓存等大数据内容
        'fileCache' => [
            'class' => 'yii\caching\FileCache',
            'cachePath' => '@runtime/cache/pages', // 可自定义路径
            'directoryLevel' => 2, // 默认即可,便于分目录管理文件
        ],




use yii\caching\DbDependency;
use yii\filters\PageCache;

    public function behaviors()
    {
        return [
            'pageCache' => [
                'class' => PageCache::class,
                'only' => ['view'], // 仅缓存 view 动作
                'duration' => 300,  // 缓存 5 分钟
                'variations' => [
                    Yii::$app->request->get('id'),
                    Yii::$app->request->get('alias'),
                ],
                'dependency' => function () {
                    $alias = Yii::$app->request->get('alias');
                    $id = Yii::$app->request->get('id');

                    if (!empty($alias)) {
                        return new DbDependency([
                            'sql' => 'SELECT updated_at FROM use_cases WHERE alias = :alias LIMIT 1',
                            'params' => [':alias' => $alias],
                        ]);
                    } elseif (!empty($id)) {
                        return new DbDependency([
                            'sql' => 'SELECT updated_at FROM use_cases WHERE id = :id LIMIT 1',
                            'params' => [':id' => $id],
                        ]);
                    }

                    return null; // 不缓存
                },
                'cache' => 'fileCache', // 指定使用 file 缓存组件
            ],
        ];
    }


3、当前 Yii2 的版本是 2.0.50。不直接传闭包给 dependency,而是在行为外先生成依赖实例


	public function behaviors()
    {
        $alias = Yii::$app->request->get('alias');
        $id = Yii::$app->request->get('id');

        if (!empty($alias)) {
            $dependency = new DbDependency([
                'sql' => 'SELECT updated_at FROM use_cases WHERE alias = :alias LIMIT 1',
                'params' => [':alias' => $alias],
            ]);
        } elseif (!empty($id)) {
            $dependency = new DbDependency([
                'sql' => 'SELECT updated_at FROM use_cases WHERE id = :id LIMIT 1',
                'params' => [':id' => $id],
            ]);
        } else {
            // 不缓存或默认缓存依赖
            $dependency = null;
        }

        return [
            'pageCache' => [
                'class' => PageCache::class,
                'only' => ['view'],
                'duration' => 300,
                'variations' => [
                    $id,
                    $alias,
                ],
                'dependency' => $dependency,
                'cache' => 'fileCache',
            ],
        ];
    }


4、不再报错,查看 Debug Log,Valid page content is found in the cache. 。由此确认页面缓存生效,符合预期。如图2
不再报错,查看 Debug Log,Valid page content is found in the cache. 。由此确认页面缓存生效,符合预期

图2

5、查看 frontend/runtime/cache/pages 目录,确定已经生成了对应的页面缓存。如图3
查看 frontend/runtime/cache/pages 目录,确定已经生成了对应的页面缓存

图3

]]>
https://www.shuijingwanwq.com/2025/07/17/9225/feed/ 0