Reputation: 32908
I have this Two Methods
private function cacheAdd($id,$data)
{
$this->minicache->set($id,$data);
}
private function cacheGet($id)
{
return $this->minicache->get($id);
}
Each time if i want to check if the items are cached i have to do something like that:
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
}
}
But i think there is a simple method to do this right?
I thought something like this:
$this->cache->docache($this->myitems());
and method docache takes just the method and converts the methodname to string and checks if the item is already cached or not How could that be done?
EDIT:
I implemented this docache method
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
}
}
and if i want to call the method i do this.
public function getFriendIds()
{
return $this->cache->docache("getFriendIds",$this->twitter->getFriendIds());
}
No this is much smaller isnt it?
Upvotes: 0
Views: 135
Reputation: 1879
You could make it a little smaller (and faster) this way: It only prevents the $info variable from being stored, so it's a little bit faster. ;) And the code is much shorter :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
}
Edit: oh, we posted about the same code at the same time :p
Upvotes: 1
Reputation: 77420
I take it getFriendIds
is one of a slew of methods all of a similar pattern, and what you want to do is make all of them one line (or so) long. In that case, you could refactor you getFriendIds
into the method you desire:
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__));
}
Untested, so there may be some issues.
Upvotes: 1
Reputation: 11
You can save a few lines here, too.
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
}
Upvotes: 1