|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Pimf |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Pimf\Cache\Storages; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @package Cache_Storages |
|
13
|
|
|
* @author Gjero Krsteski <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class Pdo extends Storage |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The cache key from the cache configuration file. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $key; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var \Pimf\Database |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $pdo; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Create a new database cache storage instance. |
|
31
|
|
|
* |
|
32
|
|
|
* @param \Pimf\Database $pdo |
|
33
|
|
|
* @param string $key |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(\Pimf\Database $pdo, $key) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->pdo = $pdo; |
|
38
|
|
|
$this->key = (string)$key; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Retrieve an item from the cache storage. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $key |
|
45
|
|
|
* |
|
46
|
|
|
* @return mixed|void |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function retrieve($key) |
|
49
|
|
|
{ |
|
50
|
|
|
$sth = $this->pdo->prepare( |
|
51
|
|
|
'SELECT * FROM pimf_cache WHERE key = :key' |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
$sth->bindValue(':key', $this->key . $key); |
|
55
|
|
|
$sth->execute(); |
|
56
|
|
|
|
|
57
|
|
|
$cache = $sth->fetchObject(); |
|
58
|
|
|
|
|
59
|
|
|
if ($cache instanceof \stdClass) { |
|
60
|
|
|
|
|
61
|
|
|
if (time() >= $cache->expiration) { |
|
62
|
|
|
return $this->forget($key); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return unserialize($cache->value); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return null; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Write an item to the cache for a given number of minutes. |
|
73
|
|
|
* |
|
74
|
|
|
* <code> |
|
75
|
|
|
* // Put an item in the cache for 15 minutes |
|
76
|
|
|
* Cache::put('name', 'Robin', 15); |
|
77
|
|
|
* </code> |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $key |
|
80
|
|
|
* @param mixed $value |
|
81
|
|
|
* @param int $minutes |
|
82
|
|
|
* |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
|
|
public function put($key, $value, $minutes) |
|
86
|
|
|
{ |
|
87
|
|
|
$key = $this->key . $key; |
|
88
|
|
|
$value = serialize($value); |
|
89
|
|
|
$expiration = $this->expiration($minutes); |
|
90
|
|
|
|
|
91
|
|
|
try { |
|
92
|
|
|
$sth = $this->pdo->prepare( |
|
93
|
|
|
"INSERT INTO pimf_cache (key, value, expiration) VALUES (:key, :value, :expiration)" |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
} catch (\Exception $exception) { |
|
97
|
|
|
$sth = $this->pdo->prepare( |
|
98
|
|
|
"UPDATE pimf_cache SET value = :value, expiration = :expiration WHERE key = :key" |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$sth->bindValue(':key', $key); |
|
103
|
|
|
$sth->bindValue(':value', $value); |
|
104
|
|
|
$sth->bindValue(':expiration', $expiration); |
|
105
|
|
|
|
|
106
|
|
|
return $sth->execute(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Write an item to the cache for five years. |
|
111
|
|
|
* |
|
112
|
|
|
* @param $key |
|
113
|
|
|
* @param $value |
|
114
|
|
|
* |
|
115
|
|
|
* @return bool |
|
116
|
|
|
*/ |
|
117
|
|
|
public function forever($key, $value) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->put($key, $value, 2628000); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Delete an item from the cache. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $key |
|
126
|
|
|
* |
|
127
|
|
|
* @return boolean |
|
128
|
|
|
*/ |
|
129
|
|
|
public function forget($key) |
|
130
|
|
|
{ |
|
131
|
|
|
$sth = $this->pdo->prepare( |
|
132
|
|
|
"DELETE FROM pimf_cache WHERE key = :key" |
|
133
|
|
|
); |
|
134
|
|
|
|
|
135
|
|
|
$sth->bindValue(':key', $this->key . $key); |
|
136
|
|
|
|
|
137
|
|
|
return $sth->execute(); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|