Answer the question
In order to leave comments, you need to log in
Object caching in Yii2. An example that results in an error. Why?
Faced a problem. There is a function that gets the id of the dialog with the user by his id (user):
public static function getDialogByUserId(int $user_id)
{
$keyCache = 'dlg:byuserid:' . $user_id;
$unionQuery = Yii::$app->redis->get($keyCache);
if (false === $unionQuery) {
$query1 = self::find()
->where(['user_one' => $user_id, 'user_two' => Yii::$app->user->id]);
$query2 = self::find()
->where(['user_two' => $user_id, 'user_one' => Yii::$app->user->id]);
$unionQuery = self::find()
->from(['x' => $query1->union($query2)])
->one();
echo '<pre>';
var_dump($unionQuery);
echo '</pre>';
//Yii::$app->redis->set($keyCache, $unionQuery, 600);
}
return $unionQuery;
}
object(common\models\dialogs\Conversation)#157 (8) {
["_attributes":"yii\db\BaseActiveRecord":private]=>
array(7) {
["c_id"]=>
int(48)
["user_one"]=>
int(8)
["user_two"]=>
int(3)
["last_message"]=>
string(37) "Ну да, типа работает "
["ip"]=>
string(15) "255.255.255.255"
["created_at"]=>
string(19) "2016-07-23 04:05:51"
["updated_at"]=>
string(19) "2016-07-23 04:30:23"
}
["_oldAttributes":"yii\db\BaseActiveRecord":private]=>
array(7) {
["c_id"]=>
int(48)
["user_one"]=>
int(8)
["user_two"]=>
int(3)
["last_message"]=>
string(37) "Ну да, типа работает "
["ip"]=>
string(15) "255.255.255.255"
["created_at"]=>
string(19) "2016-07-23 04:05:51"
["updated_at"]=>
string(19) "2016-07-23 04:30:23"
}
["_related":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_errors":"yii\base\Model":private]=>
NULL
["_validators":"yii\base\Model":private]=>
NULL
["_scenario":"yii\base\Model":private]=>
string(7) "default"
["_events":"yii\base\Component":private]=>
array(2) {
["beforeInsert"]=>
array(1) {
[0]=>
array(2) {
[0]=>
array(2) {
[0]=>
object(yii\behaviors\TimestampBehavior)#159 (6) {
["createdAtAttribute"]=>
string(10) "created_at"
["updatedAtAttribute"]=>
string(10) "updated_at"
["value"]=>
object(Closure)#158 (2) {
["this"]=>
*RECURSION*
["parameter"]=>
array(1) {
["$event"]=>
string(10) ""
}
}
["attributes"]=>
array(2) {
["beforeInsert"]=>
array(2) {
[0]=>
string(10) "created_at"
[1]=>
string(10) "updated_at"
}
["beforeUpdate"]=>
array(1) {
[0]=>
string(10) "updated_at"
}
}
["skipUpdateOnClean"]=>
bool(true)
["owner"]=>
*RECURSION*
}
[1]=>
string(18) "evaluateAttributes"
}
[1]=>
NULL
}
}
["beforeUpdate"]=>
array(1) {
[0]=>
array(2) {
[0]=>
array(2) {
[0]=>
object(yii\behaviors\TimestampBehavior)#159 (6) {
["createdAtAttribute"]=>
string(10) "created_at"
["updatedAtAttribute"]=>
string(10) "updated_at"
["value"]=>
object(Closure)#158 (2) {
["this"]=>
*RECURSION*
["parameter"]=>
array(1) {
["$event"]=>
string(10) ""
}
}
["attributes"]=>
array(2) {
["beforeInsert"]=>
array(2) {
[0]=>
string(10) "created_at"
[1]=>
string(10) "updated_at"
}
["beforeUpdate"]=>
array(1) {
[0]=>
string(10) "updated_at"
}
}
["skipUpdateOnClean"]=>
bool(true)
["owner"]=>
*RECURSION*
}
[1]=>
string(18) "evaluateAttributes"
}
[1]=>
NULL
}
}
}
["_behaviors":"yii\base\Component":private]=>
array(1) {
["timestamp"]=>
object(yii\behaviors\TimestampBehavior)#159 (6) {
["createdAtAttribute"]=>
string(10) "created_at"
["updatedAtAttribute"]=>
string(10) "updated_at"
["value"]=>
object(Closure)#158 (2) {
["this"]=>
*RECURSION*
["parameter"]=>
array(1) {
["$event"]=>
string(10) ""
}
}
["attributes"]=>
array(2) {
["beforeInsert"]=>
array(2) {
[0]=>
string(10) "created_at"
[1]=>
string(10) "updated_at"
}
["beforeUpdate"]=>
array(1) {
[0]=>
string(10) "updated_at"
}
}
["skipUpdateOnClean"]=>
bool(true)
["owner"]=>
*RECURSION*
}
}
}
Allowed memory size of 134217728 bytes exhausted (tried to allocate 2118167486 bytes)
Answer the question
In order to leave comments, you need to log in
1. You receive an object. Everything is fine and there is enough memory as long as it is a standard object, but when you put it anywhere, it is apparently converted to a string. Therefore, there may not be enough memory.
2. For query caching, yii2 has a separate mechanism: www.yiiframework.com/doc-2.0/guide-caching-data.ht...
Forgive me, but you have a problem understanding the meaning of caching. Now you are fetching data from the database (a lot of data) and putting it in the cache. What for???? You are not using the cache effectively. In the cache, you need to put not raw data, but already processed ones. The more stages you combine, the more efficient caching is.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question