summaryrefslogtreecommitdiffstats
path: root/system/libraries/Cache
diff options
context:
space:
mode:
Diffstat (limited to 'system/libraries/Cache')
-rw-r--r--system/libraries/Cache/Cache.php140
-rw-r--r--system/libraries/Cache/drivers/Cache_apc.php7
-rw-r--r--system/libraries/Cache/drivers/Cache_dummy.php7
-rw-r--r--system/libraries/Cache/drivers/Cache_file.php37
-rw-r--r--system/libraries/Cache/drivers/Cache_memcached.php35
-rw-r--r--system/libraries/Cache/drivers/Cache_redis.php237
-rw-r--r--system/libraries/Cache/drivers/Cache_wincache.php9
7 files changed, 380 insertions, 92 deletions
diff --git a/system/libraries/Cache/Cache.php b/system/libraries/Cache/Cache.php
index f98241617..e76fdc557 100644
--- a/system/libraries/Cache/Cache.php
+++ b/system/libraries/Cache/Cache.php
@@ -1,4 +1,4 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php
/**
* CodeIgniter
*
@@ -18,12 +18,13 @@
*
* @package CodeIgniter
* @author EllisLab Dev Team
- * @copyright Copyright (c) 2006 - 2012 EllisLab, Inc.
+ * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
* @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* @link http://codeigniter.com
* @since Version 2.0
* @filesource
*/
+defined('BASEPATH') OR exit('No direct script access allowed');
/**
* CodeIgniter Caching Class
@@ -36,32 +37,62 @@
*/
class CI_Cache extends CI_Driver_Library {
- protected $valid_drivers = array(
- 'cache_apc',
- 'cache_file',
- 'cache_memcached',
- 'cache_dummy',
- 'cache_wincache'
- );
+ /**
+ * Valid cache drivers
+ *
+ * @var array
+ */
+ protected $valid_drivers = array(
+ 'cache_apc',
+ 'cache_dummy',
+ 'cache_file',
+ 'cache_memcached',
+ 'cache_redis',
+ 'cache_wincache'
+ );
+
+ /**
+ * Path of cache files (if file-based cache)
+ *
+ * @var string
+ */
+ protected $_cache_path = NULL;
+
+ /**
+ * Reference to the driver
+ *
+ * @var mixed
+ */
+ protected $_adapter = 'dummy';
- protected $_cache_path = NULL; // Path of cache files (if file-based cache)
- protected $_adapter = 'dummy';
- protected $_backup_driver;
+ /**
+ * Fallback driver
+ *
+ * @var string
+ */
+ protected $_backup_driver = 'dummy';
+
+ /**
+ * Cache key prefix
+ *
+ * @var string
+ */
+ public $key_prefix = '';
/**
* Constructor
*
* Initialize class properties based on the configuration array.
*
- * @param array
+ * @param array $config = array()
* @return void
*/
public function __construct($config = array())
{
$default_config = array(
- 'adapter',
- 'memcached'
- );
+ 'adapter',
+ 'memcached'
+ );
foreach ($default_config as $key)
{
@@ -73,11 +104,26 @@ class CI_Cache extends CI_Driver_Library {
}
}
- if (isset($config['backup']))
+ isset($config['key_prefix']) && $this->key_prefix = $config['key_prefix'];
+
+ if (isset($config['backup']) && in_array('cache_'.$config['backup'], $this->valid_drivers))
{
- if (in_array('cache_'.$config['backup'], $this->valid_drivers))
+ $this->_backup_driver = $config['backup'];
+ }
+
+ // If the specified adapter isn't available, check the backup.
+ if ( ! $this->is_supported($this->_adapter))
+ {
+ if ( ! $this->is_supported($this->_backup_driver))
{
- $this->_backup_driver = $config['backup'];
+ // Backup isn't supported either. Default to 'Dummy' driver.
+ log_message('error', 'Cache adapter "'.$this->_adapter.'" and backup "'.$this->_backup_driver.'" are both unavailable. Cache is now using "Dummy" adapter.');
+ $this->_adapter = 'dummy';
+ }
+ else
+ {
+ // Backup is supported. Set it to primary.
+ $this->_adapter = $this->_backup_driver;
}
}
}
@@ -90,12 +136,12 @@ class CI_Cache extends CI_Driver_Library {
* Look for a value in the cache. If it exists, return the data
* if not, return FALSE
*
- * @param string
- * @return mixed value that is stored/FALSE on failure
+ * @param string $id
+ * @return mixed value matching $id or FALSE on failure
*/
public function get($id)
{
- return $this->{$this->_adapter}->get($id);
+ return $this->{$this->_adapter}->get($this->key_prefix.$id);
}
// ------------------------------------------------------------------------
@@ -103,14 +149,14 @@ class CI_Cache extends CI_Driver_Library {
/**
* Cache Save
*
- * @param string Unique Key
- * @param mixed Data to store
- * @param int Length of time (in seconds) to cache the data
- * @return bool true on success/false on failure
+ * @param string $id Cache ID
+ * @param mixed $data Data to store
+ * @param int $ttl = 60 Cache TTL (in seconds)
+ * @return bool TRUE on success, FALSE on failure
*/
public function save($id, $data, $ttl = 60)
{
- return $this->{$this->_adapter}->save($id, $data, $ttl);
+ return $this->{$this->_adapter}->save($this->key_prefix.$id, $data, $ttl);
}
// ------------------------------------------------------------------------
@@ -118,12 +164,12 @@ class CI_Cache extends CI_Driver_Library {
/**
* Delete from Cache
*
- * @param mixed unique identifier of the item in the cache
- * @return bool true on success/false on failure
+ * @param string $id Cache ID
+ * @return bool TRUE on success, FALSE on failure
*/
public function delete($id)
{
- return $this->{$this->_adapter}->delete($id);
+ return $this->{$this->_adapter}->delete($this->key_prefix.$id);
}
// ------------------------------------------------------------------------
@@ -131,7 +177,7 @@ class CI_Cache extends CI_Driver_Library {
/**
* Clean the cache
*
- * @return bool false on failure/true on success
+ * @return bool TRUE on success, FALSE on failure
*/
public function clean()
{
@@ -143,8 +189,8 @@ class CI_Cache extends CI_Driver_Library {
/**
* Cache Info
*
- * @param string user/filehits
- * @return mixed array on success, false on failure
+ * @param string $type = 'user' user/filehits
+ * @return mixed array containing cache info on success OR FALSE on failure
*/
public function cache_info($type = 'user')
{
@@ -156,12 +202,12 @@ class CI_Cache extends CI_Driver_Library {
/**
* Get Cache Metadata
*
- * @param mixed key to get cache metadata on
- * @return mixed return value from child method
+ * @param string $id key to get cache metadata on
+ * @return mixed cache item metadata
*/
public function get_metadata($id)
{
- return $this->{$this->_adapter}->get_metadata($id);
+ return $this->{$this->_adapter}->get_metadata($this->key_prefix.$id);
}
// ------------------------------------------------------------------------
@@ -169,7 +215,7 @@ class CI_Cache extends CI_Driver_Library {
/**
* Is the requested driver supported in this environment?
*
- * @param string The driver to test.
+ * @param string $driver The driver to test
* @return array
*/
public function is_supported($driver)
@@ -184,26 +230,6 @@ class CI_Cache extends CI_Driver_Library {
return $support[$driver];
}
- // ------------------------------------------------------------------------
-
- /**
- * __get()
- *
- * @param child
- * @return object
- */
- public function __get($child)
- {
- $obj = parent::__get($child);
-
- if ( ! $this->is_supported($child))
- {
- $this->_adapter = $this->_backup_driver;
- }
-
- return $obj;
- }
-
}
/* End of file Cache.php */
diff --git a/system/libraries/Cache/drivers/Cache_apc.php b/system/libraries/Cache/drivers/Cache_apc.php
index 59ab67533..46e633463 100644
--- a/system/libraries/Cache/drivers/Cache_apc.php
+++ b/system/libraries/Cache/drivers/Cache_apc.php
@@ -1,4 +1,4 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php
/**
* CodeIgniter
*
@@ -18,12 +18,13 @@
*
* @package CodeIgniter
* @author EllisLab Dev Team
- * @copyright Copyright (c) 2006 - 2012 EllisLab, Inc.
+ * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
* @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* @link http://codeigniter.com
* @since Version 2.0
* @filesource
*/
+defined('BASEPATH') OR exit('No direct script access allowed');
/**
* CodeIgniter APC Caching Class
@@ -75,7 +76,7 @@ class CI_Cache_apc extends CI_Driver {
* Delete from Cache
*
* @param mixed unique identifier of the item in the cache
- * @param bool true on success/false on failure
+ * @return bool true on success/false on failure
*/
public function delete($id)
{
diff --git a/system/libraries/Cache/drivers/Cache_dummy.php b/system/libraries/Cache/drivers/Cache_dummy.php
index e8b791c5b..a3bdc3c80 100644
--- a/system/libraries/Cache/drivers/Cache_dummy.php
+++ b/system/libraries/Cache/drivers/Cache_dummy.php
@@ -1,4 +1,4 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php
/**
* CodeIgniter
*
@@ -18,12 +18,13 @@
*
* @package CodeIgniter
* @author EllisLab Dev Team
- * @copyright Copyright (c) 2006 - 2012 EllisLab, Inc.
+ * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
* @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* @link http://codeigniter.com
* @since Version 2.0
* @filesource
*/
+defined('BASEPATH') OR exit('No direct script access allowed');
/**
* CodeIgniter Dummy Caching Class
@@ -70,7 +71,7 @@ class CI_Cache_dummy extends CI_Driver {
* Delete from Cache
*
* @param mixed unique identifier of the item in the cache
- * @param bool TRUE, simulating success
+ * @return bool TRUE, simulating success
*/
public function delete($id)
{
diff --git a/system/libraries/Cache/drivers/Cache_file.php b/system/libraries/Cache/drivers/Cache_file.php
index dd27aa90e..5d370380e 100644
--- a/system/libraries/Cache/drivers/Cache_file.php
+++ b/system/libraries/Cache/drivers/Cache_file.php
@@ -1,4 +1,4 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php
/**
* CodeIgniter
*
@@ -18,15 +18,16 @@
*
* @package CodeIgniter
* @author EllisLab Dev Team
- * @copyright Copyright (c) 2006 - 2012 EllisLab, Inc.
+ * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
* @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* @link http://codeigniter.com
* @since Version 2.0
* @filesource
*/
+defined('BASEPATH') OR exit('No direct script access allowed');
/**
- * CodeIgniter Memcached Caching Class
+ * CodeIgniter File Caching Class
*
* @package CodeIgniter
* @subpackage Libraries
@@ -36,14 +37,24 @@
*/
class CI_Cache_file extends CI_Driver {
+ /**
+ * Directory in which to save cache files
+ *
+ * @var string
+ */
protected $_cache_path;
+ /**
+ * Initialize file-based cache
+ *
+ * @return void
+ */
public function __construct()
{
$CI =& get_instance();
$CI->load->helper('file');
$path = $CI->config->item('cache_path');
- $this->_cache_path = ($path == '') ? APPPATH.'cache/' : $path;
+ $this->_cache_path = ($path === '') ? APPPATH.'cache/' : $path;
}
// ------------------------------------------------------------------------
@@ -61,9 +72,9 @@ class CI_Cache_file extends CI_Driver {
return FALSE;
}
- $data = unserialize(read_file($this->_cache_path.$id));
+ $data = unserialize(file_get_contents($this->_cache_path.$id));
- if (time() > $data['time'] + $data['ttl'])
+ if ($data['ttl'] > 0 && time() > $data['time'] + $data['ttl'])
{
unlink($this->_cache_path.$id);
return FALSE;
@@ -86,10 +97,10 @@ class CI_Cache_file extends CI_Driver {
public function save($id, $data, $ttl = 60)
{
$contents = array(
- 'time' => time(),
- 'ttl' => $ttl,
- 'data' => $data
- );
+ 'time' => time(),
+ 'ttl' => $ttl,
+ 'data' => $data
+ );
if (write_file($this->_cache_path.$id, serialize($contents)))
{
@@ -155,19 +166,19 @@ class CI_Cache_file extends CI_Driver {
return FALSE;
}
- $data = unserialize(read_file($this->_cache_path.$id));
+ $data = unserialize(file_get_contents($this->_cache_path.$id));
if (is_array($data))
{
$mtime = filemtime($this->_cache_path.$id);
- if ( ! isset($data['data']['ttl']))
+ if ( ! isset($data['ttl']))
{
return FALSE;
}
return array(
- 'expire' => $mtime + $data['data']['ttl'],
+ 'expire' => $mtime + $data['ttl'],
'mtime' => $mtime
);
}
diff --git a/system/libraries/Cache/drivers/Cache_memcached.php b/system/libraries/Cache/drivers/Cache_memcached.php
index 4cd5f3d6f..fae002347 100644
--- a/system/libraries/Cache/drivers/Cache_memcached.php
+++ b/system/libraries/Cache/drivers/Cache_memcached.php
@@ -1,4 +1,4 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php
/**
* CodeIgniter
*
@@ -18,12 +18,13 @@
*
* @package CodeIgniter
* @author EllisLab Dev Team
- * @copyright Copyright (c) 2006 - 2012 EllisLab, Inc.
+ * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
* @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* @link http://codeigniter.com
* @since Version 2.0
* @filesource
*/
+defined('BASEPATH') OR exit('No direct script access allowed');
/**
* CodeIgniter Memcached Caching Class
@@ -36,15 +37,25 @@
*/
class CI_Cache_memcached extends CI_Driver {
- protected $_memcached; // Holds the memcached object
+ /**
+ * Holds the memcached object
+ *
+ * @var object
+ */
+ protected $_memcached;
+ /**
+ * Memcached configuration
+ *
+ * @var array
+ */
protected $_memcache_conf = array(
- 'default' => array(
- 'default_host' => '127.0.0.1',
- 'default_port' => 11211,
- 'default_weight' => 1
- )
- );
+ 'default' => array(
+ 'default_host' => '127.0.0.1',
+ 'default_port' => 11211,
+ 'default_weight' => 1
+ )
+ );
/**
* Fetch from cache
@@ -67,7 +78,7 @@ class CI_Cache_memcached extends CI_Driver {
* @param string unique identifier
* @param mixed data being cached
* @param int time to live
- * @return bool true on success, false on failure
+ * @return bool true on success, false on failure
*/
public function save($id, $data, $ttl = 60)
{
@@ -89,7 +100,7 @@ class CI_Cache_memcached extends CI_Driver {
* Delete from Cache
*
* @param mixed key to be deleted.
- * @return bool true on success, false on failure
+ * @return bool true on success, false on failure
*/
public function delete($id)
{
@@ -202,7 +213,7 @@ class CI_Cache_memcached extends CI_Driver {
$cache_server['weight'] = $this->_memcache_conf['default']['default_weight'];
}
- if (get_class($this->_memcached) == 'Memcache')
+ if (get_class($this->_memcached) === 'Memcache')
{
// Third parameter is persistance and defaults to TRUE.
$this->_memcached->addServer(
diff --git a/system/libraries/Cache/drivers/Cache_redis.php b/system/libraries/Cache/drivers/Cache_redis.php
new file mode 100644
index 000000000..60033344b
--- /dev/null
+++ b/system/libraries/Cache/drivers/Cache_redis.php
@@ -0,0 +1,237 @@
+<?php
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 5.2.4 or newer
+ *
+ * NOTICE OF LICENSE
+ *
+ * Licensed under the Open Software License version 3.0
+ *
+ * This source file is subject to the Open Software License (OSL 3.0) that is
+ * bundled with this package in the files license.txt / license.rst. It is
+ * also available through the world wide web at this URL:
+ * http://opensource.org/licenses/OSL-3.0
+ * If you did not receive a copy of the license and are unable to obtain it
+ * through the world wide web, please send an email to
+ * licensing@ellislab.com so we can send you a copy immediately.
+ *
+ * @package CodeIgniter
+ * @author EllisLab Dev Team
+ * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
+ * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
+ * @link http://codeigniter.com
+ * @since Version 3.0
+ * @filesource
+ */
+defined('BASEPATH') OR exit('No direct script access allowed');
+
+/**
+ * CodeIgniter Redis Caching Class
+ *
+ * @package CodeIgniter
+ * @subpackage Libraries
+ * @category Core
+ * @author Anton Lindqvist <anton@qvister.se>
+ * @link
+ */
+class CI_Cache_redis extends CI_Driver
+{
+ /**
+ * Default config
+ *
+ * @static
+ * @var array
+ */
+ protected static $_default_config = array(
+ 'host' => '127.0.0.1',
+ 'password' => NULL,
+ 'port' => 6379,
+ 'timeout' => 0
+ );
+
+ /**
+ * Redis connection
+ *
+ * @var Redis
+ */
+ protected $_redis;
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get cache
+ *
+ * @param string Cache key identifier
+ * @return mixed
+ */
+ public function get($key)
+ {
+ return $this->_redis->get($key);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Save cache
+ *
+ * @param string Cache key identifier
+ * @param mixed Data to save
+ * @param int Time to live
+ * @return bool
+ */
+ public function save($key, $value, $ttl = NULL)
+ {
+ return ($ttl)
+ ? $this->_redis->setex($key, $ttl, $value)
+ : $this->_redis->set($key, $value);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Delete from cache
+ *
+ * @param string Cache key
+ * @return bool
+ */
+ public function delete($key)
+ {
+ return ($this->_redis->delete($key) === 1);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Clean cache
+ *
+ * @return bool
+ * @see Redis::flushDB()
+ */
+ public function clean()
+ {
+ return $this->_redis->flushDB();
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get cache driver info
+ *
+ * @param string Not supported in Redis.
+ * Only included in order to offer a
+ * consistent cache API.
+ * @return array
+ * @see Redis::info()
+ */
+ public function cache_info($type = NULL)
+ {
+ return $this->_redis->info();
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get cache metadata
+ *
+ * @param string Cache key
+ * @return array
+ */
+ public function get_metadata($key)
+ {
+ $value = $this->get($key);
+
+ if ($value)
+ {
+ return array(
+ 'expire' => time() + $this->_redis->ttl($key),
+ 'data' => $value
+ );
+ }
+
+ return FALSE;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Check if Redis driver is supported
+ *
+ * @return bool
+ */
+ public function is_supported()
+ {
+ if (extension_loaded('redis'))
+ {
+ $this->_setup_redis();
+ return TRUE;
+ }
+ else
+ {
+ log_message('error', 'The Redis extension must be loaded to use Redis cache.');
+ return FALSE;
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Setup Redis config and connection
+ *
+ * Loads Redis config file if present. Will halt execution
+ * if a Redis connection can't be established.
+ *
+ * @return bool
+ * @see Redis::connect()
+ */
+ protected function _setup_redis()
+ {
+ $config = array();
+ $CI =& get_instance();
+
+ if ($CI->config->load('redis', TRUE, TRUE))
+ {
+ $config += $CI->config->item('redis');
+ }
+
+ $config = array_merge(self::$_default_config, $config);
+
+ $this->_redis = new Redis();
+
+ try
+ {
+ $this->_redis->connect($config['host'], $config['port'], $config['timeout']);
+ }
+ catch (RedisException $e)
+ {
+ show_error('Redis connection refused. ' . $e->getMessage());
+ }
+
+ if (isset($config['password']))
+ {
+ $this->_redis->auth($config['password']);
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+
+ * Class destructor
+ *
+ * Closes the connection to Redis if present.
+ *
+ * @return void
+ */
+ public function __destruct()
+ {
+ if ($this->_redis)
+ {
+ $this->_redis->close();
+ }
+ }
+
+}
+
+/* End of file Cache_redis.php */
+/* Location: ./system/libraries/Cache/drivers/Cache_redis.php */ \ No newline at end of file
diff --git a/system/libraries/Cache/drivers/Cache_wincache.php b/system/libraries/Cache/drivers/Cache_wincache.php
index b32e66a46..383b3afdd 100644
--- a/system/libraries/Cache/drivers/Cache_wincache.php
+++ b/system/libraries/Cache/drivers/Cache_wincache.php
@@ -1,8 +1,8 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php
/**
* CodeIgniter
*
- * An open source application development framework for PHP 5.1.6 or newer
+ * An open source application development framework for PHP 5.2.4 or newer
*
* NOTICE OF LICENSE
*
@@ -18,12 +18,13 @@
*
* @package CodeIgniter
* @author EllisLab Dev Team
- * @copyright Copyright (c) 2006 - 2012 EllisLab, Inc.
+ * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
* @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* @link http://codeigniter.com
* @since Version 3.0
* @filesource
*/
+defined('BASEPATH') OR exit('No direct script access allowed');
/**
* CodeIgniter Wincache Caching Class
@@ -78,7 +79,7 @@ class CI_Cache_wincache extends CI_Driver {
* Delete from Cache
*
* @param mixed unique identifier of the item in the cache
- * @param bool true on success/false on failure
+ * @return bool true on success/false on failure
*/
public function delete($id)
{