2011-06-06 21 views
0

我想重寫此代碼時沒有太多的「其他」,但仍然保持它的效率,無需檢查事件或在不需要的情況下運行查詢。以較整潔的方式重寫此代碼,沒有那麼多其他的

有人可以提出一個更好的方法來寫這個功能?

public static function fetch($content) { 

    products_library::init(); 
    self::$cache = $cache = url::assetsPath() . '../cache/soldout_cache'; 

    //check the cache 
    if (file_exists($cache)) { 
     $cache_date = filectime($cache); 
     db::select('date_modified'); 
     db::orderBy('date_modified DESC'); 
     db::limit(1); 
     $mod_date = db::get('sc_module_products')->fetch(PDO::FETCH_ASSOC); 
     if ($mod_date) { 
      $mod_date = strtotime('date_modified'); 
      if ($cache_date >= $mod_date) { //serve the cache 
       try { 
        $soldout = filewriter::read($cache); 
        $soldout = unserialize($soldout); 
       } catch (Exception $e) { 
        $soldout = self::query(); 
       } 
      } 
      else 
       $soldout = self::query(); 
     } 
     else 
      $soldout = self::query(); 
    } 
    else 
     $soldout = self::query(); 

    $data['items'] = $soldout; // print_r($items); exit; 

    $html = view::load('Product_Display', $data, true); 
    return $html; 
} 

感謝

+0

怎麼了downvote? – Arend 2011-06-06 00:49:19

+0

靜態上癮? :) – Nemoden 2011-06-06 00:49:38

+0

沒人是完美的。 – Arend 2011-06-06 01:00:30

回答

2

重構它變成返回,而不是else語句

private static function getSoldout() { 
    self::$cache = $cache = url::assetsPath() . '../cache/soldout_cache'; 

    //check the cache 
    if (!file_exists($cache)) { 
     return self::query(); 
    } 

    $cache_date = filectime($cache); 
    db::select('date_modified'); 
    db::orderBy('date_modified DESC'); 
    db::limit(1); 
    $mod_date = db::get('sc_module_products')->fetch(PDO::FETCH_ASSOC); 
    if (!$mod_date) { 
     return self::query(); 
    } 

    $mod_date = strtotime('date_modified'); 
    if ($cache_date < $mod_date) { 
     return self::query(); 
    } 

    try { 
     //serve the cache 
     $soldout = filewriter::read($cache); 
     $soldout = unserialize($soldout); 
     return $soldout; 
    } catch (Exception $e) { 
     return self::query(); 
    } 
} 

public static function fetch($content) { 

    products_library::init(); 

    $soldout = self::getSoldout(); 

    $data['items'] = $soldout; // print_r($items); exit; 

    $html = view::load('Product_Display', $data, true); 
    return $html; 
} 

我不明白,這條線的方法,有沒有錯誤呢?

$mod_date = strtotime('date_modified'); 
+0

是@!非常好的點+1。這是一個bug – Jason 2011-06-07 03:56:08

+0

夥計。你的代碼很棒。我屈服於你的技能 – Jason 2011-06-07 03:57:55

0

在我看來,如果你可以通過之前,首先將其設置成該默認值$索多特是自::查詢(),如果支票然後刪除所有的別人的,所以如果條件不匹配它仍然是self :: query()。根據self :: query()的作用,可能無法工作。

+0

-1:該函數的全部想法是避免調用數據庫,如果信息已被緩存。 – 2011-06-06 00:54:32

1

將$ soldout設置爲NULL。然後刪除else $soldout = self::query()聲明。

if語句測試$ soldout爲NULL並且它真正運行查詢之後。

1

開關盒在這裏可以創造奇蹟。你只需要一個break陳述,指出一個默認情況。然而,如果我在你的鞋子裏,我會試圖重構整個事情,這不僅僅是一個快速解決方案。

1

這樣的事情可能會奏效。我不確定在所有ifs中發生了什麼,爲什麼你需要這麼多,它可能更緊湊。

public static function fetch($content) { 

    products_library::init(); 
    self::$cache = $cache = url::assetsPath() . '../cache/soldout_cache'; 

    $soldout = self::fetchCache($cache); 
    if ($soldout === false) 
    { 
     $soldout = self::query(); 
    } 

    $data['items'] = $soldout; // print_r($items); exit; 

    $html = view::load('Product_Display', $data, true); 
    return $html; 
} 

public static function fetchCache($cache) { 
    if (file_exists($cache)) { 
     $cache_date = filectime($cache); 
     db::select('date_modified'); 
     db::orderBy('date_modified DESC'); 
     db::limit(1); 
     $mod_date = db::get('sc_module_products')->fetch(PDO::FETCH_ASSOC); 
     if ($mod_date) { 
      $mod_date = strtotime('date_modified'); 
      if ($cache_date >= $mod_date) { //serve the cache 
       try { 
        $result = filewriter::read($cache); 
        $result = unserialize($soldout); 
        return $result; 
       } catch (Exception $e) { 
        return false; 
       } 
      } 
     } 
    } 
    return false; 
} 
相關問題