@@ -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 * 返回包:
0 commit comments