|
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 Memory extends Storage |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The in-memory array of cached items. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
public $storage = array(); |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Retrieve an item from the cache storage. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $key |
|
28
|
|
|
* |
|
29
|
|
|
* @return mixed|null |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function retrieve($key) |
|
32
|
|
|
{ |
|
33
|
|
|
if (array_key_exists($key, $this->storage)) { |
|
34
|
|
|
return $this->storage[$key]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return null; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Write an item to the cache for a given number of minutes. |
|
42
|
|
|
* |
|
43
|
|
|
* <code> |
|
44
|
|
|
* // Put an item in the cache for 15 minutes |
|
45
|
|
|
* Cache::put('name', 'Robin', 15); |
|
46
|
|
|
* </code> |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $key |
|
49
|
|
|
* @param mixed $value |
|
50
|
|
|
* @param int $minutes |
|
51
|
|
|
*/ |
|
52
|
|
|
public function put($key, $value, $minutes) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->storage[$key] = $value; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Write an item to the cache that lasts forever. |
|
59
|
|
|
* |
|
60
|
|
|
* @param $key |
|
61
|
|
|
* @param $value |
|
62
|
|
|
*/ |
|
63
|
|
|
public function forever($key, $value) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->put($key, $value, 0); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Delete an item from the cache. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $key |
|
72
|
|
|
*/ |
|
73
|
|
|
public function forget($key) |
|
74
|
|
|
{ |
|
75
|
|
|
unset($this->storage[$key]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Flush the entire cache. |
|
80
|
|
|
*/ |
|
81
|
|
|
public function flush() |
|
82
|
|
|
{ |
|
83
|
|
|
$this->storage = array(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|