2009-12-31 101 views
0

我有這樣的兩種方法是否可以讓此代碼更小?

private function cacheAdd($id,$data) 
{ 
    $this->minicache->set($id,$data); 
} 

private function cacheGet($id) 
{ 
    return $this->minicache->get($id); 
} 

每次如果我要檢查的項目會被緩存,我必須做這樣的事情:

public function getFriendIds() 
    { 

$info = $this->cache->minicache->getInfo("getFriendIds"); // if its an array then it is cached 
    if(is_array($info)) 
    { 
     return $this->cache->cacheGet("getFriendIds"); // return the cached object 
    } 
    // from here items wasnt cached 
    else 
    { 
     $this->cache->cacheAdd("getFriendIds",$this->twitter->getFriendIds()); // so add to cache 
     return $this->cache->cacheGet("getFriendIds"); // and return the cached items 
    } 
    } 

但我認爲這是一個簡單的方法來這樣做對嗎?

我想是這樣的:

$this->cache->docache($this->myitems()); 

和方法docache僅需方法和方法名轉換爲字符串,並檢查該項目已經被緩存或不怎麼可能做到?

編輯:

我實現了這個docache方法

public function docache($id,$data) 
    { 
     $info = $this->minicache->getInfo($id); 

     if(is_array($info)) 
     { 
     return $this->cache->cacheGet($id); // return the cached object 
     } 

     else 
     { 
     $this->cacheAdd($id,$data); // so add to cache 
     return $this->cacheGet($id); // and return the cached items 
     } 

    } 

如果我想打電話給我這樣做的方法。

public function getFriendIds() 
    { 
     return $this->cache->docache("getFriendIds",$this->twitter->getFriendIds()); 
    } 

不是這樣小得多嗎?

+1

你有什麼擔心?在時間或空間效率或可讀性方面,沒有太多的「docache」方法可以改進。至於簡化的事情,階級關係可能是一個很好的關注點。爲什麼會有緩存和小型文件?爲什麼緩存方法委託給minicache? 'minicache-> getInfo'做的是'minicache-> get'不是嗎?如果'$ id'不在緩存中,'minicache-> get'會返回什麼? – outis 2009-12-31 13:24:12

+0

嗯...真的很好的問題,minicache是​​一個外部庫 – streetparade 2009-12-31 13:34:53

回答

1

我喜歡它getFriendIds是一種類似模式的方法之一,你想要做的就是使所有的一行(或多或少)。在這種情況下,你可以重構你getFriendIds到你想要的方式:

protected function memoize($name, $callable, $args=array()) { 
    $info = $this->cache->minicache->getInfo($name); // if its an array then it is cached 
    if(is_array($info)) { 
     return $this->cache->cacheGet($name); // return the cached object 
    } else { 
     $this->cache->cacheAdd($name, call_user_func_array($callable, $args)); 
     return $this->cache->cacheGet($name); // and return the cached items 
    } 
} 

public function getFriendIds() { 
    $this->memoize(__METHOD__, array($this->twitter, __FUNCTION__)); 
} 

未經檢驗的,所以有可能會出現一些問題。

+0

是不是正確的docache到我上面實現? – streetparade 2009-12-31 12:57:39

+0

差不多。 'docache'不在你原來的問題中。主要區別在於'memoize'只是在它的值沒有被緩存時(注意'call_user_func_array')懶惰地調用方法,而'docache'則要求你總是調用方法(這會破壞緩存值的目的)。兩者都需要進一步改進:將'$ args'添加到緩存索引。 – outis 2009-12-31 13:27:07

+0

注意:$ callable應該是array(); 感謝有一個美好的一天 – streetparade 2009-12-31 14:43:03

1

您也可以在這裏保存幾行。

public function docache($id,$data) 
{ 
    $info = $this->minicache->getInfo($id); 

    if(!is_array($info)) 
    { 
     $this->cacheAdd($id,$data); // so add to cache 
    } 

    return $this->cache->cacheGet($id); // return the cached object 
} 
1

你可以把它變小一點,速度更快是這樣的: 它只是防止被保存在$信息變量,所以這是一個有點快。 ;) 和代碼要短得多:P

public function docache($id,$data){ 
    if(!is_array($this->minicache->getInfo($id))) $this->cacheAdd($id,$data); // add to cache if theres none 
    return $this->cacheGet($id); // and return the cached items 
} 

編輯:哦,我們發佈了關於在同一時間同一代碼:P

+0

看起來很乾淨,謝謝 – streetparade 2009-12-31 13:10:25