3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\Common\Cache;
28 * Riak cache provider.
30 * @link www.doctrine-project.org
32 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
34 class RiakCache extends CacheProvider
36 const EXPIRES_HEADER = 'X-Riak-Meta-Expires';
44 * Sets the riak bucket instance to use.
46 * @param \Riak\Bucket $bucket
48 public function __construct(Bucket $bucket)
50 $this->bucket = $bucket;
56 protected function doFetch($id)
59 $response = $this->bucket->get($id);
62 if ( ! $response->hasObject()) {
66 // Check for attempted siblings
67 $object = ($response->hasSiblings())
68 ? $this->resolveConflict($id, $response->getVClock(), $response->getObjectList())
69 : $response->getFirstObject();
71 // Check for expired object
72 if ($this->isExpired($object)) {
73 $this->bucket->delete($object);
78 return unserialize($object->getContent());
79 } catch (Exception\RiakException $e) {
81 // - Riak\ConnectionException
82 // - Riak\CommunicationException
83 // - Riak\UnexpectedResponseException
84 // - Riak\NotFoundException
93 protected function doContains($id)
96 // We only need the HEAD, not the entire object
97 $input = new Input\GetInput();
99 $input->setReturnHead(true);
101 $response = $this->bucket->get($id, $input);
104 if ( ! $response->hasObject()) {
108 $object = $response->getFirstObject();
110 // Check for expired object
111 if ($this->isExpired($object)) {
112 $this->bucket->delete($object);
118 } catch (Exception\RiakException $e) {
128 protected function doSave($id, $data, $lifeTime = 0)
131 $object = new Object($id);
133 $object->setContent(serialize($data));
136 $object->addMetadata(self::EXPIRES_HEADER, (string) (time() + $lifeTime));
139 $this->bucket->put($object);
142 } catch (Exception\RiakException $e) {
152 protected function doDelete($id)
155 $this->bucket->delete($id);
158 } catch (Exception\BadArgumentsException $e) {
159 // Key did not exist on cluster already
160 } catch (Exception\RiakException $e) {
162 // - Riak\Exception\ConnectionException
163 // - Riak\Exception\CommunicationException
164 // - Riak\Exception\UnexpectedResponseException
173 protected function doFlush()
176 $keyList = $this->bucket->getKeyList();
178 foreach ($keyList as $key) {
179 $this->bucket->delete($key);
183 } catch (Exception\RiakException $e) {
193 protected function doGetStats()
195 // Only exposed through HTTP stats API, not Protocol Buffers API
200 * Check if a given Riak Object have expired.
202 * @param \Riak\Object $object
206 private function isExpired(Object $object) : bool
208 $metadataMap = $object->getMetadataMap();
210 return isset($metadataMap[self::EXPIRES_HEADER])
211 && $metadataMap[self::EXPIRES_HEADER] < time();
215 * On-read conflict resolution. Applied approach here is last write wins.
216 * Specific needs may override this method to apply alternate conflict resolutions.
218 * {@internal Riak does not attempt to resolve a write conflict, and store
219 * it as sibling of conflicted one. By following this approach, it is up to
220 * the next read to resolve the conflict. When this happens, your fetched
221 * object will have a list of siblings (read as a list of objects).
222 * In our specific case, we do not care about the intermediate ones since
223 * they are all the same read from storage, and we do apply a last sibling
224 * (last write) wins logic.
225 * If by any means our resolution generates another conflict, it'll up to
226 * next read to properly solve it.}
229 * @param string $vClock
230 * @param array $objectList
232 * @return \Riak\Object
234 protected function resolveConflict($id, $vClock, array $objectList)
236 // Our approach here is last-write wins
237 $winner = $objectList[count($objectList)];
239 $putInput = new Input\PutInput();
240 $putInput->setVClock($vClock);
242 $mergedObject = new Object($id);
243 $mergedObject->setContent($winner->getContent());
245 $this->bucket->put($mergedObject, $putInput);
247 return $mergedObject;