Skip to content

Commit

Permalink
修改 new Srun 类可以石油Cache类了
Browse files Browse the repository at this point in the history
  • Loading branch information
luguohuakai committed Aug 28, 2023
1 parent a7e925b commit 7a83cd6
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/Srun.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Srun implements \srun\base\Srun
private $srun_north_access_token = '';
private $srun_north_access_token_expire = 7200;
private $srun_north_access_token_redis_key = 'srun_north_access_token_redis';
private $use_this_rds = false;
private $rds_config = [
'index' => 0,
'port' => 6379,
Expand All @@ -27,6 +28,7 @@ public function __construct($srun_north_api_url = null, $srun_north_access_token
// 自动判断当前环境是否为srun4k
$system_conf_file = '/srun3/etc/system.conf';
if (is_file($system_conf_file)) {
$this->use_this_rds = true;
$system_conf = parse_ini_file($system_conf_file);
$this->rds_config['port'] = 16382;
$this->rds_config['host'] = $system_conf['user_server'];
Expand All @@ -36,6 +38,7 @@ public function __construct($srun_north_api_url = null, $srun_north_access_token

public function setRdsConfig($rds_config)
{
$this->use_this_rds = true;
$this->rds_config = array_merge($this->rds_config, $rds_config);
}

Expand Down Expand Up @@ -109,15 +112,30 @@ public function userRight($user_name, $pwd): bool
public function accessToken()
{
if ($this->srun_north_access_token) return $this->srun_north_access_token;
$access_token = Func::Rds($this->rds_config['index'], $this->rds_config['port'], $this->rds_config['host'], $this->rds_config['pass'])->get($this->srun_north_access_token_redis_key);
if ($access_token) return $access_token;
$rs = $this->req('api/v1/auth/get-access-token', [], 'get', false);
if (isset($rs->data) && isset($rs->data->access_token)) {
// 缓存令牌
Func::Rds($this->rds_config['index'], $this->rds_config['port'], $this->rds_config['host'], $this->rds_config['pass'])->setex($this->srun_north_access_token_redis_key, $this->srun_north_access_token_expire, $rs->data->access_token);
return $rs->data->access_token;

if ($this->use_this_rds) {
$access_token = Func::Rds($this->rds_config['index'], $this->rds_config['port'], $this->rds_config['host'], $this->rds_config['pass'])->get($this->srun_north_access_token_redis_key);
if ($access_token) return $access_token;
$rs = $this->req('api/v1/auth/get-access-token', [], 'get', false);
if (isset($rs->data) && isset($rs->data->access_token)) {
// 缓存令牌
Func::Rds($this->rds_config['index'], $this->rds_config['port'], $this->rds_config['host'], $this->rds_config['pass'])->setex($this->srun_north_access_token_redis_key, $this->srun_north_access_token_expire, $rs->data->access_token);
return $rs->data->access_token;
} else {
return false;
}
} else {
return false;
$cache = new Cache;
$access_token = $cache->get($this->srun_north_access_token_redis_key);
if ($access_token) return $access_token;
$rs = $this->req('api/v1/auth/get-access-token', [], 'get', false);
if (isset($rs->data) && isset($rs->data->access_token)) {
// 缓存令牌
$cache->set($this->srun_north_access_token_redis_key, $rs->data->access_token, $this->srun_north_access_token_expire);
return $rs->data->access_token;
} else {
return false;
}
}
}

Expand Down

0 comments on commit 7a83cd6

Please sign in to comment.