netis-pl / yii2-crud

Yii2 extension that provides default CRUD for AR models
MIT License
3 stars 1 forks source link

AC: use HttpCache filter #11

Open nineinchnick opened 8 years ago

nineinchnick commented 8 years ago

Etags should be generated based only on data, that is collections and single records.

There can be many strategies for generating etags, like:

Hashing a collection always requires iterating over it, so when streaming is used cache should be disabled anyway - that duplicates streaming conditions.

Caching should be disabled if the response contains metadata like:

This usually happens when response format is html and whole page is loaded (pjax is not used). Additional filters in HttpCache could help use caching for page fragments.

Add the following behavior in the \netis\crud\ActiveController class:

            [
                'class'        => 'yii\filters\HttpCache',
                'only'         => ['index', 'relation', 'view'],
                'lastModified' => function ($action, $params) {
                    $q = new \yii\db\Query();

                    return $q->from('user')->max('updated_at');
                },
                'etagSeed' => function ($action, $params) {
                    /**
                     * use pk + version, pk + last timestamp or serialize whole record
                     * this should also include changes to the user state, like auth
                     */
                    return // generate etag seed here
                },
            ],
mozarcik commented 8 years ago

more info about this http://www.yiiframework.com/doc-2.0/guide-caching-http.html