Skip to content

Commit 27f9de9

Browse files
committed
add ttl to getRegion
1 parent 0b34e66 commit 27f9de9

File tree

2 files changed

+95
-54
lines changed

2 files changed

+95
-54
lines changed

src/Qiniu/Config.php

Lines changed: 73 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -232,46 +232,94 @@ public function getApiHostV2($accessKey, $bucket)
232232
return array($scheme . $region->apiHost, null);
233233
}
234234

235+
236+
/**
237+
* 从缓存中获取区域
238+
*
239+
* @param string $cacheId 缓存 ID
240+
* @return null|Region
241+
*/
242+
private function getRegionCache($cacheId)
243+
{
244+
if (isset($this->regionCache[$cacheId]) &&
245+
isset($this->regionCache[$cacheId]["deadline"]) &&
246+
time() < $this->regionCache[$cacheId]["deadline"]
247+
) {
248+
return $this->regionCache[$cacheId]["region"];
249+
}
250+
251+
return null;
252+
}
253+
254+
/**
255+
* 将区域设置到缓存中
256+
*
257+
* @param string $cacheId 缓存 ID
258+
* @param Region $region 缓存 ID
259+
* @return void
260+
*/
261+
private function setRegionCache($cacheId, $region)
262+
{
263+
$this->regionCache[$cacheId] = array(
264+
"region" => $region,
265+
);
266+
if (isset($region->ttl)) {
267+
$this->regionCache[$cacheId]["deadline"] = time() + $region->ttl;
268+
}
269+
}
270+
271+
/**
272+
* 从缓存中获取区域
273+
*
274+
* @param string $accessKey
275+
* @param string $bucket
276+
* @return Region
277+
*
278+
* @throws \Exception
279+
*/
235280
private function getRegion($accessKey, $bucket)
236281
{
282+
if (isset($this->zone)) {
283+
return $this->zone;
284+
}
285+
237286
$cacheId = "$accessKey:$bucket";
287+
$regionCache = $this->getRegionCache($cacheId);
288+
if ($regionCache) {
289+
return $regionCache;
290+
}
238291

239-
if (isset($this->regionCache[$cacheId])) {
240-
$region = $this->regionCache[$cacheId];
241-
} elseif (isset($this->zone)) {
242-
$region = $this->zone;
243-
$this->regionCache[$cacheId] = $region;
244-
} else {
245-
$region = Zone::queryZone($accessKey, $bucket);
246-
if (is_array($region)) {
247-
list($region, $err) = $region;
248-
if ($err != null) {
249-
throw new \Exception($err->message());
250-
}
292+
$region = Zone::queryZone($accessKey, $bucket);
293+
if (is_array($region)) {
294+
list($region, $err) = $region;
295+
if ($err != null) {
296+
throw new \Exception($err->message());
251297
}
252-
$this->regionCache[$cacheId] = $region;
253298
}
299+
300+
$this->setRegionCache($cacheId, $region);
254301
return $region;
255302
}
256303

257304
private function getRegionV2($accessKey, $bucket)
258305
{
306+
if (isset($this->zone)) {
307+
return array($this->zone, null);
308+
}
309+
259310
$cacheId = "$accessKey:$bucket";
311+
$regionCache = $this->getRegionCache($cacheId);
312+
if (isset($regionCache)) {
313+
return array($regionCache, null);
314+
}
260315

261-
if (isset($this->regionCache[$cacheId])) {
262-
$region = $this->regionCache[$cacheId];
263-
} elseif (isset($this->zone)) {
264-
$region = $this->zone;
265-
$this->regionCache[$cacheId] = $region;
266-
} else {
267-
$region = Zone::queryZone($accessKey, $bucket);
268-
if (is_array($region)) {
269-
list($region, $err) = $region;
270-
return array($region, $err);
271-
}
272-
$this->regionCache[$cacheId] = $region;
316+
$region = Zone::queryZone($accessKey, $bucket);
317+
if (is_array($region)) {
318+
list($region, $err) = $region;
319+
return array($region, $err);
273320
}
274321

322+
$this->setRegionCache($cacheId, $region);
275323
return array($region, null);
276324
}
277325
}

src/Qiniu/Region.php

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Region
1919
public $apiHost;
2020
//IOVIP域名
2121
public $iovipHost;
22+
// TTL
23+
public $ttl;
2224

2325
//构造一个Region对象
2426
public function __construct(
@@ -27,7 +29,8 @@ public function __construct(
2729
$rsHost = "rs-z0.qiniuapi.com",
2830
$rsfHost = "rsf-z0.qiniuapi.com",
2931
$apiHost = "api.qiniuapi.com",
30-
$iovipHost = null
32+
$iovipHost = null,
33+
$ttl = null
3134
) {
3235

3336
$this->srcUpHosts = $srcUpHosts;
@@ -36,6 +39,7 @@ public function __construct(
3639
$this->rsfHost = $rsfHost;
3740
$this->apiHost = $apiHost;
3841
$this->iovipHost = $iovipHost;
42+
$this->ttl = $ttl;
3943
}
4044

4145
//华东机房
@@ -170,7 +174,7 @@ public static function regionSeoul()
170174
public static function queryRegion($ak, $bucket)
171175
{
172176
$Region = new Region();
173-
$url = Config::API_HOST . '/v2/query' . "?ak=$ak&bucket=$bucket";
177+
$url = 'https://' . Config::UC_HOST . '/v2/query' . "?ak=$ak&bucket=$bucket";
174178
$ret = Client::Get($url);
175179
if (!$ret->ok()) {
176180
return array(null, new Error($url, $ret));
@@ -196,35 +200,24 @@ public static function queryRegion($ak, $bucket)
196200
}
197201

198202
//set specific hosts
199-
if (strstr($Region->iovipHost, "z1") !== false) {
200-
$Region->rsHost = "rs-z1.qiniuapi.com";
201-
$Region->rsfHost = "rsf-z1.qiniuapi.com";
202-
$Region->apiHost = "api-z1.qiniuapi.com";
203-
} elseif (strstr($Region->iovipHost, "z2") !== false) {
204-
$Region->rsHost = "rs-z2.qiniuapi.com";
205-
$Region->rsfHost = "rsf-z2.qiniuapi.com";
206-
$Region->apiHost = "api-z2.qiniuapi.com";
207-
} elseif (strstr($Region->iovipHost, "cn-east-2") !== false) {
208-
$Region->rsHost = "rs-cn-east-2.qiniuapi.com";
209-
$Region->rsfHost = "rsf-cn-east-2.qiniuapi.com";
210-
$Region->apiHost = "api-cn-east-2.qiniuapi.com";
211-
} elseif (strstr($Region->iovipHost, "na0") !== false) {
212-
$Region->rsHost = "rs-na0.qiniuapi.com";
213-
$Region->rsfHost = "rsf-na0.qiniuapi.com";
214-
$Region->apiHost = "api-na0.qiniuapi.com";
215-
} elseif (strstr($Region->iovipHost, "as0") !== false) {
216-
$Region->rsHost = "rs-as0.qiniuapi.com";
217-
$Region->rsfHost = "rsf-as0.qiniuapi.com";
218-
$Region->apiHost = "api-as0.qiniuapi.com";
219-
} elseif (strstr($Region->iovipHost, "ap-northeast-1") !== false) {
220-
$Region->rsHost = "rs-ap-northeast-1.qiniuapi.com";
221-
$Region->rsfHost = "rsf-ap-northeast-1.qiniuapi.com";
222-
$Region->apiHost = "api-ap-northeast-1.qiniuapi.com";
203+
if (isset($r['rs']['acc']['main'])) {
204+
$Region->rsHost = $r['rs']['acc']['main'];
223205
} else {
224-
$Region->rsHost = "rs.qiniuapi.com";
225-
$Region->rsfHost = "rsf.qiniuapi.com";
226-
$Region->apiHost = "api.qiniuapi.com";
206+
$Region->rsHost = Config::RS_HOST;
227207
}
208+
if (isset($r['rs']['rsf']['main'])) {
209+
$Region->rsfHost = $r['rs']['rsf']['main'];
210+
} else {
211+
$Region->rsfHost = Config::RSF_HOST;
212+
}
213+
if (isset($r['rs']['api']['main'])) {
214+
$Region->apiHost = $r['rs']['api']['main'];
215+
} else {
216+
$Region->apiHost = Config::API_HOST;
217+
}
218+
219+
// set ttl
220+
$Region->ttl = $r['ttl'];
228221

229222
return $Region;
230223
}

0 commit comments

Comments
 (0)