Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #10 from beryllium/formatting

Update formatting to comply with Symfony2 standards
  • Loading branch information...
commit 1ba627de05c0824b9a9cbc9677c52c49167b1007 2 parents e16fb88 + f4a76ab
Jeremy Livingston jeremylivingston authored
222 Cache.php
View
@@ -6,136 +6,130 @@
use Beryllium\CacheBundle\CacheClientInterface;
/**
- * Cache
- *
+ * Cache
+ *
* @uses CacheInterface
- * @package
+ * @package
* @version $id$
- * @author Kevin Boyd <beryllium@beryllium.ca>
+ * @author Kevin Boyd <beryllium@beryllium.ca>
* @license See LICENSE.md
*/
class Cache implements CacheInterface
{
- public $dic = false;
+ public $dic = false;
- protected $client = null;
- protected $safe = false;
+ protected $client = null;
+ protected $safe = false;
- /**
- * Prep the cache
- *
- * @param CacheClientInterface $client Optional cache object/service
- * @access public
- * @return void
- */
- public function __construct( CacheClientInterface $client = null )
- {
- if ( !empty( $client ) )
- {
- if ( is_object( $client ) && ( $client instanceof CacheClientInterface ) )
- $this->client = $client;
- else
- {
- throw new \Exception( 'Invalid Cache Client Interface' );
- }
- }
- }
+ /**
+ * Prep the cache
+ *
+ * @param CacheClientInterface $client Optional cache object/service
+ * @access public
+ * @return void
+ */
+ public function __construct(CacheClientInterface $client = null)
+ {
+ if (!empty($client)) {
+ if (is_object($client) && ($client instanceof CacheClientInterface)) {
+ $this->client = $client;
+ }
+ else {
+ throw new \Exception('Invalid Cache Client Interface');
+ }
+ }
+ }
- /**
- * Inject a dependency injection container (optional)
- *
- * @param mixed $dic The container
- * @access public
- * @return void
- */
- public function setContainer( $dic )
- {
- $this->dic = $dic;
- }
+ /**
+ * Inject a dependency injection container (optional)
+ *
+ * @param mixed $dic The container
+ * @access public
+ * @return void
+ */
+ public function setContainer($dic)
+ {
+ $this->dic = $dic;
+ }
- /**
- * Inject a cache client interface to interact with a custom cache service
- *
- * @param CacheClientInterface $client The client object or service
- * @access public
- * @return void
- */
- public function setClient( CacheClientInterface $client )
- {
- if ( is_object( $client ) && ( $client instanceof CacheClientInterface ) )
- $this->client = $client;
- else
- {
- throw new \Exception( 'Invalid Cache Client Interface' );
- }
- }
+ /**
+ * Inject a cache client interface to interact with a custom cache service
+ *
+ * @param CacheClientInterface $client The client object or service
+ * @access public
+ * @return void
+ */
+ public function setClient(CacheClientInterface $client)
+ {
+ if (is_object($client) && ($client instanceof CacheClientInterface))
+ $this->client = $client;
+ else {
+ throw new \Exception('Invalid Cache Client Interface');
+ }
+ }
- /**
- * Retrieve a value from the cache using the provided key
- *
- * @param string|array $key The unique key or array of keys identifying the data to be retrieved.
- * @access public
- * @return mixed The requested data, or false if there is an error
- */
- public function get( $key )
- {
- if ( $this->isSafe() && !empty( $key ) )
- {
- return $this->client->get( $key );
- }
+ /**
+ * Retrieve a value from the cache using the provided key
+ *
+ * @param string|array $key The unique key or array of keys identifying the data to be retrieved.
+ * @access public
+ * @return mixed The requested data, or false if there is an error
+ */
+ public function get($key)
+ {
+ if ($this->isSafe() && !empty($key)) {
+ return $this->client->get($key);
+ }
- return false;
- }
+ return false;
+ }
- /**
- * Add a key/value to the cache
- *
- * @param string $key A unique key to identify the data you want to store
- * @param string $value The value you want to store in the cache
- * @param int $ttl Optional: Lifetime of the data (default: 300 seconds - five minutes)
- * @access public
- * @return mixed Whatever the CacheClientObject returns, or false.
- */
- public function set( $key, $value, $ttl = 300 )
- {
- if ( $this->isSafe() && !empty( $key ) )
- {
- return $this->client->set( $key, $value, $ttl );
- }
+ /**
+ * Add a key/value to the cache
+ *
+ * @param string $key A unique key to identify the data you want to store
+ * @param string $value The value you want to store in the cache
+ * @param int $ttl Optional: Lifetime of the data (default: 300 seconds - five minutes)
+ * @access public
+ * @return mixed Whatever the CacheClientObject returns, or false.
+ */
+ public function set($key, $value, $ttl = 300)
+ {
+ if ($this->isSafe() && !empty($key)) {
+ return $this->client->set($key, $value, $ttl);
+ }
- return false;
- }
+ return false;
+ }
- /**
- * Delete a key from the cache
- *
- * @param string $key Unique key
- * @access public
- * @return void
- */
- public function delete( $key )
- {
- if ( $this->isSafe() && !empty( $key ) )
- {
- return $this->client->delete( $key );
- }
+ /**
+ * Delete a key from the cache
+ *
+ * @param string $key Unique key
+ * @access public
+ * @return void
+ */
+ public function delete($key)
+ {
+ if ($this->isSafe() && !empty($key)) {
+ return $this->client->delete($key);
+ }
- return false;
- }
+ return false;
+ }
- /**
- * Checks if the cache is in a usable state
- *
- * @access public
- * @return boolean True if the cache is usable, otherwise false
- */
- public function isSafe()
- {
- if ( $this->client instanceof CacheClientInterface )
- {
- return $this->client->isSafe();
- }
+ /**
+ * Checks if the cache is in a usable state
+ *
+ * @access public
+ * @return boolean True if the cache is usable, otherwise false
+ */
+ public function isSafe()
+ {
+ if ($this->client instanceof CacheClientInterface) {
+ return $this->client->isSafe();
+ }
- return $this->safe;
- }
+ return $this->safe;
+ }
}
34 CacheInterface.php
View
@@ -14,9 +14,33 @@
*/
interface CacheInterface
{
- public function __construct( CacheClientInterface $client = null );
- public function get( $key );
- public function set( $key, $value, $ttl );
- public function delete( $key );
- public function isSafe();
+ /**
+ * @param CacheClientInterface $client
+ */
+ public function __construct(CacheClientInterface $client = null);
+
+ /**
+ * @param string $key
+ * @return mixed
+ */
+ public function get($key);
+
+ /**
+ * @param string $key
+ * @param mixed $value
+ * @param int $ttl
+ * @return mixed
+ */
+ public function set($key, $value, $ttl);
+
+ /**
+ * @param string $key
+ * @return mixed
+ */
+ public function delete($key);
+
+ /**
+ * @return bool
+ */
+ public function isSafe();
}
254 Client/FilecacheClient.php
View
@@ -8,132 +8,146 @@
* Completely untested and undocumented. Use at your own risk!
*
* Fixes appreciated!
- *
+ *
* @uses ClientCacheInterface
- * @package
+ * @package
* @version $id$
- * @author Kevin Boyd <beryllium@beryllium.ca>
+ * @author Kevin Boyd <beryllium@beryllium.ca>
* @license See LICENSE.md
*/
class FilecacheClient implements ClientCacheInterface
{
- protected $path = null;
- public $dic = null;
-
- public function __construct( $path = null )
- {
- if ( !empty( $path ) && is_dir( $path ) && is_writable( $path ) )
- {
- $this->path = $path;
- }
- else
- {
- $this->path = null;
- }
- }
-
- public function setContainer( $dic )
- {
- $this->dic = $dic;
- }
-
- public function get( $key )
- {
- if ( !$this->isSafe() || empty( $key ) )
- {
- return false;
- }
-
- //@todo Implement multi-get
- if ( file_exists( $this->buildFilename( $key ) )
- {
- $file = file_get_contents( $this->buildFilename( $key ) );
- $file = unserialize( $file );
-
- if ( !is_array( $file ) )
- {
- return false;
- }
- else if ( $file[ 'key' ] != $key )
- {
- return false;
- }
- else if ( time() - $file[ 'ctime' ] > $file[ 'ttl' ] )
- {
- return false;
- }
- else
- {
- return unserialize( $file[ 'value' ] );
- }
- }
- else
- {
- return false;
- }
- }
-
- public function set( $key, $value, $ttl = 300 )
- {
- $file = array();
- $file[ 'key' ] = $key;
-
- $file[ 'value' ] = serialize( $value );
-
- $file[ 'ttl' ] = $ttl;
- $file[ 'ctime' ] = time();
-
- if ( $this->isSafe() && !empty( $key ) )
- {
- return file_put_contents( $this->buildFilename( $key ), serialize( $file ) );
- }
- else
- {
- return false;
- }
- }
-
- public function delete( $key )
- {
- $filename = $this->buildFilename( $key );
-
- if ( file_exists( $filename ) )
+ protected $path = null;
+ public $dic = null;
+
+ /**
+ * @param string|null $path
+ */
+ public function __construct($path = null)
+ {
+ if (!empty($path) && is_dir($path) && is_writable($path)) {
+ $this->path = $path;
+ } else {
+ $this->path = null;
+ }
+ }
+
+ /**
+ * @param $dic
+ */
+ public function setContainer($dic)
+ {
+ $this->dic = $dic;
+ }
+
+ /**
+ * @param string $key
+ * @return bool|mixed
+ */
+ public function get($key)
+ {
+ if (!$this->isSafe() || empty($key)) {
+ return false;
+ }
+
+ // @todo Implement multi-get
+ if (file_exists($this->buildFilename($key))) {
+ $file = file_get_contents($this->buildFilename($key));
+ $file = unserialize($file);
+
+ if (!is_array($file)) {
+ return false;
+ } else if ($file['key'] != $key) {
+ return false;
+ } else {
+ if (time() - $file['ctime'] > $file['ttl']) {
+ return false;
+ } else {
+ return unserialize($file['value']);
+ }
+ }
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * @param string $key
+ * @param mixed $value
+ * @param int $ttl
+ * @return bool|int
+ */
+ public function set($key, $value, $ttl = 300)
+ {
+ $file = array(
+ 'key' => $key,
+ 'value' => serialize($value),
+ 'ttl' => $ttl,
+ 'ctime' => time(),
+ );
+
+ if ($this->isSafe() && !empty($key)) {
+ return file_put_contents($this->buildFilename($key), serialize($file));
+ }
+
+ return false;
+ }
+
+ /**
+ * @param string $key
+ */
+ public function delete($key)
+ {
+ $filename = $this->buildFilename($key);
+
+ if (file_exists($filename)) {
+ unlink($filename);
+ }
+ }
+
+ /**
+ * @param string $path
+ * @return bool
+ */
+ public function setPath($path)
+ {
+ if (!empty($path) && is_dir($path) && is_writable($path)) {
+ $this->path = $path;
+
+ return true;
+ }
+
+ $this->path = null;
+
+ return false;
+ }
+
+ /**
+ * @return bool
+ */
+ public function isSafe()
+ {
+ if (is_null($this->path)) {
+ return false;
+ }
+
+ return is_dir($this->path) && is_writable($this->path);
+ }
+
+ /**
+ * @return bool
+ */
+ public function isFull()
+ {
+ //Check if the cache has exceeded its alotted size
+ }
+
+ /**
+ * @param string $key
+ * @return string
+ */
+ protected function buildFilename($key)
{
- unlink( $filename );
+ return $this->path . md5($key) . '_file.cache';
}
- }
-
- public function setPath( $path )
- {
- if ( !empty( $path ) && is_dir( $path ) && is_writable( $path ) )
- {
- $this->path = $path;
- return true;
- }
- else
- {
- $this->path = null;
- return false;
- }
- }
-
- public function isSafe()
- {
- if ( is_null( $this->path ) )
- {
- return false;
- }
-
- return is_dir( $this->path ) && is_writable( $this->path );
- }
-
- public function isFull()
- {
- //Check if the cache has exceeded its alotted size
- }
-
- protected function buildFilename( $key )
- {
- return $this->path . md5( $key ) . '_file.cache';
- }
}
346 Client/MemcacheClient.php
View
@@ -15,190 +15,174 @@
*/
class MemcacheClient implements CacheClientInterface
{
- protected $safe = false;
- protected $mem = null;
- protected $servers = array();
- protected $sockttl = 0.2;
- protected $compression = false;
-
- /**
- * Constructs the cache client using an injected Memcache instance
- *
- * @access public
- * @return void
- */
- public function __construct( \Memcache $memcache )
- {
+ protected $safe = false;
+ protected $mem = null;
+ protected $servers = array();
+ protected $sockttl = 0.2;
+ protected $compression = false;
+
+ /**
+ * Constructs the cache client using an injected Memcache instance
+ *
+ * @access public
+ */
+ public function __construct(\Memcache $memcache)
+ {
$this->mem = $memcache;
- }
-
- /**
- * Add a server to the memcache pool.
- *
- * Does not probe server, does not set Safe to true.
- *
- * Should really be private, or modified to handle the probeServer action itself.
- *
- * @param string $ip Location of memcache server
- * @param int $port Optional: Port number (default: 11211)
- * @access public
- * @return void
- */
- public function addServer( $ip, $port = 11211 )
- {
- if ( is_object( $this->mem ) )
- {
- return $this->mem->addServer( $ip, $port );
- }
- }
-
- /**
- * Add an array of servers to the memcache pool
- *
- * Uses ProbeServer to verify that the connection is valid.
- *
- * Format of array:
- *
- * $servers[ '127.0.0.1' ] = 11211;
- *
- * Logic is somewhat flawed, of course, because it wouldn't let you add multiple
- * servers on the same IP.
- *
- * Serious flaw, right? ;-)
- *
- * @param array $servers See above format definition
- * @access public
- * @return void
- */
- public function addServers( array $servers )
- {
- if ( count( $servers ) == 0 )
- {
- return false;
- }
-
- foreach ( $servers as $ip=>$port )
- {
- if ( intval( $port ) == 0 )
- {
- $port = NULL;
- }
- if ( $this->probeServer( $ip, $port ) )
- {
- $status = $this->addServer( $ip, $port );
- $this->safe = true;
- }
- }
- }
-
- /**
- * Spend a few tenths of a second opening a socket to the requested IP and port
- *
- * The purpose of this is to verify that the server exists before trying to add it,
- * to cut down on weird errors when doing ->get(). This could be a controversial or
- * flawed way to go about this.
- *
- * @param string $ip IP address (or hostname, possibly)
- * @param int $port Port that memcache is running on
- * @access public
- * @return boolean True if the socket opens successfully, or false if it fails
- */
- public function probeServer( $ip, $port )
- {
- $errno = null;
- $errstr = null;
- $fp = @fsockopen( $ip, $port, $errno, $errstr, $this->sockttl );
-
- if ( $fp )
- {
- fclose( $fp );
- return true;
- }
- else
- {
- return false;
- }
- }
-
- /**
- * Retrieve a value from memcache
- *
- * @param string|array $key Unique identifier or array of identifiers
- * @access public
- * @return mixed Requested value, or false if an error occurs
- */
- public function get( $key )
- {
- if ( $this->isSafe() )
- {
- return $this->mem->get( $key );
- }
- else
- {
- return false;
- }
- }
-
- /**
- * Add a value to the memcache
- *
- * @param string $key Unique key
- * @param mixed $value A value. I recommend a string, be it serialized or not - other values haven't been tested :)
- * @param int $ttl Number of seconds for the value to be valid for
- * @access public
- * @return void
- */
- public function set( $key, $value, $ttl )
- {
- if ( $this->isSafe() )
- {
- return $this->mem->set( $key, $value, $this->compression, $ttl );
- }
- else
- {
- return false;
- }
- }
-
- /**
- * Delete a value from the memcache
- *
- * @param string $key Unique key
- * @access public
- * @return void
- */
- public function delete( $key )
- {
- if ( $this->isSafe() )
+ }
+
+ /**
+ * Add a server to the memcache pool.
+ *
+ * Does not probe server, does not set Safe to true.
+ *
+ * Should really be private, or modified to handle the probeServer action itself.
+ *
+ * @param string $ip Location of memcache server
+ * @param int $port Optional: Port number (default: 11211)
+ * @access public
+ * @return void
+ */
+ public function addServer($ip, $port = 11211)
+ {
+ if (is_object($this->mem)) {
+ return $this->mem->addServer($ip, $port);
+ }
+ }
+
+ /**
+ * Add an array of servers to the memcache pool
+ *
+ * Uses ProbeServer to verify that the connection is valid.
+ *
+ * Format of array:
+ *
+ * $servers[ '127.0.0.1' ] = 11211;
+ *
+ * Logic is somewhat flawed, of course, because it wouldn't let you add multiple
+ * servers on the same IP.
+ *
+ * Serious flaw, right? ;-)
+ *
+ * @param array $servers See above format definition
+ * @access public
+ * @return void
+ */
+ public function addServers(array $servers)
+ {
+ if (count($servers) == 0) {
+ return false;
+ }
+
+ foreach ($servers as $ip => $port) {
+ if (intval($port) == 0) {
+ $port = null;
+ }
+
+ if ($this->probeServer($ip, $port)) {
+ $status = $this->addServer($ip, $port);
+ $this->safe = true;
+ }
+ }
+ }
+
+ /**
+ * Spend a few tenths of a second opening a socket to the requested IP and port
+ *
+ * The purpose of this is to verify that the server exists before trying to add it,
+ * to cut down on weird errors when doing ->get(). This could be a controversial or
+ * flawed way to go about this.
+ *
+ * @param string $ip IP address (or hostname, possibly)
+ * @param int $port Port that memcache is running on
+ * @access public
+ * @return boolean True if the socket opens successfully, or false if it fails
+ */
+ public function probeServer($ip, $port)
+ {
+ $errno = null;
+ $errstr = null;
+ $fp = @fsockopen($ip, $port, $errno, $errstr, $this->sockttl);
+
+ if ($fp) {
+ fclose($fp);
+
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Retrieve a value from memcache
+ *
+ * @param string|array $key Unique identifier or array of identifiers
+ * @access public
+ * @return mixed Requested value, or false if an error occurs
+ */
+ public function get($key)
+ {
+ if ($this->isSafe()) {
+ return $this->mem->get($key);
+ }
+
+ return false;
+ }
+
+ /**
+ * Add a value to the memcache
+ *
+ * @param string $key Unique key
+ * @param mixed $value A value. I recommend a string, be it serialized or not - other values haven't been tested :)
+ * @param int $ttl Number of seconds for the value to be valid for
+ * @access public
+ * @return void
+ */
+ public function set($key, $value, $ttl)
{
- return $this->mem->delete( $key, 0 );
+ if ($this->isSafe()) {
+ return $this->mem->set($key, $value, $this->compression, $ttl);
+ }
+
+ return false;
}
- else
+
+ /**
+ * Delete a value from the memcache
+ *
+ * @param string $key Unique key
+ * @access public
+ * @return void
+ */
+ public function delete($key)
+ {
+ if ($this->isSafe()) {
+ return $this->mem->delete($key, 0);
+ }
+
+ return false;
+ }
+
+ /**
+ * Check if the cache is live
+ *
+ * @access public
+ * @return True if a valid server has been added, otherwise false
+ */
+ public function isSafe()
+ {
+ return $this->safe;
+ }
+
+ /**
+ * getStats returns the result of Memcache::getExtendedStats(), an associative array
+ * containing arrays of server stats
+ *
+ * @access public
+ * @return array Server stats array
+ */
+ public function getStats()
{
- return false;
+ return $this->mem->getExtendedStats();
}
- }
-
- /**
- * Check if the cache is live
- *
- * @access public
- * @return True if a valid server has been added, otherwise false
- */
- public function isSafe()
- {
- return $this->safe;
- }
-
- /**
- * getStats returns the result of Memcache::getExtendedStats(), an associative array
- * containing arrays of server stats
- *
- * @access public
- * @return array Server stats array
- */
- public function getStats()
- {
- return $this->mem->getExtendedStats();
- }
}
280 Command/CacheClientStatsCommand.php
View
@@ -10,146 +10,150 @@
/**
* Provides a command-line interface for viewing cache client stats
- *
- * @author Kevin Boyd <beryllium@beryllium.ca>
+ *
+ * @author Kevin Boyd <beryllium@beryllium.ca>
* @license See LICENSE.md
*/
class CacheClientStatsCommand extends ContainerAwareCommand
{
-
- /**
- * Configure the CLI task
- *
- * @return void
- */
- protected function configure()
- {
- $this
- ->setName('cacheclient:stats')
- ->setDescription('Display Cache Statistics')
- ->addOption('debug', 'd', InputOption::VALUE_NONE, 'Show debugging information')
- ;
- }
-
- /**
- * Execute the CLI task
- *
- * @param InputInterface $input
- * @param OutputInterface $output
- * @return void
- */
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $debug = $input->getOption( 'debug' );
- $client = $this->getContainer()->get( 'beryllium_cache.client' );
-
- if ( is_object( $client ) && method_exists( $client, 'getStats' ) )
- {
- $text = $this->formatStats( $client->getStats(), $debug );
- }
- else
- {
- $text = 'Active cache client does not have a stats function.';
- }
-
- $output->writeln($text);
- }
-
- /**
- * Format the raw array for the command line report
- *
- * @param array $stats An array of memcache::extendedstats
- * @param bool $debug (default: false) True causes a raw dump of the stats array to be provided
- * @return string ConsoleComponent-formatted output, suitable for ->writeln() usage
- */
- public function formatStats( $stats, $debug = false )
- {
- if ( !is_array( $stats ) )
- {
- return "No statistics returned.\n";
- }
-
- if ( count( $stats ) == 0 )
- {
- return "No statistics returned.\n";
- }
-
- $out = "Servers found: " . count( $stats ) . "\n\n";
- foreach( $stats as $host=>$item )
- {
- if ( !is_array( $item ) || count( $item ) == 0 )
- {
- $out .= " <error>" . $host . "</error>\n";
-
- continue;
- }
-
- $out .= "<info>Host:\t" . $host . "</info>\n";
-
- $out .= "\tUsage: " . $this->formatUsage( $item[ 'bytes' ], $item[ 'limit_maxbytes' ] ) . "\n";
- $out .= "\tUptime: " . $this->formatUptime( $item[ 'uptime' ] ) . "\n";
- $out .= "\tOpen Connections: " . $item[ 'curr_connections' ] . "\n";
- $out .= "\tHits: " . $item[ 'get_hits' ] . "\n";
- $out .= "\tMisses: " . $item[ 'get_misses' ] . "\n";
- if ( $item[ 'get_hits' ] + $item[ 'get_misses' ] > 0 )
- $out .= "\tHelpfulness: " . round( $item[ 'get_hits' ] / ( $item[ 'get_hits' ] + $item[ 'get_misses' ] ) * 100, 2 ) . "%\n";
-
- if ( $debug )
- {
- $out .= "\n";
- foreach( $item as $key => $value )
- {
- $out .= "\t" . $key . ': ' . $value . "\n";
- }
- }
- }
-
- return $out;
- }
-
- /**
- * Format the usage stats
- *
- * @param integer $bytes Cache usage (in bytes)
- * @param integer $maxbytes Cache maximum size (in bytes)
- * @return string A short string with friendly formatting
- */
- public function formatUsage( $bytes, $maxbytes )
- {
- if ( !is_numeric( $maxbytes ) || $maxbytes < 1 )
- {
- return '(undefined)';
- }
-
- $out = round( $bytes / $maxbytes, 3 ) . "% (";
- $out .= round( $bytes / 1024 / 1024, 2 ) . 'MB of ';
- $out .= round( $maxbytes / 1024 / 1024, 2 ) . 'MB)';
-
- return $out;
- }
-
- /**
- * Formats the uptime to be friendlier
- *
- * @param integer $uptime Cache server uptime (in seconds)
- * @return string A short string with friendly formatting
- */
- public function formatUptime( $uptime )
- {
- $days = floor( $uptime / 24 / 60 / 60 );
- $days_remainder = $uptime - ( $days * 24 * 60 * 60 );
- $hours = floor( $days_remainder / 60 / 60 );
- $hours_remainder = $days_remainder - ( $hours * 60 * 60 );
- $minutes = floor( $hours_remainder / 60 );
- $minutes_remainder = $hours_remainder - ( $minutes * 60 );
- $seconds = $minutes_remainder;
-
- $out = $uptime . ' seconds (';
- if ( $days > 0 ) $out .= $days . ' days, ';
- if ( $hours > 0 ) $out .= $hours . ' hours, ';
- if ( $minutes > 0 ) $out .= $minutes . ' minutes, ';
- if ( $seconds > 0 ) $out .= $seconds . ' seconds';
-
- return $out . ')';
- }
+ /**
+ * Configure the CLI task
+ *
+ * @return void
+ */
+ protected function configure()
+ {
+ $this->setName('cacheclient:stats')
+ ->setDescription('Display Cache Statistics')
+ ->addOption('debug', 'd', InputOption::VALUE_NONE, 'Show debugging information');
+ }
+
+ /**
+ * Execute the CLI task
+ *
+ * @param InputInterface $input
+ * @param OutputInterface $output
+ * @return void
+ */
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $debug = $input->getOption('debug');
+ $client = $this->getContainer()->get('beryllium_cache.client');
+
+ if (is_object($client) && method_exists($client, 'getStats')) {
+ $text = $this->formatStats($client->getStats(), $debug);
+ } else {
+ $text = 'Active cache client does not have a stats function.';
+ }
+
+ $output->writeln($text);
+ }
+
+ /**
+ * Format the raw array for the command line report
+ *
+ * @param array $stats An array of memcache::extendedstats
+ * @param bool $debug (default: false) True causes a raw dump of the stats array to be provided
+ * @return string ConsoleComponent-formatted output, suitable for ->writeln() usage
+ */
+ public function formatStats($stats, $debug = false)
+ {
+ if (!is_array($stats)) {
+ return "No statistics returned.\n";
+ }
+
+ if (count($stats) == 0) {
+ return "No statistics returned.\n";
+ }
+
+ $out = "Servers found: " . count($stats) . "\n\n";
+ foreach ($stats as $host => $item) {
+ if (!is_array($item) || count($item) == 0) {
+ $out .= " <error>" . $host . "</error>\n";
+
+ continue;
+ }
+
+ $out .= "<info>Host:\t" . $host . "</info>\n";
+
+ $out .= "\tUsage: " . $this->formatUsage($item['bytes'], $item['limit_maxbytes']) . "\n";
+ $out .= "\tUptime: " . $this->formatUptime($item['uptime']) . "\n";
+ $out .= "\tOpen Connections: " . $item['curr_connections'] . "\n";
+ $out .= "\tHits: " . $item['get_hits'] . "\n";
+ $out .= "\tMisses: " . $item['get_misses'] . "\n";
+ if ($item['get_hits'] + $item['get_misses'] > 0) {
+ $out .= "\tHelpfulness: " . round(
+ $item['get_hits'] / ($item['get_hits'] + $item['get_misses']) * 100,
+ 2
+ ) . "%\n";
+ }
+
+ if ($debug) {
+ $out .= "\n";
+ foreach ($item as $key => $value) {
+ $out .= "\t" . $key . ': ' . $value . "\n";
+ }
+ }
+ }
+
+ return $out;
+ }
+
+ /**
+ * Format the usage stats
+ *
+ * @param integer $bytes Cache usage (in bytes)
+ * @param integer $maxbytes Cache maximum size (in bytes)
+ * @return string A short string with friendly formatting
+ */
+ public function formatUsage($bytes, $maxbytes)
+ {
+ if (!is_numeric($maxbytes) || $maxbytes < 1) {
+ return '(undefined)';
+ }
+
+ $out = round($bytes / $maxbytes, 3) . "% (";
+ $out .= round($bytes / 1024 / 1024, 2) . 'MB of ';
+ $out .= round($maxbytes / 1024 / 1024, 2) . 'MB)';
+
+ return $out;
+ }
+
+ /**
+ * Formats the uptime to be friendlier
+ *
+ * @param integer $uptime Cache server uptime (in seconds)
+ * @return string A short string with friendly formatting
+ */
+ public function formatUptime($uptime)
+ {
+ $days = floor($uptime / 24 / 60 / 60);
+ $days_remainder = $uptime - ($days * 24 * 60 * 60);
+ $hours = floor($days_remainder / 60 / 60);
+ $hours_remainder = $days_remainder - ($hours * 60 * 60);
+ $minutes = floor($hours_remainder / 60);
+ $minutes_remainder = $hours_remainder - ($minutes * 60);
+ $seconds = $minutes_remainder;
+ $out = $uptime . ' seconds (';
+
+ if ($days > 0) {
+ $out .= $days . ' days, ';
+ }
+
+ if ($hours > 0) {
+ $out .= $hours . ' hours, ';
+ }
+
+
+ if ($minutes > 0) {
+ $out .= $minutes . ' minutes, ';
+ }
+
+
+ if ($seconds > 0) {
+ $out .= $seconds . ' seconds';
+ }
+
+ return $out . ')';
+ }
}
2  DependencyInjection/BerylliumCacheExtension.php
View
@@ -25,4 +25,4 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
-}
+}
2  DependencyInjection/Configuration.php
View
@@ -26,4 +26,4 @@ public function getConfigTreeBuilder()
return $treeBuilder;
}
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.