1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by jensk on 25-10-2017. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace CloudControl\Cms\storage; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class Cache |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Cache |
13
|
|
|
*/ |
14
|
|
|
private static $instance; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $storagePath; |
20
|
|
|
/** |
21
|
|
|
* @var \PDO |
22
|
|
|
*/ |
23
|
|
|
protected $dbHandle; |
24
|
|
|
|
25
|
|
|
private function __construct() |
26
|
|
|
{ |
27
|
|
|
// Singleton, so private constructor |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return Cache |
32
|
|
|
*/ |
33
|
|
|
public static function getInstance() |
34
|
|
|
{ |
35
|
|
|
if (!self::$instance instanceof Cache) { |
36
|
|
|
self::$instance = new Cache(); |
37
|
|
|
} |
38
|
|
|
return self::$instance; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $path |
43
|
|
|
* @return \stdClass |
44
|
|
|
*/ |
45
|
|
|
public function getCacheForPath($path) |
46
|
|
|
{ |
47
|
|
|
$dbInstace = $this->getDbInstance(); |
48
|
|
|
$sql = ' |
49
|
|
|
SELECT * |
50
|
|
|
FROM `cache` |
51
|
|
|
WHERE `path` = :path |
52
|
|
|
LIMIT 1 |
53
|
|
|
'; |
54
|
|
|
$stmt = $dbInstace->prepare($sql); |
55
|
|
|
$stmt->bindParam(':path', $path); |
56
|
|
View Code Duplication |
if ($stmt->execute()) { |
|
|
|
|
57
|
|
|
return $stmt->fetch(\PDO::FETCH_OBJ); |
58
|
|
|
} else { |
59
|
|
|
$error = $stmt->errorInfo(); |
60
|
|
|
$errorMsg = $error[2]; |
61
|
|
|
throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Clears all cache |
67
|
|
|
*/ |
68
|
|
|
public function clearCache() |
69
|
|
|
{ |
70
|
|
|
$dbInstace = $this->getDbInstance(); |
71
|
|
|
$sql = ' |
72
|
|
|
DELETE FROM `cache`; |
73
|
|
|
VACUUM; |
74
|
|
|
'; |
75
|
|
|
$stmt = $dbInstace->prepare($sql); |
76
|
|
View Code Duplication |
if ($stmt->execute()) { |
|
|
|
|
77
|
|
|
return; |
78
|
|
|
} else { |
79
|
|
|
$error = $stmt->errorInfo(); |
80
|
|
|
$errorMsg = $error[2]; |
81
|
|
|
throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $storagePath |
87
|
|
|
*/ |
88
|
|
|
public function setStoragePath($storagePath) |
89
|
|
|
{ |
90
|
|
|
$this->storagePath = $storagePath; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return \PDO |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
private function getDbInstance() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
if ($this->dbHandle === null) { |
99
|
|
|
$this->dbHandle = new \PDO('sqlite:' . $this->storagePath . DIRECTORY_SEPARATOR . 'cache.db'); |
100
|
|
|
} |
101
|
|
|
return $this->dbHandle; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $baseCacheSqlPath |
106
|
|
|
*/ |
107
|
|
|
public function init($baseCacheSqlPath) |
108
|
|
|
{ |
109
|
|
|
$realBaseCacheSqlPath = realpath($baseCacheSqlPath); |
110
|
|
|
|
111
|
|
|
$db = $this->getDbInstance(); |
112
|
|
|
$sql = file_get_contents($realBaseCacheSqlPath); |
113
|
|
|
$db->exec($sql); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param $requestUri |
118
|
|
|
* @param $renderedContent |
119
|
|
|
*/ |
120
|
|
|
public function setCacheForPath($requestUri, $renderedContent) |
121
|
|
|
{ |
122
|
|
|
$dbInstace = $this->getDbInstance(); |
123
|
|
|
$sql = ' |
124
|
|
|
INSERT OR REPLACE INTO `cache` (path, creationStamp, contents) |
125
|
|
|
VALUES (:path, :creationStamp, :contents); |
126
|
|
|
'; |
127
|
|
|
$contents = \sanitize_output($renderedContent); |
128
|
|
|
$creationStamp = time(); |
129
|
|
|
$stmt = $dbInstace->prepare($sql); |
130
|
|
|
$stmt->bindParam(':path', $requestUri); |
131
|
|
|
$stmt->bindParam(':creationStamp', $creationStamp); |
132
|
|
|
$stmt->bindParam(':contents', $contents); |
133
|
|
View Code Duplication |
if ($stmt->execute()) { |
|
|
|
|
134
|
|
|
return; |
135
|
|
|
} else { |
136
|
|
|
$error = $stmt->errorInfo(); |
137
|
|
|
$errorMsg = $error[2]; |
138
|
|
|
throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |