caching - Yii2 ActiveRecord cache -


how use activerecotd cache yii 2? did't find examples in official docs. in google found 2 examples, first is:

$db = self::getdb(); $object = $db->cache(function ($db) use($id) {     return self::findone($id); }); 

but doesn't work model, tested updated framework. other example is:

$data = \yii::$app->cache->get('some_var_' . $id); if ($data === false) {     $data = self::findone($id);     \yii::$app->cache->set('some_var_' . $id, $data, 60); } 

it's working fine, it's not activerecord caching it's data caching, haven't got activerecord caching in yii 2?

1) use cache that:

$db = yii::$app->db;// or category::getdb() $result = $db->cache(function ($db) use ($id) {     return category::find()->where(['id' => $id])->all(); }, cache_timeout); 

2) if may use query dependency, use that:

$db = yii::$app->db;// or category::getdb() $dep = new dbdependency(); $dep->sql = 'select count(*) category'; $result = $db->cache(function ($db) use ($id) {     return category::find()->where(['id' => $id])->all(); }, cache_timeout, $dep); 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -