skip to Main Content

I am using Yii2 and using the yiirbacDbManager 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' => 'yiirbacDbManager',
        '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

2

Answers


  1. 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).

    Login or Signup to reply.
  2. 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;
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search