Yii2 + Redis as Database

China☆狼群 提交于 2020-01-13 05:26:31

问题


I want to use Yii2 and redis as database.

So far, I got Redis ActiveRecord Class for Yii2 from Here.

link1

link2

but, I got a problem. WHY THIS CLASS ADDS ANYTHING AS HASH IN REDIS????

Above that I cant Find in which pattern it Insert data. I add one user and it will add a user under user:xxx namespace and another record under s:user:xxx and so on but none of theme has any fields that i defined in attributes!! only contain IDs.

I know that a Key-value type database and RDBMS are different and also know how can implement relation like records in Redis, but I don't know why it will only save IDs.

I could not find any example of using redis ActiveRecords so far.

There is one in here and its not good enough.

So here is my main wuestion: how can add data to redis Using activeRecords and different data types In YII2?

And if its impossible with ActiveRecords what is the best solution? in this case

ANOTHER QUESTION: is it possible to use a Model instead and write my own model::save() method? and what is the best data validation solution at this rate?

Actually I want to make a telegram bot, so i should get messages and send them in RabitMQ and get data in a worker, do the process and save results to Redis, and finally send response to user through the RabitMQ.

So I need to do a lot of validations AND OF COURSE AUTHENTICATIONS and save and select and range and save to sets an lists and this and that ....

I want a good way to make Model or active record or the proper solution to validation, save and retrieve data to Redis and Yii2.


回答1:


Redis DB can be declared as a cache component or as a database connection or both.

When it is declared as a cache component (using the yii/redis/cache) it is accessible within that component to store key/value pairs as shown here.

$cache = Yii::$app->cache;

// try retrieving $data from cache
$data = $cache->get($key);
// store $data in cache so that it can be retrieved next time
$cache->set($key, $data);

// one more example:
$access_token = Yii::$app->security->generateRandomString();
$cache->add(
    // key
    $access_token, 
    // data (can also be an array)
    [
        'id' => Yii::$app->user->identity->id
        'name' => Yii::$app->user->identity->name
    ], 
    // expires
    60*60*3
);

Also other components may start using it for caching proposes like session if configured to do so or like the yii\web\UrlManager which by default will try to cache the generated URL rules in whatever valid caching mechanism defined under the config file's cache component as explained here. So it is normal to find some stored data other than yours in that case.

When Redis is declared as a DB connection like in the links you provided which means using the yii\redis\Connection class you can make your model extending its \yii\redis\ActiveRecord class as any other ActiveRecord model in Yii. The only difference I know so far is that you need to define your attributes manually as there is no DB schema to parse for NoSQL databases. Then just define your rules, scenarios, relations, events, ... as any other ActiveRecord model:

class Customer extends \yii\redis\ActiveRecord
{
    public function attributes()
    {
        return ['id', 'name', 'address', 'registration_date'];
    }

    public function rules()
    {
        return [
            ['name', 'required'],
            ['name', 'string', 'min' => 3, 'max' => 12, 'on' => 'register'],
            ...
        ];
    }

    public function attributeLabels() {...}
    ...
}

All available methods including save(), validate(), getErrors(), ... could be found here and should be used like any other ActiveRecord class as shown in the official guide.



来源:https://stackoverflow.com/questions/41592402/yii2-redis-as-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!