Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 ActiveRecord cache

How to use ActiveRecotd cache for Yii 2? I did't find any examples in official docs. In Google I found 2 examples, first is:

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

But it doesn't work for Model, I tested with 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, but it's not ActiveRecord caching it's data caching, So we haven't got ActiveRecord caching in Yii 2?

like image 330
Alex Avatar asked Apr 24 '15 18:04

Alex


People also ask

How to clear cache in Yii2?

Cache Flushing When you need to invalidate all the stored cache data, you can call yii\caching\Cache::flush().

How to use cache in Yii2?

Simple Data Caching So we start by setting a value to be cached. To do so, we will also have to assign it a unique ID. For example: // Storing $value in Cache $value = "This is a variable that I am storing"; $id = "myValue"; $time = 30; // in seconds Yii::app()->cache->set($id, $value, $time);

How do I retrieve data from the Yii cache?

In both cases, you may use the same code Yii::$app->cache->get ($key) to attempt retrieving data from the cache without worrying that Yii::$app->cache might be null. yii\caching\FileCache: uses standard files to store cached data.

What does Yii\caching\filedependency mean?

For example, yii\caching\FileDependency represents the dependency of a file's modification time. When this dependency changes, it means the corresponding file is modified. As a result, any outdated file content found in the cache should be invalidated and the get () call should return false.

What is yii2 framework?

Yii2 Optimization & Caching and ActiveRecord The Yii2 framework is all-in-all an excellent framework for building complex websites. Working with the framework can, however, be quite frustrating at times mostly due to a lack of documentation and examples.

What is yiidbactiverecord class?

Class yiidbActiveRecord. ActiveRecord is the base class for classes representing relational data in terms of objects. Active Record implements the Active Record design pattern. The premise behind Active Record is that an individual yiidbActiveRecord object is associated with a specific row in a database table.


2 Answers

1) Use cache like 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 you may use query dependency, use like that:

$db = Yii::$app->db;// or Category::getDb()
$dep = new DbDependency();
$dep->sql = 'SELECT count(*) FROM category';
$result = $db->cache(function ($db) use ($id) {
    return Category::find()->where(['id' => $id])->all();
}, CACHE_TIMEOUT, $dep);
like image 95
vitalik_74 Avatar answered Nov 15 '22 06:11

vitalik_74


I too am having trouble with this. Here's my workaround for the time being for a hasOne() relationship.

public function getGroup()
{
    if(isset(static::$_getGroup[$this->id])) {
        return static::$_getGroup[$this->id];
    }
    $Group = $this->hasOne(BillChargesGroup::className(), ['id' => 'group_id'])->one();
    static::$_getGroup[$this->id] = $Group;
    return $Group;
}

I only want to cache data for the current request, so this works. However because I'm using ->one(); it does not return the ActiveQuery object if we call $model->getGroup() (which I found is good for extending queries)

Unfortunately if I do return the ActiveQuery object, Yii2 does some "magic" on it and always does a SELECT * which I can't control.

like image 25
oxide246 Avatar answered Nov 15 '22 05:11

oxide246