English 中文(简体)
使该法缩小的可能性?
原标题:Posible to make this Code smaller?

我有两种方法。

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());

如果该项目已经过时或无法完成,那么这种方法和办法就只能采用这种方法,并将方法名称转换为监督和检查?

EDIT:

I 采用了这一方法

   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());
  }

没有一个小的主子?

最佳回答

我认为这是所有类似模式方法的飞跃之一,而你们想要做的是使所有这些方法都成为一条线(或这样)。 在此情况下,您可以将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__));
}

未经测试,可能有一些问题。

问题回答

在这里,你也可以拯救几个线。

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
}

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,我们同时贴上同样的法典:p





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签