views:

92

answers:

3

Hello everyone

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?

+1  A: 

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.

outis
isnt that exactly the docache wich i implemeted above?
streetparade
Almost. `docache` wasn't in your original question. The main difference is that `memoize` calls the method lazily and only if its value is not cached (note the `call_user_func_array`), while `docache` requires you to always call the method (which defeats the purpose of caching values). Both need a further refinement: add `$args` to cache indexing.
outis
NOTE: $callable should be array();thanks have a nice day
streetparade
Do you mean $callable should be type-hinted as an array?
outis
+1  A: 

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
}
Pete
+1  A: 

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

Daan
looks clean thanks
streetparade