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;
29 * Riak cache provider.
31 * @link www.doctrine-project.org
33 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
35 class RiakCache extends CacheProvider
37 const EXPIRES_HEADER = 'X-Riak-Meta-Expires';
45 * Sets the riak bucket instance to use.
47 * @param \Riak\Bucket $bucket
49 public function __construct(Bucket $bucket)
51 $this->bucket = $bucket;
57 protected function doFetch($id)
60 $response = $this->bucket->get($id);
63 if ( ! $response->hasObject()) {
67 // Check for attempted siblings
68 $object = ($response->hasSiblings())
69 ? $this->resolveConflict($id, $response->getVClock(), $response->getObjectList())
70 : $response->getFirstObject();
72 // Check for expired object
73 if ($this->isExpired($object)) {
74 $this->bucket->delete($object);
79 return unserialize($object->getContent());
80 } catch (Exception\RiakException $e) {
82 // - Riak\ConnectionException
83 // - Riak\CommunicationException
84 // - Riak\UnexpectedResponseException
85 // - Riak\NotFoundException
94 protected function doContains($id)
97 // We only need the HEAD, not the entire object
98 $input = new Input\GetInput();
100 $input->setReturnHead(true);
102 $response = $this->bucket->get($id, $input);
105 if ( ! $response->hasObject()) {
109 $object = $response->getFirstObject();
111 // Check for expired object
112 if ($this->isExpired($object)) {
113 $this->bucket->delete($object);
119 } catch (Exception\RiakException $e) {
129 protected function doSave($id, $data, $lifeTime = 0)
132 $object = new Object($id);
134 $object->setContent(serialize($data));
137 $object->addMetadata(self::EXPIRES_HEADER, (string) (time() + $lifeTime));
140 $this->bucket->put($object);
143 } catch (Exception\RiakException $e) {
153 protected function doDelete($id)
156 $this->bucket->delete($id);
159 } catch (Exception\BadArgumentsException $e) {
160 // Key did not exist on cluster already
161 } catch (Exception\RiakException $e) {
163 // - Riak\Exception\ConnectionException
164 // - Riak\Exception\CommunicationException
165 // - Riak\Exception\UnexpectedResponseException
174 protected function doFlush()
177 $keyList = $this->bucket->getKeyList();
179 foreach ($keyList as $key) {
180 $this->bucket->delete($key);
184 } catch (Exception\RiakException $e) {
194 protected function doGetStats()
196 // Only exposed through HTTP stats API, not Protocol Buffers API
201 * Check if a given Riak Object have expired.
203 * @param \Riak\Object $object
207 private function isExpired(Object $object)
209 $metadataMap = $object->getMetadataMap();
211 return isset($metadataMap[self::EXPIRES_HEADER])
212 && $metadataMap[self::EXPIRES_HEADER] < time();
216 * On-read conflict resolution. Applied approach here is last write wins.
217 * Specific needs may override this method to apply alternate conflict resolutions.
219 * {@internal Riak does not attempt to resolve a write conflict, and store
220 * it as sibling of conflicted one. By following this approach, it is up to
221 * the next read to resolve the conflict. When this happens, your fetched
222 * object will have a list of siblings (read as a list of objects).
223 * In our specific case, we do not care about the intermediate ones since
224 * they are all the same read from storage, and we do apply a last sibling
225 * (last write) wins logic.
226 * If by any means our resolution generates another conflict, it'll up to
227 * next read to properly solve it.}
230 * @param string $vClock
231 * @param array $objectList
233 * @return \Riak\Object
235 protected function resolveConflict($id, $vClock, array $objectList)
237 // Our approach here is last-write wins
238 $winner = $objectList[count($objectList)];
240 $putInput = new Input\PutInput();
241 $putInput->setVClock($vClock);
243 $mergedObject = new Object($id);
244 $mergedObject->setContent($winner->getContent());
246 $this->bucket->put($mergedObject, $putInput);
248 return $mergedObject;