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\DBAL\Cache;
22 use Doctrine\Common\Cache\Cache;
25 * Query Cache Profile handles the data relevant for query caching.
27 * It is a value object, setter methods return NEW instances.
29 * @author Benjamin Eberlei <kontakt@beberlei.de>
31 class QueryCacheProfile
36 private $resultCacheDriver;
40 private $lifetime = 0;
47 * @param int $lifetime
48 * @param string $cacheKey
49 * @param Cache $resultCache
51 public function __construct($lifetime = 0, $cacheKey = null, Cache $resultCache = null)
53 $this->lifetime = $lifetime;
54 $this->cacheKey = $cacheKey;
55 $this->resultCacheDriver = $resultCache;
61 public function getResultCacheDriver()
63 return $this->resultCacheDriver;
69 public function getLifetime()
71 return $this->lifetime;
77 public function getCacheKey()
79 if ($this->cacheKey === null) {
80 throw CacheException::noCacheKey();
82 return $this->cacheKey;
86 * Generate the real cache key from query, params and types.
88 * @param string $query
89 * @param array $params
93 public function generateCacheKeys($query, $params, $types)
95 $realCacheKey = $query . "-" . serialize($params) . "-" . serialize($types);
96 // should the key be automatically generated using the inputs or is the cache key set?
97 if ($this->cacheKey === null) {
98 $cacheKey = sha1($realCacheKey);
100 $cacheKey = $this->cacheKey;
102 return array($cacheKey, $realCacheKey);
106 * @param Cache $cache
107 * @return QueryCacheProfile
109 public function setResultCacheDriver(Cache $cache)
111 return new QueryCacheProfile($this->lifetime, $this->cacheKey, $cache);
115 * @param string|null $cacheKey
116 * @return QueryCacheProfile
118 public function setCacheKey($cacheKey)
120 return new QueryCacheProfile($this->lifetime, $cacheKey, $this->resultCacheDriver);
124 * @param int $lifetime
125 * @return QueryCacheProfile
127 public function setLifetime($lifetime)
129 return new QueryCacheProfile($lifetime, $this->cacheKey, $this->resultCacheDriver);