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 Apc extends Storage |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The cache key from the cache configuration file. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $key; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Is APCu is supported. |
26
|
|
|
* |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
protected $apcu = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new APC cache storage instance. |
33
|
|
|
* |
34
|
|
|
* @param string $key |
35
|
|
|
*/ |
36
|
|
|
public function __construct($key) |
37
|
|
|
{ |
38
|
|
|
$this->key = (string)$key; |
39
|
|
|
$this->apcu = function_exists('apcu_fetch'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Retrieve an item from the cache storage. |
44
|
|
|
* |
45
|
|
|
* @param string $key |
46
|
|
|
* |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
protected function retrieve($key) |
50
|
|
|
{ |
51
|
|
|
$result = $this->apcu |
52
|
|
|
? apcu_fetch($this->key . $key, $success) |
53
|
|
|
: apc_fetch($this->key . $key, $success) |
54
|
|
|
; |
55
|
|
|
return ($success === true) ? $result : null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Write an item to the cache for a given number of minutes. |
60
|
|
|
* |
61
|
|
|
* <code> |
62
|
|
|
* // Put an item in the cache for 15 minutes |
63
|
|
|
* Cache::put('name', 'Robin', 15); |
64
|
|
|
* </code> |
65
|
|
|
* |
66
|
|
|
* @param string $key |
67
|
|
|
* @param mixed $value |
68
|
|
|
* @param int $minutes |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function put($key, $value, $minutes) |
73
|
|
|
{ |
74
|
|
|
return $this->apcu |
75
|
|
|
? apcu_store('' . $this->key . $key, $value, (int)$minutes * 60) |
76
|
|
|
: apc_store('' . $this->key . $key, $value, (int)$minutes * 60 |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Write an item to the cache that lasts forever. |
82
|
|
|
* |
83
|
|
|
* @param string $key |
84
|
|
|
* @param mixed $value |
85
|
|
|
* |
86
|
|
|
* @return boolean |
87
|
|
|
*/ |
88
|
|
|
public function forever($key, $value) |
89
|
|
|
{ |
90
|
|
|
return $this->put($key, $value, 0); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Delete an item from the cache. |
95
|
|
|
* |
96
|
|
|
* @param string $key |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function forget($key) |
101
|
|
|
{ |
102
|
|
|
return $this->apcu ? apcu_delete($key) : apc_delete($key); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Remove all items from the cache. |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
|
public function flush() |
111
|
|
|
{ |
112
|
|
|
$this->apcu ? apcu_clear_cache() : apc_clear_cache('user'); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|