<?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, $expiration) === false) {
         $this->backend->delete($key);
         $this->backend->set($key, $value, $expiration);
      }
   }

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

<?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) {
      $addResponse = $this->backend->add($key, $value, $expiration);
      if ($addResponse === false) {
         $this->backend->delete($key);
         return $this->backend->set($key, $value, $expiration);
      }
      return $addResponse;
   }

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

Add return values for BackendWrap
Add return values for BackendWrap

We need to return some values from the BackendWrap. It is not trivial
what we are expecting here, especially from `setMultiple`. Always
returning `true` should be satisfactory for now.
