put() public method

Puts request result into cache for a given command type
public put ( string $commandKey, string $cacheKey, mixed $result )
$commandKey string
$cacheKey string
$result mixed
コード例 #1
0
ファイル: RequestCacheTest.php プロジェクト: odesk/phystrix
 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'));
 }
コード例 #2
0
 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'));
 }
コード例 #3
0
 /**
  * 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;
 }