CachingResult.php 3.71 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Cache;

5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\DBAL\Driver\Exception;
7
use Doctrine\DBAL\Driver\FetchUtils;
8 9
use Doctrine\DBAL\Driver\Result;

10
use function array_map;
11
use function array_values;
12 13 14 15 16 17 18 19 20 21 22

/**
 * A result is saved in multiple cache keys, there is the originally specified
 * cache key which is just pointing to result rows by key. The following things
 * have to be ensured:
 *
 * 1. lifetime of the original key has to be longer than that of all the individual rows keys
 * 2. if any one row key is missing the query has to be re-executed.
 *
 * Also you have to realize that the cache will load the whole result into memory at once to ensure 2.
 * This means that the memory usage for cached results might increase by using this feature.
23
 *
24
 * @internal The class is internal to the caching layer implementation.
25
 */
26
class CachingResult implements Result
27
{
28
    /** @var Cache */
29
    private $cache;
30

31
    /** @var string */
32 33
    private $cacheKey;

34
    /** @var string */
35 36
    private $realKey;

37
    /** @var int */
38 39
    private $lifetime;

40 41
    /** @var Result */
    private $result;
42

43
    /** @var array<int,array<string,mixed>>|null */
44 45
    private $data;

46
    /**
47 48 49
     * @param string $cacheKey
     * @param string $realKey
     * @param int    $lifetime
50
     */
51
    public function __construct(Result $result, Cache $cache, $cacheKey, $realKey, $lifetime)
52
    {
53 54 55 56 57
        $this->result   = $result;
        $this->cache    = $cache;
        $this->cacheKey = $cacheKey;
        $this->realKey  = $realKey;
        $this->lifetime = $lifetime;
58 59
    }

60 61 62 63 64
    /**
     * {@inheritdoc}
     */
    public function fetchNumeric()
    {
65
        $row = $this->fetch();
66 67 68 69 70 71 72 73 74 75 76 77 78

        if ($row === false) {
            return false;
        }

        return array_values($row);
    }

    /**
     * {@inheritdoc}
     */
    public function fetchAssociative()
    {
79
        return $this->fetch();
80 81 82 83 84 85 86 87 88 89 90 91 92
    }

    /**
     * {@inheritdoc}
     */
    public function fetchOne()
    {
        return FetchUtils::fetchOne($this);
    }

    /**
     * {@inheritdoc}
     */
93
    public function fetchAllNumeric(): array
94
    {
95
        return array_map('array_values', $this->result->fetchAllAssociative());
96 97 98 99 100
    }

    /**
     * {@inheritdoc}
     */
101
    public function fetchAllAssociative(): array
102
    {
103 104 105
        $data = $this->result->fetchAllAssociative();

        $this->store($data);
106

107
        return $data;
108 109
    }

110
    /**
111
     * {@inheritdoc}
112
     */
113 114 115 116 117
    public function fetchFirstColumn(): array
    {
        return FetchUtils::fetchFirstColumn($this);
    }

118 119 120 121 122 123 124 125 126 127
    public function rowCount(): int
    {
        return $this->result->rowCount();
    }

    public function columnCount(): int
    {
        return $this->result->columnCount();
    }

128
    public function free(): void
129
    {
130
        $this->data = null;
131
    }
132 133 134 135

    /**
     * @return array<string,mixed>|false
     *
136
     * @throws Exception
137
     */
138
    private function fetch()
139 140 141 142 143
    {
        if ($this->data === null) {
            $this->data = [];
        }

144
        $row = $this->result->fetchAssociative();
145 146 147 148 149 150 151

        if ($row !== false) {
            $this->data[] = $row;

            return $row;
        }

152
        $this->saveToCache();
153 154 155 156 157

        return false;
    }

    /**
158
     * @param array<int,array<string,mixed>> $data
159
     */
160
    private function store(array $data): void
161
    {
162 163 164 165 166 167 168 169 170 171 172
        $this->data = $data;

        $this->saveToCache();
    }

    private function saveToCache(): void
    {
        if ($this->data === null) {
            return;
        }

173
        $data = $this->cache->fetch($this->cacheKey);
174 175 176 177 178 179 180

        if ($data === false) {
            $data = [];
        }

        $data[$this->realKey] = $this->data;

181
        $this->cache->save($this->cacheKey, $data, $this->lifetime);
182
    }
183
}