Example #1
0
 /**
  * @param string $key
  * @param int $sub
  * @return int
  * @throws EngineException
  */
 public function countDown(string $key, int $sub = 1) : int
 {
     $value = $this->memcached->decrement($key, $sub, $sub);
     if ($value === false || !is_int($value)) {
         throw EngineException::ioError(__METHOD__, 'Decrement operation failed on Memcached server');
     }
     return $value;
 }
Example #2
0
 /**
  * @return int|null|string
  * @throws EngineException
  */
 private function redisResponse()
 {
     // Get response from stream
     $response = fgets($this->socket);
     if ($response === false) {
         throw EngineException::ioError(__CLASS__, 'Failed to receive response from Redis server');
     }
     // Prepare response for parsing
     $response = trim($response);
     $responseType = substr($response, 0, 1);
     $data = substr($response, 1);
     // Check response
     switch ($responseType) {
         case "-":
             // Error
             throw EngineException::ioError(__CLASS__, substr($data, 4));
             break;
         case "+":
             // Simple String
             return $data;
         case ":":
             // Integer
             return intval($data);
         case "\$":
             // Bulk String
             $bytes = intval($data);
             if ($bytes > 0) {
                 $data = @stream_get_contents($this->socket, $bytes + 2);
                 if ($data === false) {
                     throw EngineException::ioError(__CLASS__, 'Failed to read bulk-string response');
                 }
                 return trim($data);
             } elseif ($bytes === 0) {
                 return "";
                 // Empty String
             } elseif ($bytes === -1) {
                 return null;
                 // NULL
             } else {
                 throw EngineException::ioError(__CLASS__, 'Unexpected number of bytes to read');
             }
             break;
     }
     // Unexpected response from server?
     throw EngineException::ioError(__CLASS__, 'Unexpected response from server');
 }