QueryCacheProfile.php 2.53 KB
Newer Older
1 2
<?php

Michael Moravec's avatar
Michael Moravec committed
3 4
declare(strict_types=1);

5 6
namespace Doctrine\DBAL\Cache;

7
use Doctrine\Common\Cache\Cache;
8
use Doctrine\DBAL\Cache\Exception\NoCacheKey;
9 10 11
use function hash;
use function serialize;
use function sha1;
12

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

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

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

29
    public function __construct(int $lifetime = 0, ?string $cacheKey = null, ?Cache $resultCache = null)
30
    {
31 32
        $this->lifetime          = $lifetime;
        $this->cacheKey          = $cacheKey;
33 34 35
        $this->resultCacheDriver = $resultCache;
    }

36
    public function getResultCacheDriver() : ?Cache
37 38 39 40
    {
        return $this->resultCacheDriver;
    }

41
    public function getLifetime() : int
42 43 44 45 46
    {
        return $this->lifetime;
    }

    /**
47
     * @throws CacheException
48
     */
49
    public function getCacheKey() : string
50 51
    {
        if ($this->cacheKey === null) {
52
            throw NoCacheKey::new();
53
        }
Benjamin Morel's avatar
Benjamin Morel committed
54

55 56 57 58
        return $this->cacheKey;
    }

    /**
59
     * Generates the real cache key from query, params, types and connection parameters.
60
     *
61 62 63
     * @param mixed[]        $params
     * @param int[]|string[] $types
     * @param mixed[]        $connectionParams
Benjamin Morel's avatar
Benjamin Morel committed
64
     *
65
     * @return string[]
66
     */
67
    public function generateCacheKeys(string $query, array $params, array $types, array $connectionParams = []) : array
68
    {
69 70 71
        $realCacheKey = 'query=' . $query .
            '&params=' . serialize($params) .
            '&types=' . serialize($types) .
72
            '&connectionParams=' . hash('sha256', serialize($connectionParams));
73

74 75 76 77 78 79
        // 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
80

81
        return [$cacheKey, $realCacheKey];
82
    }
83

84
    public function setResultCacheDriver(Cache $cache) : self
85 86 87 88
    {
        return new QueryCacheProfile($this->lifetime, $this->cacheKey, $cache);
    }

89
    public function setCacheKey(?string $cacheKey) : self
90 91 92 93
    {
        return new QueryCacheProfile($this->lifetime, $cacheKey, $this->resultCacheDriver);
    }

94
    public function setLifetime(int $lifetime) : self
95 96 97
    {
        return new QueryCacheProfile($lifetime, $this->cacheKey, $this->resultCacheDriver);
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
98
}