Passed
Push — master ( 5b283a...d58ab5 )
by Richard
01:35
created

StmtCache::deleteQueryFromCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Maphper\DataSource;
3
class StmtCache {
4
    private $pdo;
5
    private $queryCache = [];
6
7
    public function __construct(\PDO $pdo) {
8
        $this->pdo = $pdo;
9
    }
10
11
    public function getCachedStmt($sql) {
12
		$queryId = $this->getQueryId($sql);
13
		if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId];
14
		else {
15
			$stmt = $this->pdo->prepare($sql, [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]);
16
			if ($stmt) $this->queryCache[$queryId] = $stmt;
17
		}
18
		return $stmt;
19
	}
20
21
    private function getQueryId($sql) {
22
        return md5($sql);
23
    }
24
25
    public function deleteQueryFromCache($sql) {
26
        unset($this->queryCache[$this->getQueryId($sql)]);
27
    }
28
}
29