@@ -85,7 +85,7 @@ private function unmarshalUpToken($uptoken)
8585
8686 public function getBucketHosts ($ ak , $ bucket )
8787 {
88- $ key = $ this ->scheme . $ ak . $ bucket ;
88+ $ key = $ this ->scheme . " : $ ak: $ bucket" ;
8989
9090 $ bucketHosts = $ this ->getBucketHostsFromCache ($ key );
9191 if (count ($ bucketHosts ) > 0 ) {
@@ -112,7 +112,7 @@ private function getBucketHostsFromCache($key)
112112 {
113113 $ ret = array ();
114114 if (count ($ this ->hostCache ) === 0 ) {
115- return $ ret ;
115+ $ this -> hostCacheFromFile () ;
116116 }
117117
118118 if (!array_key_exists ($ key , $ this ->hostCache )) {
@@ -129,9 +129,36 @@ private function getBucketHostsFromCache($key)
129129 private function setBucketHostsToCache ($ key , $ val )
130130 {
131131 $ this ->hostCache [$ key ] = $ val ;
132+ $ this ->hostCacheToFile ();
133+ return ;
134+ }
135+
136+ private function hostCacheFromFile ()
137+ {
138+
139+ $ path = $ this ->hostCacheFilePath ();
140+ if (!file_exists ($ path )) {
141+ return ;
142+ }
143+
144+ $ bucketHosts = file_get_contents ($ path );
145+ $ this ->hostCache = json_decode ($ bucketHosts , true );
132146 return ;
133147 }
134148
149+ private function hostCacheToFile ()
150+ {
151+ $ path = $ this ->hostCacheFilePath ();
152+ file_put_contents ($ path , json_encode ($ this ->hostCache ));
153+ return ;
154+ }
155+
156+ private function hostCacheFilePath ()
157+ {
158+ $ home = getenv ('HOME ' );
159+ return $ home . '/.qiniu_phpsdk_hostscache.json ' ;
160+ }
161+
135162 /* 请求包:
136163 * GET /v1/query?ak=<ak>&&bucket=<bucket>
137164 * 返回包:
@@ -156,6 +183,8 @@ private function bucketHosts($ak, $bucket)
156183 return array (null , new Error ($ url , $ ret ));
157184 }
158185 $ r = ($ ret ->body === null ) ? array () : $ ret ->json ();
186+ echo '----------------------->>>>>>>>>>> ' ;
187+ var_dump ($ r );
159188 return array ($ r , null );
160189 }
161190}
0 commit comments