1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Redis Management Module |
4
|
|
|
* |
5
|
|
|
* PHP Version 5 |
6
|
|
|
* |
7
|
|
|
* @category Steverobbins |
8
|
|
|
* @package Steverobbins_Redismanager |
9
|
|
|
* @author Steve Robbins <[email protected]> |
10
|
|
|
* @copyright 2014 Steve Robbins |
11
|
|
|
* @license http://creativecommons.org/licenses/by/3.0/deed.en_US Creative Commons Attribution 3.0 Unported License |
12
|
|
|
* @link https://github.com/steverobbins/Magento-Redismanager |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Redis manager helper class |
17
|
|
|
* |
18
|
|
|
* @category Steverobbins |
19
|
|
|
* @package Steverobbins_Redismanager |
20
|
|
|
* @author Steve Robbins <[email protected]> |
21
|
|
|
* @copyright 2014 Steve Robbins |
22
|
|
|
* @license http://creativecommons.org/licenses/by/3.0/deed.en_US Creative Commons Attribution 3.0 Unported License |
23
|
|
|
* @link https://github.com/steverobbins/Magento-Redismanager |
24
|
|
|
*/ |
25
|
|
|
class Steverobbins_Redismanager_Helper_Data extends Mage_Core_Helper_Abstract |
26
|
|
|
{ |
27
|
|
|
const XML_PATH_DEFAULT_SECTION = 'redismanager'; |
28
|
|
|
const XML_PATH_DEFAULT_GROUP = 'settings'; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Services cache |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $_services; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Config getter |
40
|
|
|
* |
41
|
|
|
* @param string $path |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getConfig($path) |
45
|
|
|
{ |
46
|
|
|
$bits = explode('/', $path); |
47
|
|
|
$count = count($bits); |
48
|
|
|
return Mage::getStoreConfig( |
49
|
|
|
($count == 3 ? $bits[0] : self::XML_PATH_DEFAULT_SECTION) . '/' . |
50
|
|
|
($count > 1 ? $bits[$count - 2] : self::XML_PATH_DEFAULT_GROUP) . '/' . |
51
|
|
|
$bits[$count - 1] |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Fetch all redis services |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function getServices() |
61
|
|
|
{ |
62
|
|
|
if (is_null($this->_services)) { |
63
|
|
|
if ($this->getConfig('auto')) { |
64
|
|
|
$this->_services = array(); |
65
|
|
|
$config = Mage::app()->getConfig(); |
66
|
|
|
foreach (array('cache', 'full_page_cache', 'fpc') as $cacheType) { |
67
|
|
|
$node = $config->getXpath('global/' . $cacheType . '[1]'); |
68
|
|
|
if (isset($node[0]->backend) && in_array((string)$node[0]->backend, array('Cm_Cache_Backend_Redis', 'Mage_Cache_Backend_Redis'))) { |
69
|
|
|
$this->_services[] = $this->_buildServiceArray( |
70
|
|
|
$this->__(str_replace('_', ' ', uc_words($cacheType))), |
71
|
|
|
$node[0]->backend_options->server, |
72
|
|
|
$node[0]->backend_options->port, |
73
|
|
|
$node[0]->backend_options->password, |
74
|
|
|
$node[0]->backend_options->database |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
// get session |
79
|
|
|
$node = $config->getXpath('global/redis_session'); |
80
|
|
|
if ($node) { |
81
|
|
|
$this->_services[] = $this->_buildServiceArray( |
82
|
|
|
$this->__('Session'), |
83
|
|
|
$node[0]->host, |
84
|
|
|
$node[0]->port, |
85
|
|
|
$node[0]->password, |
86
|
|
|
$node[0]->db |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} else { |
90
|
|
|
$this->_services = unserialize($this->getConfig('manual')); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
return $this->_services; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Perform a flushAll when synchflush is enabled (for use in the event observers) |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
public function flushAllByObserver() |
102
|
|
|
{ |
103
|
|
|
if ($this->getConfig('syncflush')) { |
104
|
|
|
Mage::getSingleton('core/session') |
105
|
|
|
->addSuccess('Redismanager has observed a cache flush by Magento, flushing Redis...'); |
106
|
|
|
$this->flushAll(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Flush all Redis caches |
112
|
|
|
* |
113
|
|
|
* @param string $flushThis |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
public function flushAll($flushThis = null) |
117
|
|
|
{ |
118
|
|
|
$flushed = array(); |
119
|
|
|
foreach ($this->getServices() as $service) { |
120
|
|
|
$serviceMatch = $service['host'] . ':' . $service['port']; |
121
|
|
|
if (in_array($serviceMatch, $flushed) |
122
|
|
|
|| (!is_null($flushThis) && $flushThis != $serviceMatch) |
123
|
|
|
) { |
124
|
|
|
continue; |
125
|
|
|
} |
126
|
|
|
try { |
127
|
|
|
$this->getRedisInstance( |
128
|
|
|
$service['host'], |
129
|
|
|
$service['port'], |
130
|
|
|
$service['password'], |
131
|
|
|
$service['db'] |
132
|
|
|
)->getRedis()->flushAll(); |
133
|
|
|
$flushed[] = $serviceMatch; |
134
|
|
|
$serviceName = $service['name'] . ' (' . $service['host'] . ':' . $service['port'] . ')'; |
135
|
|
|
Mage::getSingleton('core/session')->addSuccess($this->__('%s flushed', $serviceName)); |
136
|
|
|
} catch (Exception $e) { |
137
|
|
|
Mage::getSingleton('core/session')->addError($e->getMessage()); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
return $flushed; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Assign values with casting |
145
|
|
|
* |
146
|
|
|
* @param string $name |
147
|
|
|
* @param string $host |
148
|
|
|
* @param string $port |
149
|
|
|
* @param string $pass |
150
|
|
|
* @param string $db |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
protected function _buildServiceArray($name, $host, $port, $pass, $db) |
155
|
|
|
{ |
156
|
|
|
return array( |
157
|
|
|
'name' => $name, |
158
|
|
|
'host' => (string)$host, |
159
|
|
|
'port' => (string)$port, |
160
|
|
|
'password' => (string)$pass, |
161
|
|
|
'db' => (string)$db |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get Redis client |
167
|
|
|
* |
168
|
|
|
* @param string $host |
169
|
|
|
* @param string $port |
170
|
|
|
* @param string $pass |
171
|
|
|
* @param string $db |
172
|
|
|
* |
173
|
|
|
* @return Steverobbins_Redismanager_Model_Backend_Redis_Cm|Steverobbins_Redismanager_Model_Backend_Redis_Mage |
174
|
|
|
*/ |
175
|
|
|
public function getRedisInstance($host, $port, $pass, $db) |
176
|
|
|
{ |
177
|
|
|
if (class_exists('Mage_Cache_Backend_Redis')) { |
178
|
|
|
return Mage::getModel('redismanager/backend_redis_mage', array( |
179
|
|
|
'server' => $host, |
180
|
|
|
'port' => $port, |
181
|
|
|
'password' => $pass, |
182
|
|
|
'database' => $db |
183
|
|
|
)); |
184
|
|
|
} elseif (class_exists('Cm_Cache_Backend_Redis')) { |
185
|
|
|
return Mage::getModel('redismanager/backend_redis_cm', array( |
186
|
|
|
'server' => $host, |
187
|
|
|
'port' => $port, |
188
|
|
|
'password' => $pass, |
189
|
|
|
'database' => $db |
190
|
|
|
)); |
191
|
|
|
} |
192
|
|
|
Mage::throwException('Unable to determine Redis backend class.'); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|