Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Cache / QueryCacheProfile.php
1 <?php
2 /*
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.
14  *
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>.
18  */
19
20 namespace Doctrine\DBAL\Cache;
21
22 use Doctrine\Common\Cache\Cache;
23
24 /**
25  * Query Cache Profile handles the data relevant for query caching.
26  *
27  * It is a value object, setter methods return NEW instances.
28  *
29  * @author Benjamin Eberlei <kontakt@beberlei.de>
30  */
31 class QueryCacheProfile
32 {
33     /**
34      * @var Cache
35      */
36     private $resultCacheDriver;
37     /**
38      * @var int
39      */
40     private $lifetime = 0;
41     /**
42      * @var string
43      */
44     private $cacheKey;
45
46     /**
47      * @param int $lifetime
48      * @param string $cacheKey
49      * @param Cache $resultCache
50      */
51     public function __construct($lifetime = 0, $cacheKey = null, Cache $resultCache = null)
52     {
53         $this->lifetime = $lifetime;
54         $this->cacheKey = $cacheKey;
55         $this->resultCacheDriver = $resultCache;
56     }
57
58     /**
59      * @return Cache
60      */
61     public function getResultCacheDriver()
62     {
63         return $this->resultCacheDriver;
64     }
65
66     /**
67      * @return int
68      */
69     public function getLifetime()
70     {
71         return $this->lifetime;
72     }
73
74     /**
75      * @return string
76      */
77     public function getCacheKey()
78     {
79         if ($this->cacheKey === null) {
80             throw CacheException::noCacheKey();
81         }
82         return $this->cacheKey;
83     }
84
85     /**
86      * Generate the real cache key from query, params and types.
87      *
88      * @param string $query
89      * @param array $params
90      * @param array $types
91      * @return array
92      */
93     public function generateCacheKeys($query, $params, $types)
94     {
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);
99         } else {
100             $cacheKey = $this->cacheKey;
101         }
102         return array($cacheKey, $realCacheKey);
103     }
104
105     /**
106      * @param Cache $cache
107      * @return QueryCacheProfile
108      */
109     public function setResultCacheDriver(Cache $cache)
110     {
111         return new QueryCacheProfile($this->lifetime, $this->cacheKey, $cache);
112     }
113
114     /**
115      * @param string|null $cacheKey
116      * @return QueryCacheProfile
117      */
118     public function setCacheKey($cacheKey)
119     {
120         return new QueryCacheProfile($this->lifetime, $cacheKey, $this->resultCacheDriver);
121     }
122
123     /**
124      * @param  int $lifetime
125      * @return QueryCacheProfile
126      */
127     public function setLifetime($lifetime)
128     {
129         return new QueryCacheProfile($lifetime, $this->cacheKey, $this->resultCacheDriver);
130     }
131 }