forked from avdg/ppi-framework-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cache.php
140 lines (125 loc) · 3.4 KB
/
Cache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* PPI Cache handler
*
* @package Cache
* @author Paul Dragoonis <dragoonis@php.net>
* @license http://opensource.org/licenses/mit-license.php MIT
* @link http://www.ppiframework.com
*/
class PPI_Cache {
/**
* Defaults for the handler
*
* @var array
*/
protected $_defaults = array(
'handler' => 'disk'
);
/**
* The handler in use
*
* @var null|PPI_Cache_Interface
*/
protected $_handler = null;
/**
* The options to the cache layer. This can be an array of options
* or a string of the driver name eg: new PPI_Cache('apc');
*
* @param array|string $options
*/
function __construct(array $options = array()) {
// We now let you specify the handler as a string for quickness.
if(is_string($options)) {
$options = array('handler' => $options);
}
if(isset($options['handler'])) {
// If it's a pre instantiated cache handler then use that
if(!is_string($options['handler']) && $options['handler'] instanceof PPI_Cache_Interface) {
$this->_handler = $options['handler'];
unset($options['handler']);
}
}
$this->_defaults = ($options + $this->_defaults);
// If no handler was passed in, then we setup that handler now by the string name: i.e: 'disk'
if($this->_handler === null) {
$this->setupHandler($this->_defaults['handler']);
}
}
/**
* Initialise the cache handler
*
* @param string $handler The handler name
* @return void
* @throws PPI_Exception
*/
function setupHandler($handler) {
$handler = strtolower($handler);
$handler = 'PPI_Cache_' . ucfirst($handler);
$this->_handler = new $handler($this->_defaults);
if($this->_handler->enabled() === false) {
throw new PPI_Exception('The cache driver ' . $handler . ' is currently disabled.');
}
$this->_handler->init();
}
/**
* Get a key value from the cache
*
* @param string $key The Key
* @return mixed
*/
function get($key) {
return $this->_handler->get($key);
}
/**
* Set a value in the cache
*
* @param string $key The Key
* @param mixed $value The Value
* @param integer $ttl The TTL
* @return boolean
*/
function set($key, $value, $ttl = 0) {
return $this->_handler->set($key, $value, $ttl);
}
/**
* Check if a key exists in the cache
*
* @param string $key The Key
* @return boolean
*/
function exists($key) {
return $this->_handler->exists($key);
}
/**
* Remove a value from the cache by key
*
* @param string $key The Key
* @return boolean
*/
function remove($key) {
return $this->_handler->remove($key);
}
/**
* Get the current registered handler
*
* @return null|PPI_Cache_Interface
*/
function getHandler() {
return $this->_handler;
}
/**
* Set the current handler, we check enabled() and exec init() on the handler too to make sure it's ready to go.
*
* @throws PPI_Exception
* @param PPI_Cache_Interface $handler
* @return void
*/
function setHandler(PPI_Cache_Interface $handler) {
$this->_handler = $handler;
if($this->_handler->enabled() === false) {
throw new PPI_Exception('The cache driver ' . get_class($handler) . ' is currently disabled.');
}
$this->_handler->init();
}
}