Skip to content
Snippets Groups Projects
Commit 85f22acb authored by Tim van Dijen's avatar Tim van Dijen
Browse files

Replace SimpleSAML_Memcache with namespaced version

parent c6c7f58d
No related branches found
No related tags found
No related merge requests found
...@@ -33,7 +33,7 @@ $warnServerDown = 0; ...@@ -33,7 +33,7 @@ $warnServerDown = 0;
$warnBigSlab = 0; $warnBigSlab = 0;
// We use the stats interface to determine which servers exists // We use the stats interface to determine which servers exists
$stats = SimpleSAML_Memcache::getRawStats(); $stats = \SimpleSAML\Memcache::getRawStats();
$keys = array(); $keys = array();
foreach ($stats as $group) { foreach ($stats as $group) {
...@@ -61,7 +61,7 @@ echo("Starting synchronization.\n"); ...@@ -61,7 +61,7 @@ echo("Starting synchronization.\n");
$skipped = 0; $skipped = 0;
$sync = 0; $sync = 0;
foreach ($keys as $key) { foreach ($keys as $key) {
$res = SimpleSAML_Memcache::get($key); $res = \SimpleSAML\Memcache::get($key);
if ($res === null) { if ($res === null) {
$skipped += 1; $skipped += 1;
} else { } else {
......
<?php <?php
namespace SimpleSAML;
/** /**
* This file implements functions to read and write to a group of memcache * This file implements functions to read and write to a group of memcache
* servers. * servers.
...@@ -16,7 +18,8 @@ ...@@ -16,7 +18,8 @@
* @author Olav Morken, UNINETT AS. * @author Olav Morken, UNINETT AS.
* @package SimpleSAMLphp * @package SimpleSAMLphp
*/ */
class SimpleSAML_Memcache
class Memcache
{ {
/** /**
* Cache of the memcache servers we are using. * Cache of the memcache servers we are using.
...@@ -43,7 +46,7 @@ class SimpleSAML_Memcache ...@@ -43,7 +46,7 @@ class SimpleSAML_Memcache
*/ */
public static function get($key) public static function get($key)
{ {
SimpleSAML\Logger::debug("loading key $key from memcache"); \SimpleSAML\Logger::debug("loading key $key from memcache");
$latestInfo = null; $latestInfo = null;
$latestTime = 0.0; $latestTime = 0.0;
...@@ -146,7 +149,7 @@ class SimpleSAML_Memcache ...@@ -146,7 +149,7 @@ class SimpleSAML_Memcache
*/ */
public static function set($key, $value, $expire = null) public static function set($key, $value, $expire = null)
{ {
SimpleSAML\Logger::debug("saving key $key to memcache"); \SimpleSAML\Logger::debug("saving key $key to memcache");
$savedInfo = array( $savedInfo = array(
'timestamp' => microtime(true), 'timestamp' => microtime(true),
'data' => $value 'data' => $value
...@@ -160,7 +163,7 @@ class SimpleSAML_Memcache ...@@ -160,7 +163,7 @@ class SimpleSAML_Memcache
// store this object to all groups of memcache servers // store this object to all groups of memcache servers
foreach (self::getMemcacheServers() as $server) { foreach (self::getMemcacheServers() as $server) {
if (self::$extension === 'memcached') { if (self::$extension === '\memcached') {
$server->set($key, $savedInfoSerialized, $expire); $server->set($key, $savedInfoSerialized, $expire);
} else { } else {
$server->set($key, $savedInfoSerialized, 0, $expire); $server->set($key, $savedInfoSerialized, 0, $expire);
...@@ -177,7 +180,7 @@ class SimpleSAML_Memcache ...@@ -177,7 +180,7 @@ class SimpleSAML_Memcache
public static function delete($key) public static function delete($key)
{ {
assert(is_string($key)); assert(is_string($key));
SimpleSAML\Logger::debug("deleting key $key from memcache"); \SimpleSAML\Logger::debug("deleting key $key from memcache");
// store this object to all groups of memcache servers // store this object to all groups of memcache servers
foreach (self::getMemcacheServers() as $server) { foreach (self::getMemcacheServers() as $server) {
...@@ -207,13 +210,13 @@ class SimpleSAML_Memcache ...@@ -207,13 +210,13 @@ class SimpleSAML_Memcache
* @param Memcache $memcache The Memcache object we should add this server to. * @param Memcache $memcache The Memcache object we should add this server to.
* @param array $server An associative array with the configuration options for the server to add. * @param array $server An associative array with the configuration options for the server to add.
* *
* @throws Exception If any configuration option for the server is invalid. * @throws \Exception If any configuration option for the server is invalid.
*/ */
private static function addMemcacheServer($memcache, $server) private static function addMemcacheServer($memcache, $server)
{ {
// the hostname option is required // the hostname option is required
if (!array_key_exists('hostname', $server)) { if (!array_key_exists('hostname', $server)) {
throw new Exception( throw new \Exception(
"hostname setting missing from server in the 'memcache_store.servers' configuration option." "hostname setting missing from server in the 'memcache_store.servers' configuration option."
); );
} }
...@@ -222,7 +225,7 @@ class SimpleSAML_Memcache ...@@ -222,7 +225,7 @@ class SimpleSAML_Memcache
// the hostname must be a valid string // the hostname must be a valid string
if (!is_string($hostname)) { if (!is_string($hostname)) {
throw new Exception( throw new \Exception(
"Invalid hostname for server in the 'memcache_store.servers' configuration option. The hostname is". "Invalid hostname for server in the 'memcache_store.servers' configuration option. The hostname is".
' supposed to be a string.' ' supposed to be a string.'
); );
...@@ -242,7 +245,7 @@ class SimpleSAML_Memcache ...@@ -242,7 +245,7 @@ class SimpleSAML_Memcache
// get the port number from the array, and validate it // get the port number from the array, and validate it
$port = (int) $server['port']; $port = (int) $server['port'];
if (($port <= 0) || ($port > 65535)) { if (($port <= 0) || ($port > 65535)) {
throw new Exception( throw new \Exception(
"Invalid port for server in the 'memcache_store.servers' configuration option. The port number". "Invalid port for server in the 'memcache_store.servers' configuration option. The port number".
' is supposed to be an integer between 0 and 65535.' ' is supposed to be an integer between 0 and 65535.'
); );
...@@ -261,7 +264,7 @@ class SimpleSAML_Memcache ...@@ -261,7 +264,7 @@ class SimpleSAML_Memcache
// get the weight and validate it // get the weight and validate it
$weight = (int) $server['weight']; $weight = (int) $server['weight'];
if ($weight <= 0) { if ($weight <= 0) {
throw new Exception( throw new \Exception(
"Invalid weight for server in the 'memcache_store.servers' configuration option. The weight is". "Invalid weight for server in the 'memcache_store.servers' configuration option. The weight is".
' supposed to be a positive integer.' ' supposed to be a positive integer.'
); );
...@@ -276,7 +279,7 @@ class SimpleSAML_Memcache ...@@ -276,7 +279,7 @@ class SimpleSAML_Memcache
// get the timeout and validate it // get the timeout and validate it
$timeout = (int) $server['timeout']; $timeout = (int) $server['timeout'];
if ($timeout <= 0) { if ($timeout <= 0) {
throw new Exception( throw new \Exception(
"Invalid timeout for server in the 'memcache_store.servers' configuration option. The timeout is". "Invalid timeout for server in the 'memcache_store.servers' configuration option. The timeout is".
' supposed to be a positive integer.' ' supposed to be a positive integer.'
); );
...@@ -287,7 +290,7 @@ class SimpleSAML_Memcache ...@@ -287,7 +290,7 @@ class SimpleSAML_Memcache
} }
// add this server to the Memcache object // add this server to the Memcache object
if (self::$extension === 'memcached') { if (self::$extension === '\memcached') {
$memcache->addServer($hostname, $port); $memcache->addServer($hostname, $port);
} else { } else {
$memcache->addServer($hostname, $port, true, $weight, $timeout, $timeout, true); $memcache->addServer($hostname, $port, true, $weight, $timeout, $timeout, true);
...@@ -303,13 +306,13 @@ class SimpleSAML_Memcache ...@@ -303,13 +306,13 @@ class SimpleSAML_Memcache
* *
* @return Memcache A Memcache object of the servers in the group * @return Memcache A Memcache object of the servers in the group
* *
* @throws Exception If the servers configuration is invalid. * @throws \Exception If the servers configuration is invalid.
*/ */
private static function loadMemcacheServerGroup(array $group) private static function loadMemcacheServerGroup(array $group)
{ {
$class = class_exists('Memcache') ? 'Memcache' : (class_exists('Memcached') ? 'Memcached' : false); $class = class_exists('Memcache') ? 'Memcache' : (class_exists('Memcached') ? 'Memcached' : false);
if (!$class) { if (!$class) {
throw new Exception('Missing Memcached implementation. You must install either the Memcache or Memcached extension.'); throw new \Exception('Missing Memcached implementation. You must install either the Memcache or Memcached extension.');
} }
self::$extension = strtolower($class); self::$extension = strtolower($class);
...@@ -320,7 +323,7 @@ class SimpleSAML_Memcache ...@@ -320,7 +323,7 @@ class SimpleSAML_Memcache
foreach ($group as $index => $server) { foreach ($group as $index => $server) {
// make sure that we don't have an index. An index would be a sign of invalid configuration // make sure that we don't have an index. An index would be a sign of invalid configuration
if (!is_int($index)) { if (!is_int($index)) {
throw new Exception( throw new \Exception(
"Invalid index on element in the 'memcache_store.servers' configuration option. Perhaps you". "Invalid index on element in the 'memcache_store.servers' configuration option. Perhaps you".
' have forgotten to add an array(...) around one of the server groups? The invalid index was: '. ' have forgotten to add an array(...) around one of the server groups? The invalid index was: '.
$index $index
...@@ -329,7 +332,7 @@ class SimpleSAML_Memcache ...@@ -329,7 +332,7 @@ class SimpleSAML_Memcache
// make sure that the server object is an array. Each server is an array with name-value pairs // make sure that the server object is an array. Each server is an array with name-value pairs
if (!is_array($server)) { if (!is_array($server)) {
throw new Exception( throw new \Exception(
'Invalid value for the server with index '.$index. 'Invalid value for the server with index '.$index.
'. Remeber that the \'memcache_store.servers\' configuration option'. '. Remeber that the \'memcache_store.servers\' configuration option'.
' contains an array of arrays of arrays.' ' contains an array of arrays of arrays.'
...@@ -349,7 +352,7 @@ class SimpleSAML_Memcache ...@@ -349,7 +352,7 @@ class SimpleSAML_Memcache
* *
* @return Memcache[] Array with Memcache objects. * @return Memcache[] Array with Memcache objects.
* *
* @throws Exception If the servers configuration is invalid. * @throws \Exception If the servers configuration is invalid.
*/ */
private static function getMemcacheServers() private static function getMemcacheServers()
{ {
...@@ -371,7 +374,7 @@ class SimpleSAML_Memcache ...@@ -371,7 +374,7 @@ class SimpleSAML_Memcache
foreach ($groups as $index => $group) { foreach ($groups as $index => $group) {
// make sure that the group doesn't have an index. An index would be a sign of invalid configuration // make sure that the group doesn't have an index. An index would be a sign of invalid configuration
if (!is_int($index)) { if (!is_int($index)) {
throw new Exception( throw new \Exception(
"Invalid index on element in the 'memcache_store.servers'". "Invalid index on element in the 'memcache_store.servers'".
' configuration option. Perhaps you have forgotten to add an array(...)'. ' configuration option. Perhaps you have forgotten to add an array(...)'.
' around one of the server groups? The invalid index was: '.$index ' around one of the server groups? The invalid index was: '.$index
...@@ -383,7 +386,7 @@ class SimpleSAML_Memcache ...@@ -383,7 +386,7 @@ class SimpleSAML_Memcache
* an array of name => value pairs for that server. * an array of name => value pairs for that server.
*/ */
if (!is_array($group)) { if (!is_array($group)) {
throw new Exception( throw new \Exception(
"Invalid value for the server with index ".$index. "Invalid value for the server with index ".$index.
". Remeber that the 'memcache_store.servers' configuration option". ". Remeber that the 'memcache_store.servers' configuration option".
' contains an array of arrays of arrays.' ' contains an array of arrays of arrays.'
...@@ -408,7 +411,7 @@ class SimpleSAML_Memcache ...@@ -408,7 +411,7 @@ class SimpleSAML_Memcache
* *
* @return integer The value which should be passed in the set(...) calls to the memcache objects. * @return integer The value which should be passed in the set(...) calls to the memcache objects.
* *
* @throws Exception If the option 'memcache_store.expires' has a negative value. * @throws \Exception If the option 'memcache_store.expires' has a negative value.
*/ */
private static function getExpireTime() private static function getExpireTime()
{ {
...@@ -421,7 +424,7 @@ class SimpleSAML_Memcache ...@@ -421,7 +424,7 @@ class SimpleSAML_Memcache
// it must be a positive integer // it must be a positive integer
if ($expire < 0) { if ($expire < 0) {
throw new Exception( throw new \Exception(
"The value of 'memcache_store.expires' in the configuration can't be a negative integer." "The value of 'memcache_store.expires' in the configuration can't be a negative integer."
); );
} }
...@@ -447,7 +450,7 @@ class SimpleSAML_Memcache ...@@ -447,7 +450,7 @@ class SimpleSAML_Memcache
* *
* @return array Array with the names of each stat and an array with the value for each server group. * @return array Array with the names of each stat and an array with the value for each server group.
* *
* @throws Exception If memcache server status couldn't be retrieved. * @throws \Exception If memcache server status couldn't be retrieved.
*/ */
public static function getStats() public static function getStats()
{ {
...@@ -457,11 +460,11 @@ class SimpleSAML_Memcache ...@@ -457,11 +460,11 @@ class SimpleSAML_Memcache
$stats = method_exists($sg, 'getExtendedStats') ? $sg->getExtendedStats() : $sg->getStats(); $stats = method_exists($sg, 'getExtendedStats') ? $sg->getExtendedStats() : $sg->getStats();
foreach ($stats as $server => $data) { foreach ($stats as $server => $data) {
if ($data === false) { if ($data === false) {
throw new Exception('Failed to get memcache server status.'); throw new \Exception('Failed to get memcache server status.');
} }
} }
$stats = SimpleSAML\Utils\Arrays::transpose($stats); $stats = \SimpleSAML\Utils\Arrays::transpose($stats);
$ret = array_merge_recursive($ret, $stats); $ret = array_merge_recursive($ret, $stats);
} }
......
...@@ -42,7 +42,7 @@ class Memcache extends Store ...@@ -42,7 +42,7 @@ class Memcache extends Store
assert(is_string($type)); assert(is_string($type));
assert(is_string($key)); assert(is_string($key));
return \SimpleSAML_Memcache::get($this->prefix . '.' . $type . '.' . $key); return \SimpleSAML\Memcache::get($this->prefix . '.' . $type . '.' . $key);
} }
...@@ -64,7 +64,7 @@ class Memcache extends Store ...@@ -64,7 +64,7 @@ class Memcache extends Store
$expire = 0; $expire = 0;
} }
\SimpleSAML_Memcache::set($this->prefix . '.' . $type . '.' . $key, $value, $expire); \SimpleSAML\Memcache::set($this->prefix . '.' . $type . '.' . $key, $value, $expire);
} }
...@@ -79,6 +79,6 @@ class Memcache extends Store ...@@ -79,6 +79,6 @@ class Memcache extends Store
assert(is_string($type)); assert(is_string($type));
assert(is_string($key)); assert(is_string($key));
\SimpleSAML_Memcache::delete($this->prefix . '.' . $type . '.' . $key); \SimpleSAML\Memcache::delete($this->prefix . '.' . $type . '.' . $key);
} }
} }
...@@ -7,29 +7,30 @@ ...@@ -7,29 +7,30 @@
* *
* @param array &$hookinfo hookinfo * @param array &$hookinfo hookinfo
*/ */
function memcacheMonitor_hook_sanitycheck(&$hookinfo) { function memcacheMonitor_hook_sanitycheck(&$hookinfo) {
assert(is_array($hookinfo)); assert(is_array($hookinfo));
assert(array_key_exists('errors', $hookinfo)); assert(array_key_exists('errors', $hookinfo));
assert(array_key_exists('info', $hookinfo)); assert(array_key_exists('info', $hookinfo));
try { try {
$servers = SimpleSAML_Memcache::getRawStats(); $servers = \SimpleSAML\Memcache::getRawStats();
} catch (Exception $e) { } catch (\Exception $e) {
$hookinfo['errors'][] = '[memcacheMonitor] Error parsing memcache configuration: ' . $e->getMessage(); $hookinfo['errors'][] = '[memcacheMonitor] Error parsing memcache configuration: ' . $e->getMessage();
return; return;
} }
$allOK = TRUE; $allOK = true;
foreach ($servers as $group) { foreach ($servers as $group) {
foreach ($group as $server => $status) { foreach ($group as $server => $status) {
if ($status === FALSE) { if ($status === false) {
$hookinfo['errors'][] = '[memcacheMonitor] No response from server: ' . $server; $hookinfo['errors'][] = '[memcacheMonitor] No response from server: ' . $server;
$allOK = FALSE; $allOK = false;
} }
} }
} }
if ($allOK) { if ($allOK) {
$hookinfo['info'][] = '[memcacheMonitor] All servers responding.'; $hookinfo['info'][] = '[memcacheMonitor] All servers responding.';
} }
} }
...@@ -81,7 +81,7 @@ $formats = array( ...@@ -81,7 +81,7 @@ $formats = array(
'uptime' => 'hours', 'uptime' => 'hours',
); );
$statsraw = \SimpleSAML_Memcache::getStats(); $statsraw = \SimpleSAML\Memcache::getStats();
$stats = $statsraw; $stats = $statsraw;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment