0

I am using Yii2 and using the yii\rbac\DbManager for auth assignment.

I was looking at the logs to see where all the database calls are coming from and this query

SELECT `b`.* FROM `auth_assignment` `a`, `auth_item` `b` WHERE 
((`a`.`item_name`=`b`.`name`) AND (`a`.`user_id`='91')) AND (`b`.`type`=1)

Keeps running again and again, sometimes 10/15 times in succession.

I have added

    'authManager' => [
        'class' => 'yii\rbac\DbManager',
        'cache' => 'cache'
    ],

As the docs say that will cache the auth assignments (I am using Memcached). But it doesnt seem to work...

Anyone have any idea? Either how to cache it or why it keeps getting called so many times?

Cheers

George Hallam
  • 51
  • 1
  • 4
  • Make sure the application component ID for your cache is correct (double check that you really called it "cache"). Also note that the cache feature for yii\rbac\DbManager has been available since version 2.0.3 so check your Yii2 version. – dataskills Jun 09 '17 at 01:08

2 Answers2

0

https://github.com/yiisoft/yii2/issues/3168

Only cache auth_item, auth_rule and auth_item_child data. All these data are cached as a single entry in cache. Note that auth_assignment is too big to be cached (imagine a system with millions of users).

Rohit Poudel
  • 1,793
  • 2
  • 20
  • 24
0

Add caching in vendor/yiisoft/yii2/rbac/DbManager.php (Also in all places you need caching)

this code:

$all_data = $this->db->cache(function ($db) use ($query) { return $query->all($db); },360);


public function getAssignments($userId)
    {
        if (empty($userId)) {
            return [];
        }

        $query = (new Query)
            ->from($this->assignmentTable)
            ->where(['user_id' => (string) $userId]);


        $all_data = $this->db->cache(function ($db) use ($query) {
                   return $query->all($db);
                  },360);


        $assignments = [];
        foreach ($all_data as $row) {
            $assignments[$row['item_name']] = new Assignment([
                'userId' => $row['user_id'],
                'roleName' => $row['item_name'],
                'createdAt' => $row['created_at'],
            ]);
        }

        return $assignments;
    }