Overview

Namespaces

  • Webmozart
    • KeyValueStore
      • Api
      • Decorator
      • Util

Classes

  • AbstractDecorator
  • CachingDecorator
  • CountableDecorator
  • SortableDecorator
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 
<?php

/*
 * This file is part of the webmozart/key-value-store package.
 *
 * (c) Bernhard Schussek <bschussek@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Webmozart\KeyValueStore\Decorator;

use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\ClearableCache;
use Doctrine\Common\Cache\FlushableCache;
use InvalidArgumentException;
use Webmozart\KeyValueStore\Api\KeyValueStore;
use Webmozart\KeyValueStore\Api\NoSuchKeyException;

/**
 * A caching decorator implementing a cache layer for any store.
 *
 * @since  1.0
 *
 * @author Bernhard Schussek <bschussek@gmail.com>
 */
class CachingDecorator extends AbstractDecorator
{
    /**
     * @var Cache
     */
    private $cache;

    private $ttl;

    /**
     * Creates the store.
     *
     * @param KeyValueStore $store The cached store.
     * @param Cache         $cache The cache.
     * @param int           $ttl   The time-to-live for cache entries. If set to
     *                             0, cache entries never expire.
     *
     * @throws InvalidArgumentException If the provided cache is not supported
     */
    public function __construct(KeyValueStore $store, Cache $cache, $ttl = 0)
    {
        if (!$cache instanceof ClearableCache && !$cache instanceof FlushableCache) {
            throw new InvalidArgumentException(sprintf(
                'The cache must either implement ClearableCache or '.
                'FlushableCache. Got: %s',
                get_class($cache)
            ));
        }

        parent::__construct($store);

        $this->cache = $cache;
        $this->ttl = $ttl;
    }

    /**
     * {@inheritdoc}
     */
    public function set($key, $value)
    {
        $this->store->set($key, $value);
        $this->cache->save($key, $value, $this->ttl);
    }

    /**
     * {@inheritdoc}
     */
    public function get($key, $default = null)
    {
        if ($this->cache->contains($key)) {
            return $this->cache->fetch($key);
        }

        try {
            $value = $this->store->getOrFail($key);
        } catch (NoSuchKeyException $e) {
            return $default;
        }

        $this->cache->save($key, $value, $this->ttl);

        return $value;
    }

    /**
     * {@inheritdoc}
     */
    public function getOrFail($key)
    {
        if ($this->cache->contains($key)) {
            return $this->cache->fetch($key);
        }

        $value = $this->store->getOrFail($key);

        $this->cache->save($key, $value, $this->ttl);

        return $value;
    }

    /**
     * {@inheritdoc}
     */
    public function getMultiple(array $keys, $default = null)
    {
        $values = array();

        // Read cached values from the cache
        foreach ($keys as $i => $key) {
            if ($this->cache->contains($key)) {
                $values[$key] = $this->cache->fetch($key);
                unset($keys[$i]);
            }
        }

        // Don't write cache, as we can't differentiate between existing and
        // non-existing keys
        return array_replace($values, $this->store->getMultiple($keys, $default));
    }

    /**
     * {@inheritdoc}
     */
    public function getMultipleOrFail(array $keys)
    {
        $values = array();

        // Read cached values from the cache
        foreach ($keys as $i => $key) {
            if ($this->cache->contains($key)) {
                $values[$key] = $this->cache->fetch($key);
                unset($keys[$i]);
            }
        }

        $values = array_replace($values, $this->store->getMultipleOrFail($keys));

        // Write newly fetched values to the cache
        foreach ($keys as $key) {
            $this->cache->save($key, $values[$key], $this->ttl);
        }

        return $values;
    }

    /**
     * {@inheritdoc}
     */
    public function remove($key)
    {
        $this->store->remove($key);
        $this->cache->delete($key);
    }

    /**
     * {@inheritdoc}
     */
    public function exists($key)
    {
        if ($this->cache->contains($key)) {
            return true;
        }

        return $this->store->exists($key);
    }

    /**
     * {@inheritdoc}
     */
    public function clear()
    {
        $this->store->clear();

        if ($this->cache instanceof ClearableCache) {
            $this->cache->deleteAll();
        } else {
            $this->cache->flushAll();
        }
    }
}
Webmozart Key-Value Store API API documentation generated by ApiGen