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