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

namespace Doctrine\DBAL\Cache;

5
use Doctrine\Common\Cache\Cache;
6 7
use Doctrine\DBAL\Driver\DriverException;
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
        $this->store(
96
            $this->result->fetchAllAssociative()
97
        );
98

99
        return array_map('array_values', $this->data);
100 101 102 103 104
    }

    /**
     * {@inheritdoc}
     */
105
    public function fetchAllAssociative(): array
106
    {
107
        $this->store(
108
            $this->result->fetchAllAssociative()
109
        );
110

111
        return $this->data;
112 113
    }

114
    /**
115
     * {@inheritdoc}
116
     */
117 118 119 120 121
    public function fetchFirstColumn(): array
    {
        return FetchUtils::fetchFirstColumn($this);
    }

122 123 124 125 126 127 128 129 130 131
    public function rowCount(): int
    {
        return $this->result->rowCount();
    }

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

132
    public function free(): void
133
    {
134
        $this->data = null;
135
    }
136 137 138 139 140 141

    /**
     * @return array<string,mixed>|false
     *
     * @throws DriverException
     */
142
    private function fetch()
143 144 145 146 147
    {
        if ($this->data === null) {
            $this->data = [];
        }

148
        $row = $this->result->fetchAssociative();
149 150 151 152 153 154 155

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

            return $row;
        }

156
        $this->saveToCache();
157 158 159 160 161

        return false;
    }

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

        $this->saveToCache();
    }

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

177
        $data = $this->cache->fetch($this->cacheKey);
178 179 180 181 182 183 184

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

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

185
        $this->cache->save($this->cacheKey, $data, $this->lifetime);
186
    }
187
}