Permalink
Browse files

Initial changeset. Added support for Memcached methods.

  • Loading branch information...
1 parent f9776b5 commit adf7c7a325852a45045c138a2d3c8c241c556759 @fauria fauria committed Apr 21, 2011
Showing with 48 additions and 19 deletions.
  1. +1 −1 config/memcached.php
  2. +47 −18 libraries/memcached_library.php
View
@@ -23,7 +23,7 @@
$memcached['config'] = array(
'prefix' => '', // Prefixes every key value (useful for multi environment setups)
- 'compression' => MEMCACHE_COMPRESSED, // Default: MEMCACHE_COMPRESSED Compression Method or FALSE
+ '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.
@@ -5,37 +5,45 @@ class Memcached_library
private $config;
private $local_cache = array();
private $m;
+ private $client_type;
private $ci;
protected $errors = array();
public function __construct()
{
-
$this->ci =& get_instance();
// Lets try to load Memcache or Memcached Class
- $this->m = class_exists('Memcache') ? "Memcache" : (class_exists('Memcached') ? "Memcached" : FALSE);
+ $this->client_type = class_exists('Memcache') ? "Memcache" : (class_exists('Memcached') ? "Memcached" : FALSE);
- if($this->m) {
+ if($this->client_type)
+ {
$this->ci->load->config('memcached');
$this->config = $this->ci->config->item('memcached');
// Which one should be loaded
- if ($this->m == "Memcached") { $this->m = new Memcached(); }
- else { $this->m = new Memcache(); }
-
- log_message('debug', "Memcached Library: Memcached Class Loaded");
- $this->auto_connect();
+ 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");
- // 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']);
- }
-
+ $this->auto_connect();
}
- else {
- log_message('debug', "Memcached Library: Failed to load Memcached or Memcache Class");
+ else
+ {
+ log_message('error', "Memcached Library: Failed to load Memcached or Memcache Class");
}
}
@@ -103,7 +111,14 @@ public function add($key = NULL, $value = NULL, $expiration = NULL)
else
{
$this->local_cache[$this->key_name($key)] = $value;
- return $this->m->add($this->key_name($key), $value, $this->config['config']['compression'], $expiration);
+ if($this->client_type == 'Memcache')
+ {
+ return $this->m->add($this->key_name($key), $value, $this->config['config']['compression'], $expiration);
+ }
+ else
+ {
+ return $this->m->add($this->key_name($key), $value, $expiration);
+ }
}
}
@@ -206,7 +221,14 @@ public function replace($key = NULL, $value = NULL, $expiration = NULL)
else
{
$this->local_cache[$this->key_name($key)] = $value;
- return $this->m->replace($this->key_name($key), $value, $this->config['config']['compression'], $expiration);
+ if($this->client_type == 'Memcache')
+ {
+ return $this->m->replace($this->key_name($key), $value, $this->config['config']['compression'], $expiration);
+ }
+ else
+ {
+ return $this->m->replace($this->key_name($key), $value, $expiration);
+ }
}
}
@@ -256,7 +278,14 @@ public function getstats($type="items")
*/
public function setcompressthreshold($tresh, $savings=0.2)
{
- return $this->m->setCompressThreshold($tresh, $savings=0.2);
+ if($this->client_type == 'Memcache')
+ {
+ return $this->m->setCompressThreshold($tresh, $savings=0.2);
+ }
+ else
+ {
+ return TRUE;
+ }
}
/*

0 comments on commit adf7c7a

Please sign in to comment.