From 1028e53ad829e9c78b67dee8a0a1838dfbc3d1e5 Mon Sep 17 00:00:00 2001 From: Tom Schlick Date: Fri, 11 Sep 2015 15:26:58 -0400 Subject: [PATCH] Applied fixes from StyleCI --- config/memcached.php | 41 +- controllers/example_memcached.php | 99 ++-- libraries/memcached_library.php | 780 +++++++++++++++--------------- 3 files changed, 447 insertions(+), 473 deletions(-) diff --git a/config/memcached.php b/config/memcached.php index a8ded34..4332319 100644 --- a/config/memcached.php +++ b/config/memcached.php @@ -1,40 +1,41 @@ array( + 'default' => [ - 'host' => 'localhost', - 'port' => '11211', - 'weight' => '1', - 'persistent' => FALSE + 'host' => 'localhost', + 'port' => '11211', + 'weight' => '1', + 'persistent' => false, - ) -); + ], +]; // -------------------------------------------------------------------------- // Configuration // -------------------------------------------------------------------------- -$memcached['config'] = array( +$memcached['config'] = [ - 'engine' => 'Memcached', // Set which caching engine you are using. Acceptable values: Memcached or Memcache - 'prefix' => '', // Prefixes every key value (useful for multi environment setups) - 'compression' => FALSE, // Default: FALSE or MEMCACHE_COMPRESSED Compression Method (Memcache only). + 'engine' => 'Memcached', // Set which caching engine you are using. Acceptable values: Memcached or Memcache + 'prefix' => '', // Prefixes every key value (useful for multi environment setups) + 'compression' => false, // Default: FALSE or MEMCACHE_COMPRESSED Compression Method (Memcache only). - // Not necessary if you already are using 'compression' - 'auto_compress_tresh' => FALSE, // Controls the minimum value length before attempting to compress automatically. - 'auto_compress_savings' => 0.2, // Specifies the minimum amount of savings to actually store the value compressed. The supplied value must be between 0 and 1. + // Not necessary if you already are using 'compression' + 'auto_compress_tresh' => false, // Controls the minimum value length before attempting to compress automatically. + 'auto_compress_savings' => 0.2, // Specifies the minimum amount of savings to actually store the value compressed. The supplied value must be between 0 and 1. - 'expiration' => 3600, // Default content expiration value (in seconds) - 'delete_expiration' => 0 // Default time between the delete command and the actual delete action occurs (in seconds) - -); + 'expiration' => 3600, // Default content expiration value (in seconds) + 'delete_expiration' => 0, // Default time between the delete command and the actual delete action occurs (in seconds) +]; $config['memcached'] = $memcached; diff --git a/controllers/example_memcached.php b/controllers/example_memcached.php index a2fee0b..c5412a6 100644 --- a/controllers/example_memcached.php +++ b/controllers/example_memcached.php @@ -1,52 +1,49 @@ load->library('memcached_library'); - - // Lets try to get the key - $results = $this->memcached_library->get('test'); - - // If the key does not exist it could mean the key was never set or expired - if (!$results) - { - // Modify this Query to your liking! - $query = $this->db->get('members', 7000); - - // Lets store the results - $this->memcached_library->add('test', $query->result()); - - // Output a basic msg - echo "Alright! Stored some results from the Query... Refresh Your Browser"; - } - else - { - // Output - var_dump($results); - - // Now let us delete the key for demonstration sake! - $this->memcached_library->delete('test'); - } - - } - - function stats() - { - $this->load->library('memcached_library'); - - echo $this->memcached_library->getversion(); - echo "
"; - - // We can use any of the following "reset, malloc, maps, cachedump, slabs, items, sizes" - $p = $this->memcached_library->getstats("sizes"); - - var_dump($p); - } -} \ No newline at end of file + +class Example_memcached extends Controller +{ + public function Example_memcached() + { + parent::Controller(); + } + + public function test() + { + // Load library + $this->load->library('memcached_library'); + + // Lets try to get the key + $results = $this->memcached_library->get('test'); + + // If the key does not exist it could mean the key was never set or expired + if (!$results) { + // Modify this Query to your liking! + $query = $this->db->get('members', 7000); + + // Lets store the results + $this->memcached_library->add('test', $query->result()); + + // Output a basic msg + echo 'Alright! Stored some results from the Query... Refresh Your Browser'; + } else { + // Output + var_dump($results); + + // Now let us delete the key for demonstration sake! + $this->memcached_library->delete('test'); + } + } + + public function stats() + { + $this->load->library('memcached_library'); + + echo $this->memcached_library->getversion(); + echo '
'; + + // We can use any of the following "reset, malloc, maps, cachedump, slabs, items, sizes" + $p = $this->memcached_library->getstats('sizes'); + + var_dump($p); + } +} diff --git a/libraries/memcached_library.php b/libraries/memcached_library.php index f19cf71..1da7275 100644 --- a/libraries/memcached_library.php +++ b/libraries/memcached_library.php @@ -1,405 +1,381 @@ -ci =& get_instance(); - - // Load the memcached library config - $this->ci->load->config('memcached'); - $this->config = $this->ci->config->item('memcached'); - - // Lets try to load Memcache or Memcached Class - $this->client_type = class_exists($this->config['config']['engine']) ? $this->config['config']['engine'] : FALSE; - - if($this->client_type) - { - // Which one should be loaded - switch($this->client_type) - { - case 'Memcached': - $this->m = new Memcached(); - break; - case 'Memcache': - $this->m = new Memcache(); - // Set Automatic Compression Settings - if ($this->config['config']['auto_compress_tresh']) - { - $this->setcompressthreshold($this->config['config']['auto_compress_tresh'], $this->config['config']['auto_compress_savings']); - } - break; - } - log_message('debug', "Memcached Library: " . $this->client_type . " Class Loaded"); - - $this->auto_connect(); - } - else - { - log_message('error', "Memcached Library: Failed to load Memcached or Memcache Class"); - } - } - - /* - +-------------------------------------+ - Name: auto_connect - Purpose: runs through all of the servers defined in - the configuration and attempts to connect to each - @param return : none - +-------------------------------------+ - */ - private function auto_connect() - { - foreach($this->config['servers'] as $key=>$server) - { - if(!$this->add_server($server)) - { - $this->errors[] = "Memcached Library: Could not connect to the server named $key"; - log_message('error', 'Memcached Library: Could not connect to the server named "'.$key.'"'); - } - else - { - log_message('debug', 'Memcached Library: Successfully connected to the server named "'.$key.'"'); - } - } - } - - /* - +-------------------------------------+ - Name: add_server - Purpose: - @param return : TRUE or FALSE - +-------------------------------------+ - */ - public function add_server($server) - { - extract($server); - return $this->m->addServer($host, $port, $weight); - } - - /* - +-------------------------------------+ - Name: add - Purpose: add an item to the memcache server(s) - @param return : TRUE or FALSE - +-------------------------------------+ - */ - public function add($key = NULL, $value = NULL, $expiration = NULL) - { - if(is_null($expiration)) - { - $expiration = $this->config['config']['expiration']; - } - if(is_array($key)) - { - foreach($key as $multi) - { - if(!isset($multi['expiration']) || $multi['expiration'] == '') - { - $multi['expiration'] = $this->config['config']['expiration']; - } - $this->add($this->key_name($multi['key']), $multi['value'], $multi['expiration']); - } - } - else - { - switch($this->client_type) - { - case 'Memcache': - $add_status = $this->m->add($this->key_name($key), $value, $this->config['config']['compression'], $expiration); - break; - - default: - case 'Memcached': - $add_status = $this->m->add($this->key_name($key), $value, $expiration); - break; - } - - return $add_status; - } - } - - /* - +-------------------------------------+ - Name: set - Purpose: similar to the add() method but uses set - @param return : TRUE or FALSE - +-------------------------------------+ - */ - public function set($key = NULL, $value = NULL, $expiration = NULL) - { - if(is_null($expiration)) - { - $expiration = $this->config['config']['expiration']; - } - if(is_array($key)) - { - foreach($key as $multi) - { - if(!isset($multi['expiration']) || $multi['expiration'] == '') - { - $multi['expiration'] = $this->config['config']['expiration']; - } - $this->set($this->key_name($multi['key']), $multi['value'], $multi['expiration']); - } - } - else - { - switch($this->client_type) - { - case 'Memcache': - $add_status = $this->m->set($this->key_name($key), $value, $this->config['config']['compression'], $expiration); - break; - - default: - case 'Memcached': - $add_status = $this->m->set($this->key_name($key), $value, $expiration); - break; - } - - return $add_status; - } - } - - /* - +-------------------------------------+ - Name: get - Purpose: gets the data for a single key or an array of keys - @param return : array of data or multi-dimensional array of data - +-------------------------------------+ - */ - public function get($key = NULL) - { - if($this->m) - { - if(is_null($key)) - { - $this->errors[] = 'The key value cannot be NULL'; - return FALSE; - } - - if(is_array($key)) - { - foreach($key as $n=>$k) - { - $key[$n] = $this->key_name($k); - } - return $this->m->getMulti($key); - } - else - { - return $this->m->get($this->key_name($key)); - } - } - return FALSE; - } - - - /* - +-------------------------------------+ - Name: delete - Purpose: deletes a single or multiple data elements from the memached servers - @param return : none - +-------------------------------------+ - */ - public function delete($key, $expiration = NULL) - { - if(is_null($key)) - { - $this->errors[] = 'The key value cannot be NULL'; - return FALSE; - } - - if(is_null($expiration)) - { - $expiration = $this->config['config']['delete_expiration']; - } - - if(is_array($key)) - { - foreach($key as $multi) - { - $this->delete($multi, $expiration); - } - } - else - { - return $this->m->delete($this->key_name($key), $expiration); - } - } - - /* - +-------------------------------------+ - Name: replace - Purpose: replaces the value of a key that already exists - @param return : none - +-------------------------------------+ - */ - public function replace($key = NULL, $value = NULL, $expiration = NULL) - { - if(is_null($expiration)) - { - $expiration = $this->config['config']['expiration']; - } - if(is_array($key)) - { - foreach($key as $multi) - { - if(!isset($multi['expiration']) || $multi['expiration'] == '') - { - $multi['expiration'] = $this->config['config']['expiration']; - } - $this->replace($multi['key'], $multi['value'], $multi['expiration']); - } - } - else - { - switch($this->client_type) - { - case 'Memcache': - $replace_status = $this->m->replace($this->key_name($key), $value, $this->config['config']['compression'], $expiration); - break; - - default: - case 'Memcached': - $replace_status = $this->m->replace($this->key_name($key), $value, $expiration); - break; - } - - return $replace_status; - } - } - - /* - +-------------------------------------+ - Name: increment - Purpose: increments a value - @param return : none - +-------------------------------------+ - */ - public function increment($key = null, $by = 1) - { - return $this->m->increment($this->key_name($key), $by); - } - - - /* - +-------------------------------------+ - Name: decrement - Purpose: decrements a value - @param return : none - +-------------------------------------+ - */ - public function decrement($key = null, $by = 1) - { - return $this->m->decrement($this->key_name($key), $by); - } - - /* - +-------------------------------------+ - Name: flush - Purpose: flushes all items from cache - @param return : none - +-------------------------------------+ - */ - public function flush() - { - return $this->m->flush(); - } - - /* - +-------------------------------------+ - Name: getversion - Purpose: Get Server Vesion Number - @param Returns a string of server version number or FALSE on failure. - +-------------------------------------+ - */ - public function getversion() - { - return $this->m->getVersion(); - } - - /* - +-------------------------------------+ - Name: getstats - Purpose: Get Server Stats - Possible: "reset, malloc, maps, cachedump, slabs, items, sizes" - @param returns an associative array with server's statistics. Array keys correspond to stats parameters and values to parameter's values. - +-------------------------------------+ - */ - public function getstats($type="items") - { - switch($this->client_type) - { - case 'Memcache': - $stats = $this->m->getStats($type); - break; - - default: - case 'Memcached': - $stats = $this->m->getStats(); - break; - } - return $stats; - } - - /* - +-------------------------------------+ - Name: setcompresstreshold - Purpose: Set When Automatic compression should kick-in - @param return TRUE/FALSE - +-------------------------------------+ - */ - public function setcompressthreshold($tresh, $savings=0.2) - { - switch($this->client_type) - { - case 'Memcache': - $setcompressthreshold_status = $this->m->setCompressThreshold($tresh, $savings=0.2); - break; - - default: - $setcompressthreshold_status = TRUE; - break; - } - return $setcompressthreshold_status; - } - - /* - +-------------------------------------+ - Name: key_name - Purpose: standardizes the key names for memcache instances - @param return : md5 key name - +-------------------------------------+ - */ - private function key_name($key) - { - return md5(strtolower($this->config['config']['prefix'].$key)); - } - - /* - +--------------------------------------+ - Name: isConected - Purpose: Check if the memcache server is connected. - +--------------------------------------+ - */ - - public function isConnected() - { - foreach($this->getstats() as $key => $server) - { - if($server['pid'] == -1) - { - return false; - } - return true; - } - - } + private $config; + private $m; + private $client_type; + private $ci; + protected $errors = []; + + public function __construct() + { + $this->ci = &get_instance(); + + // Load the memcached library config + $this->ci->load->config('memcached'); + $this->config = $this->ci->config->item('memcached'); + + // Lets try to load Memcache or Memcached Class + $this->client_type = class_exists($this->config['config']['engine']) ? $this->config['config']['engine'] : false; + + if ($this->client_type) { + // Which one should be loaded + switch ($this->client_type) { + case 'Memcached': + $this->m = new Memcached(); + break; + case 'Memcache': + $this->m = new Memcache(); + // Set Automatic Compression Settings + if ($this->config['config']['auto_compress_tresh']) { + $this->setcompressthreshold($this->config['config']['auto_compress_tresh'], $this->config['config']['auto_compress_savings']); + } + break; + } + log_message('debug', 'Memcached Library: '.$this->client_type.' Class Loaded'); + + $this->auto_connect(); + } else { + log_message('error', 'Memcached Library: Failed to load Memcached or Memcache Class'); + } + } + + /* + +-------------------------------------+ + Name: auto_connect + Purpose: runs through all of the servers defined in + the configuration and attempts to connect to each + @param return : none + +-------------------------------------+ + */ + + private function auto_connect() + { + foreach ($this->config['servers'] as $key => $server) { + if (!$this->add_server($server)) { + $this->errors[] = "Memcached Library: Could not connect to the server named $key"; + log_message('error', 'Memcached Library: Could not connect to the server named "'.$key.'"'); + } else { + log_message('debug', 'Memcached Library: Successfully connected to the server named "'.$key.'"'); + } + } + } + + /* + +-------------------------------------+ + Name: add_server + Purpose: + @param return : TRUE or FALSE + +-------------------------------------+ + */ + + public function add_server($server) + { + extract($server); + + return $this->m->addServer($host, $port, $weight); + } + + /* + +-------------------------------------+ + Name: add + Purpose: add an item to the memcache server(s) + @param return : TRUE or FALSE + +-------------------------------------+ + */ + + public function add($key = null, $value = null, $expiration = null) + { + if (is_null($expiration)) { + $expiration = $this->config['config']['expiration']; + } + if (is_array($key)) { + foreach ($key as $multi) { + if (!isset($multi['expiration']) || $multi['expiration'] == '') { + $multi['expiration'] = $this->config['config']['expiration']; + } + $this->add($this->key_name($multi['key']), $multi['value'], $multi['expiration']); + } + } else { + switch ($this->client_type) { + case 'Memcache': + $add_status = $this->m->add($this->key_name($key), $value, $this->config['config']['compression'], $expiration); + break; + + default: + case 'Memcached': + $add_status = $this->m->add($this->key_name($key), $value, $expiration); + break; + } + + return $add_status; + } + } + + /* + +-------------------------------------+ + Name: set + Purpose: similar to the add() method but uses set + @param return : TRUE or FALSE + +-------------------------------------+ + */ + + public function set($key = null, $value = null, $expiration = null) + { + if (is_null($expiration)) { + $expiration = $this->config['config']['expiration']; + } + if (is_array($key)) { + foreach ($key as $multi) { + if (!isset($multi['expiration']) || $multi['expiration'] == '') { + $multi['expiration'] = $this->config['config']['expiration']; + } + $this->set($this->key_name($multi['key']), $multi['value'], $multi['expiration']); + } + } else { + switch ($this->client_type) { + case 'Memcache': + $add_status = $this->m->set($this->key_name($key), $value, $this->config['config']['compression'], $expiration); + break; + + default: + case 'Memcached': + $add_status = $this->m->set($this->key_name($key), $value, $expiration); + break; + } + + return $add_status; + } + } + + /* + +-------------------------------------+ + Name: get + Purpose: gets the data for a single key or an array of keys + @param return : array of data or multi-dimensional array of data + +-------------------------------------+ + */ + + public function get($key = null) + { + if ($this->m) { + if (is_null($key)) { + $this->errors[] = 'The key value cannot be NULL'; + + return false; + } + + if (is_array($key)) { + foreach ($key as $n => $k) { + $key[$n] = $this->key_name($k); + } + + return $this->m->getMulti($key); + } else { + return $this->m->get($this->key_name($key)); + } + } + + return false; + } + + /* + +-------------------------------------+ + Name: delete + Purpose: deletes a single or multiple data elements from the memached servers + @param return : none + +-------------------------------------+ + */ + + public function delete($key, $expiration = null) + { + if (is_null($key)) { + $this->errors[] = 'The key value cannot be NULL'; + + return false; + } + + if (is_null($expiration)) { + $expiration = $this->config['config']['delete_expiration']; + } + + if (is_array($key)) { + foreach ($key as $multi) { + $this->delete($multi, $expiration); + } + } else { + return $this->m->delete($this->key_name($key), $expiration); + } + } + + /* + +-------------------------------------+ + Name: replace + Purpose: replaces the value of a key that already exists + @param return : none + +-------------------------------------+ + */ + + public function replace($key = null, $value = null, $expiration = null) + { + if (is_null($expiration)) { + $expiration = $this->config['config']['expiration']; + } + if (is_array($key)) { + foreach ($key as $multi) { + if (!isset($multi['expiration']) || $multi['expiration'] == '') { + $multi['expiration'] = $this->config['config']['expiration']; + } + $this->replace($multi['key'], $multi['value'], $multi['expiration']); + } + } else { + switch ($this->client_type) { + case 'Memcache': + $replace_status = $this->m->replace($this->key_name($key), $value, $this->config['config']['compression'], $expiration); + break; + + default: + case 'Memcached': + $replace_status = $this->m->replace($this->key_name($key), $value, $expiration); + break; + } + + return $replace_status; + } + } + + /* + +-------------------------------------+ + Name: increment + Purpose: increments a value + @param return : none + +-------------------------------------+ + */ + + public function increment($key = null, $by = 1) + { + return $this->m->increment($this->key_name($key), $by); + } + + /* + +-------------------------------------+ + Name: decrement + Purpose: decrements a value + @param return : none + +-------------------------------------+ + */ + + public function decrement($key = null, $by = 1) + { + return $this->m->decrement($this->key_name($key), $by); + } + + /* + +-------------------------------------+ + Name: flush + Purpose: flushes all items from cache + @param return : none + +-------------------------------------+ + */ + + public function flush() + { + return $this->m->flush(); + } + + /* + +-------------------------------------+ + Name: getversion + Purpose: Get Server Vesion Number + @param Returns a string of server version number or FALSE on failure. + +-------------------------------------+ + */ + + public function getversion() + { + return $this->m->getVersion(); + } + + /* + +-------------------------------------+ + Name: getstats + Purpose: Get Server Stats + Possible: "reset, malloc, maps, cachedump, slabs, items, sizes" + @param returns an associative array with server's statistics. Array keys correspond to stats parameters and values to parameter's values. + +-------------------------------------+ + */ + + public function getstats($type = 'items') + { + switch ($this->client_type) { + case 'Memcache': + $stats = $this->m->getStats($type); + break; + + default: + case 'Memcached': + $stats = $this->m->getStats(); + break; + } + + return $stats; + } + + /* + +-------------------------------------+ + Name: setcompresstreshold + Purpose: Set When Automatic compression should kick-in + @param return TRUE/FALSE + +-------------------------------------+ + */ + + public function setcompressthreshold($tresh, $savings = 0.2) + { + switch ($this->client_type) { + case 'Memcache': + $setcompressthreshold_status = $this->m->setCompressThreshold($tresh, $savings = 0.2); + break; + + default: + $setcompressthreshold_status = true; + break; + } + + return $setcompressthreshold_status; + } + + /* + +-------------------------------------+ + Name: key_name + Purpose: standardizes the key names for memcache instances + @param return : md5 key name + +-------------------------------------+ + */ + + private function key_name($key) + { + return md5(strtolower($this->config['config']['prefix'].$key)); + } + + /* + +--------------------------------------+ + Name: isConected + Purpose: Check if the memcache server is connected. + +--------------------------------------+ + */ + + public function isConnected() + { + foreach ($this->getstats() as $key => $server) { + if ($server['pid'] == -1) { + return false; + } + + return true; + } + } }