Exemple #1
0
function testCache(WurflCloud_Cache_CacheInterface &$cache)
{
    $max_write = 100;
    $max_unique = 20;
    $ret = array('success' => 0, 'error' => 0, 'write_avg' => '', 'read_avg' => '');
    $start = microtime(true);
    $a = 0;
    for ($i = 0; $i < $max_write; $i++) {
        if ($a >= $max_unique) {
            $a = 0;
        }
        if ($cache->setDevice("WurflCloud/{$i} Foobar", array('id' => 'foobar_ver1_subua' . $a, 'model_name' => 'foobar'))) {
            $ret['success']++;
        } else {
            $ret['error']++;
        }
        $a++;
    }
    $time = microtime(true) - $start;
    $ret['write_avg'] = round($time / $max_write * 1000, 3) . " ms";
    $start = microtime(true);
    for ($i = 0; $i < $max_write; $i++) {
        $result = $cache->getDevice("WurflCloud/{$i} Foobar");
        if (is_array($result) && isset($result['id']) && strpos($result['id'], 'foobar_ver1') !== false) {
            $ret['success']++;
        } else {
            $ret['error']++;
        }
    }
    $time = microtime(true) - $start;
    $ret['read_avg'] = round($time / $max_write * 1000, 3) . " ms";
    return $ret;
}
 /**
  * Returns the value of the requested capability.  If the capability does not exist, returns null.
  * @param string $capability The WURFL capability (e.g. "is_wireless_device")
  * @return mixed Value of requested $capability or null if not found
  * @throws WurflCloud_Client_InvalidCapabilityException The requested capability is invalid or unavailable
  */
 public function getDeviceCapability($capability)
 {
     $capability = strtolower($capability);
     if (array_key_exists($capability, $this->capabilities)) {
         return $this->capabilities[$capability];
     } else {
         if (!$this->http_client->wasCalled()) {
             // The capability is not in the cache (http_client was not called) - query the Cloud
             // to see if we even have the capability
             $this->source = 'cloud';
             $this->callWurflCloud();
             $this->validateCache();
             if ($this->source == 'cloud') {
                 $this->cache->setDevice($this->user_agent, $this->capabilities);
             }
             if (array_key_exists($capability, $this->capabilities)) {
                 return $this->capabilities[$capability];
             }
         }
         // The Cloud was queried and did not return a result for the this capability
         throw new WurflCloud_Client_InvalidCapabilityException('The requested capability (' . $capability . ') is invalid or you are not subscribed to it.');
     }
 }