Created
February 26, 2013 14:53
-
-
Save betweenbrain/5038980 to your computer and use it in GitHub Desktop.
JCacheStorageDb for storing cache files in database
from http://forum.joomla.org/viewtopic.php?p=1380543
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Check to ensure this file is within the rest of the framework | |
defined('JPATH_BASE') or die(); | |
/** | |
* DB cache storage handler | |
* | |
* @author Geraint Edwards (http://www.gwesystems.com) | |
* @package Joomla.Framework | |
* @subpackage Cache | |
* @since 1.5 | |
*/ | |
class JCacheStorageDb extends JCacheStorage | |
{ | |
var $cleanedExpired = false; | |
/** | |
* Constructor | |
* | |
* @access protected | |
* @param array $options optional parameters | |
*/ | |
function __construct( $options = array() ) | |
{ | |
parent::__construct($options); | |
if (!$this->cleanedExpired) $this->gc(); | |
} | |
/** | |
* One time only DB setup function | |
* | |
*/ | |
function setupDB() { | |
$db =& JFactory::getDBO(); | |
$sql = "CREATE TABLE IF NOT EXISTS `#__dbcache` (" | |
. "\n `id` varchar ( 32 ) NOT NULL default ''," | |
. "\n `groupname` varchar ( 32 ) NOT NULL default ''," | |
. "\n `expire` datetime NOT NULL default '0000-00-00 00:00:00'," | |
. "\n `value` TEXT NOT NULL default ''," | |
. "\n PRIMARY KEY ( `id`,`groupname` )," | |
. "\n KEY ( `expire`,`groupname` )" | |
. "\n ) CHARACTER SET utf8 COLLATE utf8_general_ci"; | |
$db->setQuery( $sql ); | |
if (!$db->query()){ | |
echo $db->getErrorMsg()."<br/>"; | |
echo $db->_sql; | |
} | |
} | |
/** | |
* Get cached data from a db by id and group | |
* | |
* @access public | |
* @param string $id The cache data id | |
* @param string $group The cache data group | |
* @param boolean $checkTime True to verify cache time expiration threshold | |
* @return mixed Boolean false on failure or a cached data string | |
* @since 1.5 | |
*/ | |
function get($id, $group, $checkTime) | |
{ | |
$data = false; | |
$data = base64_encode($data); | |
$db =& JFactory::getDBO(); | |
$sql = "SELECT value FROM `#__dbcache` WHERE id=".$db->quote($id)." AND groupname=".$db->quote($group)." AND expire>FROM_UNIXTIME(".$db->quote($this->_now).")"; | |
$db->setQuery($sql); | |
// Must set false to ensure that Joomfish doesn't get involved | |
$data = $db->loadResult(false); | |
if (is_null($data)){ | |
$data = false; | |
} | |
else { | |
$data = base64_decode($data); | |
} | |
return $data; | |
} | |
/** | |
* Store the data to a file by id and group | |
* | |
* @access public | |
* @param string $id The cache data id | |
* @param string $group The cache data group | |
* @param string $data The data to store in cache | |
* @return boolean True on success, false otherwise | |
* @since 1.5 | |
*/ | |
function store($id, $group, $data) | |
{ | |
$data = base64_encode($data); | |
$db =& JFactory::getDBO(); | |
$sql = "REPLACE INTO `#__dbcache` (id, groupname,expire,value) VALUES (".$db->quote($id).",".$db->quote($group).",FROM_UNIXTIME(".$db->quote($this->_now + $this->_lifetime)."),".$db->quote($data).")"; | |
$db->setQuery($sql); | |
return $db->query(); | |
} | |
/** | |
* Remove a cached data file by id and group | |
* | |
* @access public | |
* @param string $id The cache data id | |
* @param string $group The cache data group | |
* @return boolean True on success, false otherwise | |
* @since 1.5 | |
*/ | |
function remove($id, $group) | |
{ | |
$db =& JFactory::getDBO(); | |
$sql = "DELETE FROM `#__dbcache` WHERE id=".$db->quote($id)." AND groupname=".$db->quote($group); | |
$db->setQuery($sql); | |
return $db->query(); | |
} | |
/** | |
* Clean cache for a group given a mode. | |
* | |
* group mode : cleans all cache in the group | |
* notgroup mode : cleans all cache not in the group | |
* | |
* @access public | |
* @param string $group The cache data group | |
* @param string $mode The mode for cleaning cache [group|notgroup] | |
* @return boolean True on success, false otherwise | |
* @since 1.5 | |
*/ | |
function clean($group, $mode) | |
{ | |
switch (trim($mode)) | |
{ | |
case 'group': | |
$db =& JFactory::getDBO(); | |
$sql = "DELETE FROM `#__dbcache` "; | |
$db->setQuery($sql); | |
return $db->query(); | |
break; | |
default: | |
$db =& JFactory::getDBO(); | |
$sql = "DELETE FROM `#__dbcache` WHERE groupname=".$db->quote($group); | |
$db->setQuery($sql); | |
return $db->query(); | |
break; | |
} | |
return $return; | |
} | |
/** | |
* Garbage collect expired cache data | |
* | |
* @access public | |
* @return boolean True on success, false otherwise. | |
*/ | |
function gc() | |
{ | |
$db =& JFactory::getDBO(); | |
$sql = "DELETE FROM `#__dbcache` WHERE expire< FROM_UNIXTIME(".$db->quote($this->_now).")"; | |
$db->setQuery($sql); | |
$result = $db->query(); | |
// if we can't delete then the database table probably doesn't exist so create it | |
if (!$result && $db->getErrorNum()==1146){ | |
$this->setupDB(); | |
} | |
} | |
/** | |
* Test to see if the cache storage is available. | |
* | |
* @static | |
* @access public | |
* @return boolean True on success, false otherwise. | |
*/ | |
function test() | |
{ | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment