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

namespace Doctrine\DBAL\Cache;

5
use Doctrine\Common\Cache\Cache;
6 7 8
use function hash;
use function serialize;
use function sha1;
9

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

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

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

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

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

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

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

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

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

84 85 86 87 88 89
        // 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
90

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
95
     * @return \Doctrine\DBAL\Cache\QueryCacheProfile
96 97 98 99 100 101 102 103
     */
    public function setResultCacheDriver(Cache $cache)
    {
        return new QueryCacheProfile($this->lifetime, $this->cacheKey, $cache);
    }

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

    /**
113
     * @param int $lifetime
Benjamin Morel's avatar
Benjamin Morel committed
114 115
     *
     * @return \Doctrine\DBAL\Cache\QueryCacheProfile
116 117 118 119 120
     */
    public function setLifetime($lifetime)
    {
        return new QueryCacheProfile($lifetime, $this->cacheKey, $this->resultCacheDriver);
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
121
}