AIParking_Intops_Server/components/CacheRbacDbManager.php
2020-02-01 16:47:12 +07:00

148 lines
3.9 KiB
PHP

<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace app\components;
use yii\helpers\ArrayHelper;
/**
* Description of CacheRbacManager
*
* @author Kdg
*/
class CacheRbacDbManager extends \yii\rbac\DbManager {
/**
* @var string the ID of the cache application component that is used to cache rbac.
* Defaults to 'cache' which refers to the primary cache application component.
*/
public $cache = 'cache';
/**
* @var integer Lifetime of cached data in seconds
*/
public $cacheDuration = 3600;
/**
* @var string cache key name
*/
public $cacheKeyName = 'RbacCached';
/**
* @var array php cache
*/
protected $cachedData = [];
/**
* @inheritdoc
*/
public function checkAccess($userId, $permissionName, $params = []) {
if (!empty($params))
return parent::checkAccess($userId, $permissionName, $params);
$cacheKey = 'checkAccess:' . $userId . ':' . $permissionName;
$cached = $this->getValue($cacheKey);
if (empty($cached)) {
$cached = parent::checkAccess($userId, $permissionName);
$this->setValue($cacheKey, $cached);
}
return $cached;
}
/**
* @inheritdoc
*/
protected function checkAccessRecursive($user, $itemName, $params, $assignments) {
$cacheKey = 'checkAccessRecursive:' . $user . ':' . $itemName;
if (!empty($params))
$cacheKey .= ':' . current($params)->primaryKey;
$cached = $this->getValue($cacheKey);
if (empty($cached)) {
$cached = parent::checkAccessRecursive($user, $itemName, $params, $assignments);
$this->setValue($cacheKey, $cached);
}
return $cached;
}
/**
* @inheritdoc
*/
protected function getItem($name) {
$cacheKey = 'Item:' . $name;
$cached = $this->getValue($cacheKey);
if (empty($cached)) {
$cached = parent::getItem($name);
$this->setValue($cacheKey, $cached);
}
return $cached;
}
/**
* @inheritdoc
*/
public function getAssignments($userId) {
if (empty($userId))
return parent::getAssignments($userId);
$cacheKey = 'Assignments:' . $userId;
$cached = $this->getValue($cacheKey);
if (empty($cached)) {
$cached = parent::getAssignments($userId);
$this->setValue($cacheKey, $cached);
}
return $cached;
}
/**
* Set a value in cache
* @param $key
* @param $value
* @return mixed
*/
protected function setValue($key, $value) {
$this->cachedData = $this->getCache()->get($this->cacheKeyName);
if (empty($this->cachedData))
$this->cachedData = [];
$this->cachedData[$key] = $value;
return $this->getCache()->set($this->cacheKeyName, $this->cachedData, $this->cacheDuration);
}
/**
* Get cached value
* @param $key
* @return mixed
*/
protected function getValue($key) {
$cached = ArrayHelper::getValue($this->cachedData, $key);
if (!isset($cached)) {
$cacheData = $this->getCache()->get($this->cacheKeyName);
$cached = $this->cachedData[$key] = ArrayHelper::getValue($cacheData, $key);
}
return $cached;
}
/**
* Get cached value
* @param $key
* @return mixed
*/
public function deleteAllCache() {
return $this->getCache()->delete($this->cacheKeyName);
}
/**
*
* @return \yii\caching\Cache
*/
private function getCache() {
if (is_string($this->cache)) {
$this->cache = \Yii::$app->get($this->cache);
}
return $this->cache;
}
}