RiakCache.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace Doctrine\Common\Cache;
  3. use Riak\Bucket;
  4. use Riak\Exception;
  5. use Riak\Input;
  6. use Riak\Object;
  7. use function count;
  8. use function serialize;
  9. use function time;
  10. use function unserialize;
  11. /**
  12. * Riak cache provider.
  13. *
  14. * @link www.doctrine-project.org
  15. *
  16. * @deprecated
  17. */
  18. class RiakCache extends CacheProvider
  19. {
  20. public const EXPIRES_HEADER = 'X-Riak-Meta-Expires';
  21. /** @var Bucket */
  22. private $bucket;
  23. /**
  24. * Sets the riak bucket instance to use.
  25. */
  26. public function __construct(Bucket $bucket)
  27. {
  28. $this->bucket = $bucket;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. protected function doFetch($id)
  34. {
  35. try {
  36. $response = $this->bucket->get($id);
  37. // No objects found
  38. if (! $response->hasObject()) {
  39. return false;
  40. }
  41. // Check for attempted siblings
  42. $object = ($response->hasSiblings())
  43. ? $this->resolveConflict($id, $response->getVClock(), $response->getObjectList())
  44. : $response->getFirstObject();
  45. // Check for expired object
  46. if ($this->isExpired($object)) {
  47. $this->bucket->delete($object);
  48. return false;
  49. }
  50. return unserialize($object->getContent());
  51. } catch (Exception\RiakException $e) {
  52. // Covers:
  53. // - Riak\ConnectionException
  54. // - Riak\CommunicationException
  55. // - Riak\UnexpectedResponseException
  56. // - Riak\NotFoundException
  57. }
  58. return false;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. protected function doContains($id)
  64. {
  65. try {
  66. // We only need the HEAD, not the entire object
  67. $input = new Input\GetInput();
  68. $input->setReturnHead(true);
  69. $response = $this->bucket->get($id, $input);
  70. // No objects found
  71. if (! $response->hasObject()) {
  72. return false;
  73. }
  74. $object = $response->getFirstObject();
  75. // Check for expired object
  76. if ($this->isExpired($object)) {
  77. $this->bucket->delete($object);
  78. return false;
  79. }
  80. return true;
  81. } catch (Exception\RiakException $e) {
  82. // Do nothing
  83. }
  84. return false;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. protected function doSave($id, $data, $lifeTime = 0)
  90. {
  91. try {
  92. $object = new Object($id);
  93. $object->setContent(serialize($data));
  94. if ($lifeTime > 0) {
  95. $object->addMetadata(self::EXPIRES_HEADER, (string) (time() + $lifeTime));
  96. }
  97. $this->bucket->put($object);
  98. return true;
  99. } catch (Exception\RiakException $e) {
  100. // Do nothing
  101. }
  102. return false;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. protected function doDelete($id)
  108. {
  109. try {
  110. $this->bucket->delete($id);
  111. return true;
  112. } catch (Exception\BadArgumentsException $e) {
  113. // Key did not exist on cluster already
  114. } catch (Exception\RiakException $e) {
  115. // Covers:
  116. // - Riak\Exception\ConnectionException
  117. // - Riak\Exception\CommunicationException
  118. // - Riak\Exception\UnexpectedResponseException
  119. }
  120. return false;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. protected function doFlush()
  126. {
  127. try {
  128. $keyList = $this->bucket->getKeyList();
  129. foreach ($keyList as $key) {
  130. $this->bucket->delete($key);
  131. }
  132. return true;
  133. } catch (Exception\RiakException $e) {
  134. // Do nothing
  135. }
  136. return false;
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. protected function doGetStats()
  142. {
  143. // Only exposed through HTTP stats API, not Protocol Buffers API
  144. return null;
  145. }
  146. /**
  147. * Check if a given Riak Object have expired.
  148. */
  149. private function isExpired(Object $object) : bool
  150. {
  151. $metadataMap = $object->getMetadataMap();
  152. return isset($metadataMap[self::EXPIRES_HEADER])
  153. && $metadataMap[self::EXPIRES_HEADER] < time();
  154. }
  155. /**
  156. * On-read conflict resolution. Applied approach here is last write wins.
  157. * Specific needs may override this method to apply alternate conflict resolutions.
  158. *
  159. * {@internal Riak does not attempt to resolve a write conflict, and store
  160. * it as sibling of conflicted one. By following this approach, it is up to
  161. * the next read to resolve the conflict. When this happens, your fetched
  162. * object will have a list of siblings (read as a list of objects).
  163. * In our specific case, we do not care about the intermediate ones since
  164. * they are all the same read from storage, and we do apply a last sibling
  165. * (last write) wins logic.
  166. * If by any means our resolution generates another conflict, it'll up to
  167. * next read to properly solve it.}
  168. *
  169. * @param string $id
  170. * @param string $vClock
  171. * @param array $objectList
  172. *
  173. * @return Object
  174. */
  175. protected function resolveConflict($id, $vClock, array $objectList)
  176. {
  177. // Our approach here is last-write wins
  178. $winner = $objectList[count($objectList) - 1];
  179. $putInput = new Input\PutInput();
  180. $putInput->setVClock($vClock);
  181. $mergedObject = new Object($id);
  182. $mergedObject->setContent($winner->getContent());
  183. $this->bucket->put($mergedObject, $putInput);
  184. return $mergedObject;
  185. }
  186. }