From 7f0065ddbd80fe32e49cede2d65d231c6ec6f183 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Thu, 15 Aug 2013 17:10:58 +0200 Subject: Switch to CI's caching class This supports more caching backends and doesn't force users to install the memcache extension. Signed-off-by: Florian Pritz --- application/config/.gitignore | 2 +- application/config/config.php | 7 ++ application/config/example/memcache.php | 9 -- application/config/example/memcached.php | 17 +++ application/controllers/file.php | 6 +- application/libraries/MemcacheLibrary.php | 202 ------------------------------ application/models/mfile.php | 6 +- 7 files changed, 31 insertions(+), 218 deletions(-) delete mode 100644 application/config/example/memcache.php create mode 100644 application/config/example/memcached.php delete mode 100644 application/libraries/MemcacheLibrary.php (limited to 'application') diff --git a/application/config/.gitignore b/application/config/.gitignore index bf68cb2ad..45e1c5158 100644 --- a/application/config/.gitignore +++ b/application/config/.gitignore @@ -1,3 +1,3 @@ config-local.php database.php -memcache.php +memcached.php diff --git a/application/config/config.php b/application/config/config.php index 3ff8b816b..dda82de97 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -380,6 +380,13 @@ $config['actions_max_age'] = 60*60*24*5; // 5 days // won't be deleted $config['small_upload_size'] = 1024*10; // 10KB +// possible values: +// - apc: needs the apc module and is only useful on long running php processes +// - file: you will have to clean up the cache directory yourself (application/cache/) +// - memcached: config in application/config/memcached.php; you need the memcached module (with the D) +// - dummy: disables caching +$config['cache_backend'] = "dummy"; + // for possible drivers look into ./application/libraries/Duser/drivers/ $config['authentication_driver'] = 'db'; diff --git a/application/config/example/memcache.php b/application/config/example/memcache.php deleted file mode 100644 index a5bc7e3a8..000000000 --- a/application/config/example/memcache.php +++ /dev/null @@ -1,9 +0,0 @@ - diff --git a/application/config/example/memcached.php b/application/config/example/memcached.php new file mode 100644 index 000000000..29b145ec8 --- /dev/null +++ b/application/config/example/memcached.php @@ -0,0 +1,17 @@ + array( + "hostname" => "127.0.0.1", + "port" => 11211, + "weight" => 1, + ), + "socket" => array( + "hostname" => FCPATH.'/memcached.sock', + "port" => 0, + "weight" => 2, + ), +); + + +?> diff --git a/application/controllers/file.php b/application/controllers/file.php index 2d6a8ecc4..55ed10fea 100644 --- a/application/controllers/file.php +++ b/application/controllers/file.php @@ -155,8 +155,8 @@ class File extends CI_Controller { $this->data['filedata'] = $filedata; // highlight the file and chache the result - $this->load->library("MemcacheLibrary"); - if (! $cached = $this->memcachelibrary->get($filedata['hash'].'_'.$lexer)) { + $this->load->driver('cache', array('adapter' => $this->config->item("cache_backend"))); + if (! $cached = $this->cache->get($filedata['hash'].'_'.$lexer)) { $cached = array(); if ($lexer == "rmd") { ob_start(); @@ -175,7 +175,7 @@ class File extends CI_Controller { $ret = $this->_colorify($file, "text"); $cached["output"] = $ret["output"]; } - $this->memcachelibrary->set($filedata['hash'].'_'.$lexer, $cached, 100); + $this->cache->save($filedata['hash'].'_'.$lexer, $cached, 100); } if ($cached["return_value"] != 0) { diff --git a/application/libraries/MemcacheLibrary.php b/application/libraries/MemcacheLibrary.php deleted file mode 100644 index 1716a1d10..000000000 --- a/application/libraries/MemcacheLibrary.php +++ /dev/null @@ -1,202 +0,0 @@ - -* @license GNU General Public License -* @version 0.1 -*/ - - -class MemcacheLibrary { - - /** - * variable that holds memcached backend instance - * - * @var object - * @access public - */ - public $memcachedInstance; - - /** - * variable that holds servers for the memcache - * - * @var array - * @access public - */ - public $servers = array(); - - - /** - * main CodeIgniter instance - * - * @var object - * @access public - */ - public $CI; - - /** - * constructor function for the library - */ - public function __construct() { - - /* initialize memcached instance */ - if(class_exists("Memcache")) { - $this->memcachedInstance = new Memcache(); - } else { - throw new Exception( - "Memcached client doesn't exists in your PHP configuration" - ); - } - - /* load super CI instance */ - $this->CI =& get_instance(); - - /* load default server info */ - $this->CI->config->load("memcache"); - - /* connect to default server */ - if($this->CI->config->item("MEMCACHE_HOST") && $this->CI->config->item("MEMCACHE_PORT") !== false) { - $this->addServer($this->CI->config->item("MEMCACHE_HOST"), $this->CI->config->item("MEMCACHE_PORT")); - } - - } - - /** - * adder function for the memcache servers - * - * @access public - * @return void - */ - public function addServer($server, $port) { - $this->servers[] = array( - "server" => $server, - "port" => $port, - ); - - $this->memcachedInstance->addServer($server, $port); - } - - /** - * gets related key from the memcache - * - * @access public - */ - public function get($key) { - $this->logDebugMessage(sprintf("%s key requested from memcache", $key)); - - // hide notice if server is unreachable - $old_error_level = error_reporting(); - error_reporting($old_error_level & ~E_NOTICE); - - $ret = $this->memcachedInstance->get($key); - - error_reporting($old_error_level); - return $ret; - } - - /** - * sets related key to the memcache - * - * @access public - */ - public function set($key, $value, $expire = null) { - $this->logDebugMessage( - sprintf("%s key set to memcache. (expire: %s)",$key, $expire) - ); - - // hide notice if server is unreachable - $old_error_level = error_reporting(); - error_reporting($old_error_level & ~E_NOTICE); - - $ret = $this->memcachedInstance->set($key, $value, null, $expire); - - error_reporting($old_error_level); - return $ret; - } - - /** - * deletes related key from the memcache - * - * @access public - */ - public function delete($key) { - $this->logDebugMessage(sprintf("%s key deleted from memcache.", $key)); - // hide notice if server is unreachable - $old_error_level = error_reporting(); - error_reporting($old_error_level & ~E_NOTICE); - - $ret = $this->memcachedInstance->delete($key); - - error_reporting($old_error_level); - return $ret; - } - - /** - * increments related key from the memcache - * - * @access public - */ - public function increment($key, $offset = 1) { - $this->logDebugMessage(sprintf("%s key incremented %s times", $key, $offset)); - return $this->memcachedInstance->increment($key, $offset); - } - - /** - * decrements related key from the memcache - * - * @access public - */ - public function decrement($key, $offset = 1) { - $this->logDebugMessage(sprintf("%s key decremented %s times", $key, $offset)); - return $this->memcachedInstance->decrement($key, $offset); - } - - /** - * gets running memcached servers. - * - * @access public - * @return array - */ - public function getRunningServers() { - return $this->servers; - } - - /** - * array of server statistics, one entry per server. - * - * @access public - * @return array - */ - public function getStatistics() { - return $this->memcachedInstance->getStats(); - } - - /** - * Invalidates all items from the memcache. - * - * @access public - * @return boolean - */ - public function flush($delay = 0) { - $this->logDebugMessage(sprintf("memcache flushed! (delay: %s)", $delay)); - return $this->memcachedInstance->flush($delay); - } - - /** - * logs the memcache actions to the codeigniter's main logging system. - * - * @access private - */ - private function logDebugMessage($message) { - log_message("debug", $message); - } -} - -# vim: set noet: diff --git a/application/models/mfile.php b/application/models/mfile.php index fabce7b94..f992a0891 100644 --- a/application/models/mfile.php +++ b/application/models/mfile.php @@ -338,8 +338,8 @@ class Mfile extends CI_Model { } public function get_lexers() { - $this->load->library("MemcacheLibrary"); - if (! $lexers = $this->memcachelibrary->get('lexers')) { + $this->load->driver('cache', array('adapter' => $this->config->item("cache_backend"))); + if (! $lexers = $this->cache->get('lexers')) { $lexers = array(); $last_desc = ""; exec("python ".escapeshellarg(FCPATH."scripts/get_lexer_list.py"), $output); @@ -353,7 +353,7 @@ class Mfile extends CI_Model { $lexers[$name] = $desc; } $lexers["text"] = "Plain text"; - $this->memcachelibrary->set('lexers', $lexers, 1800); + $this->cache->save('lexers', $lexers, 1800); } return $lexers; -- cgit v1.2.3-24-g4f1b