MatthiasMullie\Scrapbook\Buffered\Utils\Transaction::cas PHP Method

cas() public method

..). In this scenario, when we finally want to replay the write actions onto the real cache, the first 3 actions would likely work fine. The last (second CAS) however would not, since it never got a real updated $token from the real cache. To work around this problem, all get() calls will return a unique CAS token and store the value-at-that-time associated with that token. All we have to do when we want to write the data to real cache is, right before was CAS for real, get the value & (real) cas token from storage & compare that value to the one we had stored. If that checks out, we can safely resume the CAS with the real token we just received. {@inheritdoc}
public cas ( $token, $key, $value, $expire )
    public function cas($token, $key, $value, $expire = 0)
    {
        $originalValue = isset($this->tokens[$token]) ? $this->tokens[$token] : null;
        // value is no longer the same as what we used for token
        if (serialize($this->get($key)) !== $originalValue) {
            return false;
        }
        // "CAS" value to local cache/memory
        $success = $this->local->set($key, $value, $expire);
        if ($success === false) {
            return false;
        }
        // only schedule the CAS to be performed on real cache if it was OK on
        // local cache
        $this->defer->cas($originalValue, $key, $value, $expire);
        return true;
    }

Usage Example

コード例 #1
0
 /**
  * {@inheritdoc}
  */
 public function cas($token, $key, $value, $expire = 0)
 {
     $result = $this->transaction->cas($token, $key, $value, $expire);
     $this->transaction->commit();
     return $result;
 }