Skip to content

Commit 2756abc

Browse files
authored
Merge pull request #192 from rwifeng/multi_zones
Multi zones
2 parents 94350e0 + 2e5821c commit 2756abc

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

src/Qiniu/Zone.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ public function getIoHost($ak, $bucket)
4343
{
4444
list($bucketHosts,) = $this->getBucketHosts($ak, $bucket);
4545
$ioHosts = $bucketHosts['ioHost'];
46-
if (count($ioHosts) === 0) {
47-
return "";
48-
}
49-
5046
return $ioHosts[0];
5147
}
5248

@@ -85,7 +81,7 @@ private function unmarshalUpToken($uptoken)
8581

8682
public function getBucketHosts($ak, $bucket)
8783
{
88-
$key = $this->scheme . $ak . $bucket;
84+
$key = $this->scheme . ":$ak:$bucket";
8985

9086
$bucketHosts = $this->getBucketHostsFromCache($key);
9187
if (count($bucketHosts) > 0) {
@@ -112,7 +108,7 @@ private function getBucketHostsFromCache($key)
112108
{
113109
$ret = array();
114110
if (count($this->hostCache) === 0) {
115-
return $ret;
111+
$this->hostCacheFromFile();
116112
}
117113

118114
if (!array_key_exists($key, $this->hostCache)) {
@@ -129,9 +125,36 @@ private function getBucketHostsFromCache($key)
129125
private function setBucketHostsToCache($key, $val)
130126
{
131127
$this->hostCache[$key] = $val;
128+
$this->hostCacheToFile();
132129
return;
133130
}
134131

132+
private function hostCacheFromFile()
133+
{
134+
135+
$path = $this->hostCacheFilePath();
136+
if (!file_exists($path)) {
137+
return;
138+
}
139+
140+
$bucketHosts = file_get_contents($path);
141+
$this->hostCache = json_decode($bucketHosts, true);
142+
return;
143+
}
144+
145+
private function hostCacheToFile()
146+
{
147+
$path = $this->hostCacheFilePath();
148+
file_put_contents($path, json_encode($this->hostCache), LOCK_EX);
149+
return;
150+
}
151+
152+
private function hostCacheFilePath()
153+
{
154+
$home = getenv('HOME');
155+
return $home . '/.qiniu_phpsdk_hostscache.json';
156+
}
157+
135158
/* 请求包:
136159
* GET /v1/query?ak=<ak>&&bucket=<bucket>
137160
* 返回包:

tests/Qiniu/Tests/ZoneTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,20 @@ public function testUpHosts()
5050
$this->assertNull($err);
5151
$this->assertEquals('https://up-z1.qbox.me', $upHosts[0]);
5252
}
53+
54+
public function testIoHosts()
55+
{
56+
57+
$ioHost = $this->zone->getIoHost($this->ak, $this->bucketName);
58+
$this->assertEquals('http://iovip.qbox.me', $ioHost);
59+
60+
$ioHost = $this->zone->getIoHost($this->ak, $this->bucketNameBC);
61+
$this->assertEquals('http://iovip-z1.qbox.me', $ioHost);
62+
63+
$ioHost = $this->zoneHttps->getIoHost($this->ak, $this->bucketName);
64+
$this->assertEquals('https://iovip.qbox.me', $ioHost);
65+
66+
$ioHost = $this->zoneHttps->getIoHost($this->ak, $this->bucketNameBC);
67+
$this->assertEquals('https://iovip-z1.qbox.me', $ioHost);
68+
}
5369
}

0 commit comments

Comments
 (0)