Exemplo n.º 1
0
 /**
  * 获取内容并输出
  * 如果stroage里面不存在,则从URL里面获取
  * */
 private function handle($filename, $key, $delete = false, $direct = false)
 {
     $content = '';
     if ($this->succeed) {
         $storage = storage::gethandle();
         if ($delete) {
             if (!$storage->exists($key)) {
                 die(json_encode(array('purge' => $filename, 'key' => $key, 'success' => 'not exists')));
             }
             $return = $storage->delete($key);
             die(json_encode(array('purge' => $filename, 'key' => $key, 'success' => $return)));
         }
         if ($storage->exists($key) && !$direct) {
             if (!NO_LOCATE && ($url = $storage->url($key))) {
                 $this->locate($url);
             }
             $content = $storage->read($key);
             $this->hit = $key;
             if (empty($content)) {
                 $this->succeed = false;
                 $this->error_type = 'empty_conent';
             }
         } else {
             //$content = @file_get_contents(BASE_URL.$filename);
             $content = lib::fetch_url(BASE_URL . $filename);
             if (!is_array($content) || count($content) < 2) {
                 $this->succeed = false;
                 $this->error_type = 'fetch_error';
             } elseif ($content[0] == 200) {
                 //返回200,才写入
                 if (!$direct) {
                     $storage->write($key, $content[1]);
                 }
             } else {
                 header('HTTP/1.1 ' . $content[0]);
             }
             $content = $content[1];
         }
     }
     //显示内容
     $this->render($content);
 }