ResultCacheStatement.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. namespace Doctrine\DBAL\Cache;
  3. use ArrayIterator;
  4. use Doctrine\Common\Cache\Cache;
  5. use Doctrine\DBAL\Driver\Exception;
  6. use Doctrine\DBAL\Driver\FetchUtils;
  7. use Doctrine\DBAL\Driver\Result;
  8. use Doctrine\DBAL\Driver\ResultStatement;
  9. use Doctrine\DBAL\Driver\Statement;
  10. use Doctrine\DBAL\FetchMode;
  11. use InvalidArgumentException;
  12. use IteratorAggregate;
  13. use PDO;
  14. use function array_map;
  15. use function array_merge;
  16. use function array_values;
  17. use function assert;
  18. use function reset;
  19. /**
  20. * Cache statement for SQL results.
  21. *
  22. * A result is saved in multiple cache keys, there is the originally specified
  23. * cache key which is just pointing to result rows by key. The following things
  24. * have to be ensured:
  25. *
  26. * 1. lifetime of the original key has to be longer than that of all the individual rows keys
  27. * 2. if any one row key is missing the query has to be re-executed.
  28. *
  29. * Also you have to realize that the cache will load the whole result into memory at once to ensure 2.
  30. * This means that the memory usage for cached results might increase by using this feature.
  31. *
  32. * @deprecated
  33. */
  34. class ResultCacheStatement implements IteratorAggregate, ResultStatement, Result
  35. {
  36. /** @var Cache */
  37. private $resultCache;
  38. /** @var string */
  39. private $cacheKey;
  40. /** @var string */
  41. private $realKey;
  42. /** @var int */
  43. private $lifetime;
  44. /** @var ResultStatement */
  45. private $statement;
  46. /** @var array<int,array<string,mixed>>|null */
  47. private $data;
  48. /** @var int */
  49. private $defaultFetchMode = FetchMode::MIXED;
  50. /**
  51. * @param string $cacheKey
  52. * @param string $realKey
  53. * @param int $lifetime
  54. */
  55. public function __construct(ResultStatement $stmt, Cache $resultCache, $cacheKey, $realKey, $lifetime)
  56. {
  57. $this->statement = $stmt;
  58. $this->resultCache = $resultCache;
  59. $this->cacheKey = $cacheKey;
  60. $this->realKey = $realKey;
  61. $this->lifetime = $lifetime;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. *
  66. * @deprecated Use free() instead.
  67. */
  68. public function closeCursor()
  69. {
  70. $this->free();
  71. return true;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function columnCount()
  77. {
  78. return $this->statement->columnCount();
  79. }
  80. /**
  81. * {@inheritdoc}
  82. *
  83. * @deprecated Use one of the fetch- or iterate-related methods.
  84. */
  85. public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
  86. {
  87. $this->defaultFetchMode = $fetchMode;
  88. return true;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. *
  93. * @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
  94. */
  95. public function getIterator()
  96. {
  97. $data = $this->fetchAll();
  98. return new ArrayIterator($data);
  99. }
  100. /**
  101. * {@inheritdoc}
  102. *
  103. * @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
  104. */
  105. public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
  106. {
  107. if ($this->data === null) {
  108. $this->data = [];
  109. }
  110. $row = $this->statement->fetch(FetchMode::ASSOCIATIVE);
  111. if ($row) {
  112. $this->data[] = $row;
  113. $fetchMode = $fetchMode ?: $this->defaultFetchMode;
  114. if ($fetchMode === FetchMode::ASSOCIATIVE) {
  115. return $row;
  116. }
  117. if ($fetchMode === FetchMode::NUMERIC) {
  118. return array_values($row);
  119. }
  120. if ($fetchMode === FetchMode::MIXED) {
  121. return array_merge($row, array_values($row));
  122. }
  123. if ($fetchMode === FetchMode::COLUMN) {
  124. return reset($row);
  125. }
  126. throw new InvalidArgumentException('Invalid fetch-style given for caching result.');
  127. }
  128. $this->saveToCache();
  129. return false;
  130. }
  131. /**
  132. * {@inheritdoc}
  133. *
  134. * @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.
  135. */
  136. public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
  137. {
  138. $data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE, $fetchArgument, $ctorArgs);
  139. $this->data = $data;
  140. $this->saveToCache();
  141. if ($fetchMode === FetchMode::NUMERIC) {
  142. foreach ($data as $i => $row) {
  143. $data[$i] = array_values($row);
  144. }
  145. } elseif ($fetchMode === FetchMode::MIXED) {
  146. foreach ($data as $i => $row) {
  147. $data[$i] = array_merge($row, array_values($row));
  148. }
  149. } elseif ($fetchMode === FetchMode::COLUMN) {
  150. foreach ($data as $i => $row) {
  151. $data[$i] = reset($row);
  152. }
  153. }
  154. return $data;
  155. }
  156. /**
  157. * {@inheritdoc}
  158. *
  159. * @deprecated Use fetchOne() instead.
  160. */
  161. public function fetchColumn($columnIndex = 0)
  162. {
  163. $row = $this->fetch(FetchMode::NUMERIC);
  164. // TODO: verify that return false is the correct behavior
  165. return $row[$columnIndex] ?? false;
  166. }
  167. /**
  168. * {@inheritdoc}
  169. */
  170. public function fetchNumeric()
  171. {
  172. $row = $this->doFetch();
  173. if ($row === false) {
  174. return false;
  175. }
  176. return array_values($row);
  177. }
  178. /**
  179. * {@inheritdoc}
  180. */
  181. public function fetchAssociative()
  182. {
  183. return $this->doFetch();
  184. }
  185. /**
  186. * {@inheritdoc}
  187. */
  188. public function fetchOne()
  189. {
  190. return FetchUtils::fetchOne($this);
  191. }
  192. /**
  193. * {@inheritdoc}
  194. */
  195. public function fetchAllNumeric(): array
  196. {
  197. if ($this->statement instanceof Result) {
  198. $data = $this->statement->fetchAllAssociative();
  199. } else {
  200. $data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE);
  201. }
  202. $this->data = $data;
  203. $this->saveToCache();
  204. return array_map('array_values', $data);
  205. }
  206. /**
  207. * {@inheritdoc}
  208. */
  209. public function fetchAllAssociative(): array
  210. {
  211. if ($this->statement instanceof Result) {
  212. $data = $this->statement->fetchAllAssociative();
  213. } else {
  214. $data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE);
  215. }
  216. $this->data = $data;
  217. $this->saveToCache();
  218. return $data;
  219. }
  220. /**
  221. * {@inheritdoc}
  222. */
  223. public function fetchFirstColumn(): array
  224. {
  225. return FetchUtils::fetchFirstColumn($this);
  226. }
  227. /**
  228. * Returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
  229. * executed by the corresponding object.
  230. *
  231. * If the last SQL statement executed by the associated Statement object was a SELECT statement,
  232. * some databases may return the number of rows returned by that statement. However,
  233. * this behaviour is not guaranteed for all databases and should not be
  234. * relied on for portable applications.
  235. *
  236. * @return int The number of rows.
  237. */
  238. public function rowCount()
  239. {
  240. assert($this->statement instanceof Statement);
  241. return $this->statement->rowCount();
  242. }
  243. public function free(): void
  244. {
  245. $this->data = null;
  246. }
  247. /**
  248. * @return array<string,mixed>|false
  249. *
  250. * @throws Exception
  251. */
  252. private function doFetch()
  253. {
  254. if ($this->data === null) {
  255. $this->data = [];
  256. }
  257. if ($this->statement instanceof Result) {
  258. $row = $this->statement->fetchAssociative();
  259. } else {
  260. $row = $this->statement->fetch(FetchMode::ASSOCIATIVE);
  261. }
  262. if ($row !== false) {
  263. $this->data[] = $row;
  264. return $row;
  265. }
  266. $this->saveToCache();
  267. return false;
  268. }
  269. private function saveToCache(): void
  270. {
  271. if ($this->data === null) {
  272. return;
  273. }
  274. $data = $this->resultCache->fetch($this->cacheKey);
  275. if (! $data) {
  276. $data = [];
  277. }
  278. $data[$this->realKey] = $this->data;
  279. $this->resultCache->save($this->cacheKey, $data, $this->lifetime);
  280. }
  281. }