public function testExists() { $result = (object) array('a' => 123); $this->assertFalse($this->requestCache->exists('TestCommand', 'cache-key-123')); $this->requestCache->put('TestCommand', 'cache-key-123', $result); $this->assertTrue($this->requestCache->exists('TestCommand', 'cache-key-123')); }
public function testClearAll() { $result = (object) array('a' => 123); $this->requestCache->put('TestCommand', 'cache-key-123', $result); $this->assertEquals($result, $this->requestCache->get('TestCommand', 'cache-key-123')); $this->requestCache->clearAll('TestCommand'); $this->assertNull($this->requestCache->get('TestCommand', 'cache-key-123')); }
/** * Executes the command * Isolation and fault tolerance logic (Circuit Breaker) is built-in * * @return mixed * @throws BadRequestException Re-throws it when the command throws it, without metrics tracking */ public function execute() { $this->prepare(); $metrics = $this->getMetrics(); $cacheEnabled = $this->isRequestCacheEnabled(); // always adding the command to request log $this->recordExecutedCommand(); // trying from cache first if ($cacheEnabled) { $fromCache = $this->requestCache->get($this->getCommandKey(), $this->getCacheKey()); if ($fromCache !== null) { $metrics->markResponseFromCache(); $this->recordExecutionEvent(self::EVENT_RESPONSE_FROM_CACHE); return $fromCache; } } $circuitBreaker = $this->getCircuitBreaker(); if (!$circuitBreaker->allowRequest()) { $metrics->markShortCircuited(); $this->recordExecutionEvent(self::EVENT_SHORT_CIRCUITED); return $this->getFallbackOrThrowException(); } $this->invocationStartTime = $this->getTimeInMilliseconds(); try { $result = $this->run(); $this->recordExecutionTime(); $metrics->markSuccess(); $circuitBreaker->markSuccess(); $this->recordExecutionEvent(self::EVENT_SUCCESS); } catch (Exception $exception) { if (in_array(get_class($exception), $this->config->get('badRequestException', array('\\Odesk\\Phystrix\\Exception\\BadRequestException')))) { // Treated differently and allowed to propagate without any stats tracking or fallback logic $this->recordExecutionTime(); throw $exception; } $this->recordExecutionTime(); $metrics->markFailure(); $this->executionException = $exception; $this->recordExecutionEvent(self::EVENT_FAILURE); $result = $this->getFallbackOrThrowException($exception); } // putting the result into cache if ($cacheEnabled) { $this->requestCache->put($this->getCommandKey(), $this->getCacheKey(), $result); } return $result; }