1

I have blog via yii2 and using page caching

    'class' => 'yii\filters\PageCache',
    'only' => ['view','video'],
    'duration' => 900,
    'dependency' => [
        'class' => 'yii\caching\DbDependency',
         'sql' => '?',
    ],

my post table has primary key as id ; how to I set sql as separate page cache per post id ? thank you guy .

kiamoz
  • 714
  • 1
  • 7
  • 18

1 Answers1

1

Just pass the current ID to it and yes, you can do it, use the enabled property and variation like this

[
            'enabled' => Yii::$app->request->isGet && Yii::$app->user->isGuest,
            'class' => 'yii\filters\PageCache',
            'only' => ['index'],
            'duration' => 900,
            'variations' => [
                Yii::$app->request->get('id')
            ]
]

This ensures that it will cache only get requests for non logged in users per post ID, you don't want to cache other request than get or logged in users if they can do some things like commenting and see per user data (login name etc.)

You can even use a more advanced config if you like eg. dynamic placeholder with page cache to allow caching even for authenticated users, just check the docs.

Programista
  • 1,037
  • 5
  • 15
  • 38
  • yes it's work very nice ! i test it now . before that I used fragment caching for sidebar cache ( becosue my sidebar is very heavy in mysql) and just the single post content gets fresh from mysql and I think it's not matter to cache for that or not but know its very important for server and mysql to dont fetch new data per user in high visit time and big data . – kiamoz Aug 11 '17 at 05:26
  • If you disable cache for logged in users i would still use fragment cache for sidebar if you can't use pagecache for such users. Or better, change every per user data to a fragment cache and page cache the whole time. – Programista Aug 11 '17 at 13:27
  • yes it's true , I have question . if cache variations is getting large in number as large as our posts in database it not cause a problem ? – kiamoz Aug 11 '17 at 14:08
  • Depends on traffic and server, you have 900 duration so after 15 minutes they are all deleted, only those visited are regenerated, one post page can have many sizes, depends, with an average of 300kb you would fit about 340 pages in 100mb size, 1gb = 3400 cached pages. On disk or where you want, you can change cache provider for pagecache to redis cache if you can and want (Default disk). You can always compress the page cache pages if it will be an issue, but this requires much work. So depends how many pages will be visited day to day basis and if you have disk space for cache. (or memory) – Programista Aug 11 '17 at 17:54
  • grate help and thanks for all you words , this topic is a little advance and few person had visit it , thank you for your attention too. good by – kiamoz Aug 11 '17 at 21:23
  • Dear sir in this case I used a counter to count the post view and now with page cache it's not work , do you have idea about it ? I have to use fragment cache? – kiamoz Aug 12 '17 at 13:50
  • Yes if you want the value to refresh. – Programista Aug 12 '17 at 15:25