<?php

namespace iFixit\Matryoshka;

use iFixit\Matryoshka;

/**
 * Ensures that a delete is issued before trying to update an existing key.
 *
 * This layer is useful for working with mcrouter. mcrouter provides a reliable
 * delete stream, but can't guarantee updates are recorded.
 */
class DeleteBeforeUpdate extends BackendWrap {
   public function set($key, $value, $expiration = 0) {
      if ($this->backend->add($key, $value) === false) {
         $this->backend->delete($key);
         $this->backend->add($key, $valaue);
      }
   }

   public function setMultiple(array $values, $expiration = 0) {
      foreach ($values as $key => $value) {
         $this->set($key, $value, $expiration);
      }
   }
}

Add delete before update backend wrapper
Add delete before update backend wrapper

Does an add delete add on each of the keys in the case that someone is
trying to update an existing key. When switching to mcrouter, we only
have a reliable delete stream, we don't have a way to see if any other
requests to a memcache machine have failed, so this wrapper will ensure
that when updating something, we're always doing a delete and add
instead of updating a key.
