A
A
Andrey Nikitin2022-02-03 09:17:49
Yii
Andrey Nikitin, 2022-02-03 09:17:49

How to save data to cache in yii2 along with page pagination?

Good afternoon! The problem is this. You need to save the page/component to the cache with the pagination of this page.

I read the docks, tried everything that was there, unfortunately it turns out not the result that you need.

In general, I add behaviors to the controller,

Page caching

public function behaviors()
    {
        return [
            'pageCache' =>
                [
                    'class' => 'yii\filters\PageCache',
                    'only' => ['single'],
                    'duration' => 6000,
                    'variations' => [
                        [Yii::$app->request->get('page')],
                    ],
                    'dependency' => [
                        'class' => DbDependency::class,
                        'sql' => 'SELECT COUNT(*) FROM article',
                    ],
                ],
        ];
    }


everything works fine, until the moment when you need to use user authentication. In this type of caching, the entire page is saved, along with the entry / exit buttons. And when I go to the site under the login, I kind of went in, using the debugger, but in terms of the type of buttons, nothing changes when it should switch to the person’s nickname and the exit button, since this page is all in the cache and it gives the last thing added there.

I also tried other types of cache, everything is fine there, user authentication works fine, I also configured the cache update, it works, but the jamb is that pagination is not saved from the word at all.

Fragment caching

<?if($this->beginCache('artIndex', ['dependency' => $dependency])):?>
                        <?foreach ($articles as $article):?>
                            <div class="col-md-6">
                                <?= Html::a(Html::img("@web/".$article->image,
                                        ['alt' => 'картинка']) .

                                    "<div class='blog-content-body'>
                                                <div class='post-meta'>
                                                    <span class='category'>{$article->category->title}</span>
                                                    <span class='mr-2'>{$article->dateSave}</span> &bullet;
                                                    <span class='ml-2'><span class='fa fa-comments'></span> 3</span>
                                                </div>
                                                <h2>{$article->title}</h2>
                                            </div>",
                                    [
    //                                    'article/single', 'id' => $article->id
                                        'article/single', 'slug' => $article->slug
                                    ],
                                    [
                                        'class'=>'blog-entry element-animate', 'data-animate-effect'=>'fadeIn'
                                    ]
                                )
                                ?>
                            </div>
                        <?endforeach;?>
                    <?$this->endCache()?>
                    <?endif;?>



Article goes in the model
cache update

public function behaviors()
    {
        return [
            'CachedBehavior' => [
                'class' => CachedBehavior::class,
                'cache_key' => [
                    'artIndex',
                ],
            ]
        ];
    }



The same thing happens in Data Caching.

They just drove me into some kind of dead end))) Of course, you can refuse authorization, it is only needed so that people can write comments and connect ready-made chats, but something tells me, then there will be a problem with cache with this chat... Tell me how to cache data from a page with pagination

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Nikitin, 2022-02-05
@andreyNi

In general, I figured it out, maybe it will be useful to someone, in

controller
public function actionIndex(){

        $query = Article::getArticleDesc();

        $data = $this->cacheGet('artIndex');

        if ($data === false) {
            
            $pages = new Pagination([
                'totalCount' => $query->count(),
                'pageSize' => 20,
                'forcePageParam' => false,
                'pageSizeParam' => false
            ]);
            $articles = $query->offset($pages->offset)->limit($pages->limit)->all();
            $this->cacheSet('artIndex', $articles, 10000);
            return $this->render('index', compact('articles', 'pages'));
        }


        $pages = new Pagination([
            'totalCount' => $query->count(),
            'pageSize' => 20,
            'forcePageParam' => false,
            'pageSizeParam' => false
        ]);
        $articles = $query->offset($pages->offset)->limit($pages->limit)->all();

        return $this->render('index', compact('articles', 'pages'));
    }

may be ugly, but it works

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question