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; }
/** * Get the requested capabilities from the WURFL Cloud for the given HTTP Request (normally $_SERVER) * @param array $http_request HTTP Request of the device being detected * @param array $search_capabilities Array of capabilities that you would like to retrieve */ public function detectDevice($http_request = null, $search_capabilities = null) { $this->source = self::SOURCE_NONE; $this->http_request = $http_request === null ? $_SERVER : $http_request; $this->search_capabilities = $search_capabilities === null ? array() : $search_capabilities; $this->user_agent = $this->getUserAgent($http_request); $result = $this->cache->getDevice($this->user_agent); if (!$result) { $this->source = self::SOURCE_CLOUD; $this->callWurflCloud(); $this->validateCache(); if ($this->source == self::SOURCE_CLOUD) { $this->cache->setDevice($this->user_agent, $this->capabilities); } } else { $this->source = self::SOURCE_CACHE; $this->capabilities = $result; // The user requested capabilities that don't exist in the cached copy. Retrieve and cache the missing capabilities if (!$this->allCapabilitiesPresent()) { $this->source = self::SOURCE_CLOUD; $initial_capabilities = $this->capabilities; $this->callWurflCloud(); $this->capabilities = array_merge($this->capabilities, $initial_capabilities); if ($this->source == self::SOURCE_CLOUD) { $this->cache->setDevice($this->user_agent, $this->capabilities); } } } }