Skip to content

Instantly share code, notes, and snippets.

@manadan999
Last active December 20, 2015 13:29
Show Gist options
  • Select an option

  • Save manadan999/6139193 to your computer and use it in GitHub Desktop.

Select an option

Save manadan999/6139193 to your computer and use it in GitHub Desktop.
This is the file which I am using. I am using MemcacheSASL as client.
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
require_once APPPATH.'libraries/MemcacheSASL.php';
//require_once('phpsqlparser/php-sql-parser.php');
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* Database Cache Class
*
* @category Database
* @author ExpressionEngine Dev Team
* @link http://codeigniter.com/user_guide/database/
*/
class CI_DB_Cache
{
var $CI;
var $db; // allows passing of db object so that multiple database connections and returned db objects can be supported
private $_memcached; // Holds the memcached object
// protected $_default_options = array(
// 'default_host' => $_ENV["MEMCACHIER_SERVERS"],
// 'default_port' => '11211',
// 'default_weight' => '1'
// );
// ------------------------------------------------------------------------
/**
* Constructor
*
* Grabs the CI super object instance so we can access it.
*
*/
function __construct(&$db)
{
// Assign the main CI object to $this->CI
// and load the file helper since we use it a lot
$this->CI = & get_instance();
$this->db = & $db;
$this->CI->load->helper('file');
}
function get_segment_number($segment_one, $segment_two)
{
if (($number = $this->_memcached->get($segment_one . '_' . $segment_two)) === FALSE)
{
$number = rand(1, 10000);
$this->_memcached->set($segment_one . '_' . $segment_two, $number, 100000);
$segment_number = $number;
}
else
{
$number = $this->_memcached->get($segment_one . '_' . $segment_two);
$segment_number = $number;
}
return $segment_number;
}
function get_memcache_key($sql_hash, $segment_number)
{
return $segment_number . '_' . $sql_hash;
}
// --------------------------------------------------------------------
private function _setup_memcached()
{
// Try to load memcached server info from the config file.
$CI = & get_instance();
if ($CI->config->load('memcached', TRUE, TRUE))
{
if (is_array($CI->config->config['memcached']))
{
$this->_memcache_conf = NULL;
foreach ($CI->config->config['memcached'] as $name => $conf)
{
$this->_memcache_conf[$name] = $conf;
}
}
}
$this->_memcached = new MemcacheSASL();
// foreach ($this->_memcache_conf as $name => $cache_server)
// {
// if (!array_key_exists('hostname', $cache_server))
// {
// $cache_server['hostname'] = $this->_default_options['default_host'];
// }
//
// if (!array_key_exists('port', $cache_server))
// {
// $cache_server['port'] = $this->_default_options['default_port'];
// }
//
// if (!array_key_exists('weight', $cache_server))
// {
// $cache_server['weight'] = $this->_default_options['default_weight'];
// }
//
// $this->_memcached->addServer(
// $cache_server['hostname'], $cache_server['port'], $cache_server['weight']
// );
// $this->_memcached->setSaslAuthData($_ENV["MEMCACHIER_USERNAME"], $_ENV["MEMCACHIER_PASSWORD"]);
// }
$this->_memcached->addServer($_ENV["MEMCACHIER_SERVERS"], '11211');
$this->_memcached->setSaslAuthData($_ENV["MEMCACHIER_USERNAME"], $_ENV["MEMCACHIER_PASSWORD"]);
}
// ------------------------------------------------------------------------
/**
* Check if Memcache is supported and do the setup if it supports.
*
* @access public
* @param string the path to the cache directory
* @return bool
*/
function memcache_available()
{
// if (!extension_loaded('memcache'))
// {
// log_message('error', 'The Memcache Extension must be loaded to use Memcached Cache.');
//
// return FALSE;
// }
$this->_setup_memcached();
return TRUE;
}
// --------------------------------------------------------------------
/**
* Retrieve a cached query
*
* The URI being requested will become the name of the cache sub-folder.
* An MD5 hash of the SQL statement will become the cache file name
*
* @access public
* @return string
*/
function read($sql)
{
if (!$this-> memcache_available())
{
return $this->db->cache_off();
}
$sql_hash = md5($sql);
$segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
$segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
$segment_number = $this->get_segment_number($segment_one, $segment_two);
$key = $this->get_memcache_key($sql_hash, $segment_number);
if (FALSE === ($cachedata = $this->_memcached->get($key)))
{
return FALSE;
}
return unserialize($cachedata);
}
// --------------------------------------------------------------------
/**
* Write a query to a cache file
*
* @access public
* @return bool
*/
function write($sql, $object)
{
if (!$this-> memcache_available())
{
return $this->db->cache_off();
}
$segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
$segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
$sql_hash = md5($sql);
$segment_number = $this->get_segment_number($segment_one, $segment_two);
$key = $this->get_memcache_key($sql_hash, $segment_number);
//$this->_memcached->set($key, serialize($object), FALSE, 180);
$this->_memcached->set($key, serialize($object), 180);
return TRUE;
}
// --------------------------------------------------------------------
/**
* Delete cache files within a particular directory
* @param string $table_name Name of the table
* @param string $sql SQL query
* @access public
* @return bool
*/
function delete($segment_one = '', $segment_two = '')
{
if (!$this-> memcache_available())
{
return $this->db->cache_off();
}
if ($segment_one == '')
{
$segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
}
if ($segment_two == '')
{
$segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
}
if ($this->_memcached->increment($segment_one . '_' . $segment_two))
{
return TRUE;
}
else
{
return FALSE;
}
}
// --------------------------------------------------------------------
/**
* Delete all existing cache files
*
* @access public
* @return bool
*/
function delete_all()
{
if (!$this-> memcache_available())
{
return $this->db->cache_off();
}
$this->_memcached->flush();
}
}
/* End of file DB_cache.php */
/* Location: ./system/database/DB_cache.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment