QueryCacheProfile.php 2.81 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Cache;

5
use Doctrine\Common\Cache\Cache;
6

7 8 9
use function hash;
use function serialize;
use function sha1;
10

11 12 13
/**
 * Query Cache Profile handles the data relevant for query caching.
 *
14
 * It is a value object, setter methods return NEW instances.
15 16 17
 */
class QueryCacheProfile
{
18
    /** @var Cache|null */
19
    private $resultCacheDriver;
Benjamin Morel's avatar
Benjamin Morel committed
20

21
    /** @var int */
22
    private $lifetime = 0;
Benjamin Morel's avatar
Benjamin Morel committed
23

24
    /** @var string|null */
25 26 27
    private $cacheKey;

    /**
28 29
     * @param int         $lifetime
     * @param string|null $cacheKey
30
     */
31
    public function __construct($lifetime = 0, $cacheKey = null, ?Cache $resultCache = null)
32
    {
33 34
        $this->lifetime          = $lifetime;
        $this->cacheKey          = $cacheKey;
35 36 37 38
        $this->resultCacheDriver = $resultCache;
    }

    /**
39
     * @return Cache|null
40 41 42 43 44 45 46
     */
    public function getResultCacheDriver()
    {
        return $this->resultCacheDriver;
    }

    /**
47
     * @return int
48 49 50 51 52 53 54 55
     */
    public function getLifetime()
    {
        return $this->lifetime;
    }

    /**
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
56
     *
57
     * @throws CacheException
58 59 60 61 62 63
     */
    public function getCacheKey()
    {
        if ($this->cacheKey === null) {
            throw CacheException::noCacheKey();
        }
Benjamin Morel's avatar
Benjamin Morel committed
64

65 66 67 68
        return $this->cacheKey;
    }

    /**
69
     * Generates the real cache key from query, params, types and connection parameters.
70
     *
71
     * @param string         $sql
72 73 74
     * @param mixed[]        $params
     * @param int[]|string[] $types
     * @param mixed[]        $connectionParams
Benjamin Morel's avatar
Benjamin Morel committed
75
     *
76
     * @return string[]
77
     */
78
    public function generateCacheKeys($sql, $params, $types, array $connectionParams = [])
79
    {
80
        $realCacheKey = 'query=' . $sql .
81 82
            '&params=' . serialize($params) .
            '&types=' . serialize($types) .
83
            '&connectionParams=' . hash('sha256', serialize($connectionParams));
84

85 86 87 88 89 90
        // should the key be automatically generated using the inputs or is the cache key set?
        if ($this->cacheKey === null) {
            $cacheKey = sha1($realCacheKey);
        } else {
            $cacheKey = $this->cacheKey;
        }
Benjamin Morel's avatar
Benjamin Morel committed
91

92
        return [$cacheKey, $realCacheKey];
93
    }
94 95

    /**
Grégoire Paris's avatar
Grégoire Paris committed
96
     * @return QueryCacheProfile
97 98 99 100 101 102 103 104
     */
    public function setResultCacheDriver(Cache $cache)
    {
        return new QueryCacheProfile($this->lifetime, $this->cacheKey, $cache);
    }

    /**
     * @param string|null $cacheKey
Benjamin Morel's avatar
Benjamin Morel committed
105
     *
Grégoire Paris's avatar
Grégoire Paris committed
106
     * @return QueryCacheProfile
107 108 109 110 111 112 113
     */
    public function setCacheKey($cacheKey)
    {
        return new QueryCacheProfile($this->lifetime, $cacheKey, $this->resultCacheDriver);
    }

    /**
114
     * @param int $lifetime
Benjamin Morel's avatar
Benjamin Morel committed
115
     *
Grégoire Paris's avatar
Grégoire Paris committed
116
     * @return QueryCacheProfile
117 118 119 120 121
     */
    public function setLifetime($lifetime)
    {
        return new QueryCacheProfile($lifetime, $this->cacheKey, $this->resultCacheDriver);
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
122
}