Merge pull request #1 from joshtronic/cache-n-carry

Cache class
This commit is contained in:
Josh Sherman 2011-11-01 16:08:10 -07:00
commit 50c71201ca
8 changed files with 642 additions and 33 deletions

211
classes/Cache.php Normal file
View file

@ -0,0 +1,211 @@
<?php
/**
* Caching System for PICKLES
*
* PHP version 5
*
* Licensed under The MIT License
* Redistribution of these files must retain the above copyright notice.
*
* @author Josh Sherman <josh@gravityblvd.com>
* @copyright Copyright 2007-2011, Josh Sherman
* @license http://www.opensource.org/licenses/mit-license.html
* @package PICKLES
* @link http://p.ickl.es
*/
/**
* Cache Class
*
* Wrapper class for Memcache() to allow for better error handling when the
* Memcached server is unavailable. Designed around the syntax for Memcached()
* to allow for an easier transistion to the aforementioned in the future. I
* don't entirely remember specifics, but the reason for not using Memcached()
* was due to an unexplainable bug in the version in the repository for Ubuntu
* 10.04 LTS. Memcached() does support more of the memcached protocol and will
* eventually be what PICKLES uses.
*
* Requires php5-memcache
*
* @link http://us.php.net/manual/en/book.memcache.php
* @link http://packages.ubuntu.com/lucid/php5-memcache
* @link http://www.memcached.org/
*/
class Cache extends Object
{
/**
* Hostname for the Memcached Server
*
* @access private
* @var string
*/
private $hostname = null;
/**
* Port to use to connect
*
* @access private
* @var integer
*/
private $port = null;
/**
* Connection resource to Memcached
*
* @access private
* @var object
*/
private $connection = null;
/**
* Constructor
*
* Sets up our connection variables.
*
* @param string $hostname optional hostname to connect to
* @param string $database optional port to use
*/
public function __construct($hostname = null, $port = null)
{
parent::__construct();
if ($this->config->pickles['cache'])
{
if (isset($this->config->datasources[$this->config->pickles['cache']]))
{
$datasource = $this->config->datasources[$this->config->pickles['cache']];
if (isset($datasource['hostname'], $datasource['port']))
{
$this->hostname = $datasource['hostname'];
$this->port = $datasource['port'];
}
}
}
}
/**
* Destructor
*
* Closes the connection when the object dies.
*/
public function __destruct()
{
if ($this->connection)
{
$this->connection->close();
}
}
/**
* Get Instance
*
* Let's the parent class do all the work.
*
* @static
* @param string $class name of the class to instantiate
* @return object self::$instance instance of the Cache class
*/
public static function getInstance($class = 'Cache')
{
return parent::getInstance($class);
}
/**
* Opens Connection
*
* Establishes a connection to the memcached server.
*/
public function open()
{
if ($this->connection === null)
{
$this->connection = new Memcache();
$this->connection->connect($this->hostname, $this->port);
}
return true;
}
/**
* Get Key
*
* Gets the value of the key and returns it.
*
* @param string $key key to retrieve
* @return mixed value of the requested key, false if not set
*/
public function get($key)
{
if ($this->open())
{
return $this->connection->get($key);
}
return false;
}
/**
* Set Key
*
* Sets key to the specified value. I've found that compression can lead to
* issues with integers and can slow down the storage and retrieval of data
* (defeats the purpose of caching if you ask me) and isn't supported. I've
* also been burned by data inadvertantly being cached for infinity, hence
* the 5 minute default.
*
* @param string $key key to set
* @param mixed $value value to set
* @param integer $expiration optional expiration, defaults to 5 minutes
* @return boolean status of writing the data to the key
*/
public function set($key, $value, $expire = 300)
{
if ($this->open())
{
return $this->connection->set($key, $value, 0, $expire);
}
return false;
}
/**
* Delete Key
*
* Deletes the specified key.
*
* @param string $key key to delete
* @return boolean status of deleting the key
*/
public function delete($key)
{
if ($this->open())
{
return $this->connection->delete($key);
}
return false;
}
/**
* Increment Key
*
* Increments the value of an existing key.
*
* @param string $key key to increment
* @return boolean status of incrementing the key
* @todo Wondering if I should check the key and set to 1 if it's new
*/
public function increment($key)
{
if ($this->open())
{
return $this->connection->increment($key);
}
return false;
}
}
?>

View file

@ -183,10 +183,13 @@ class Config extends Object
$this->data['pickles']['profiler'] = false;
}
// Defaults logging to false if it doesn't exist
if (!isset($this->data['pickles']['logging']))
// Defaults expected PICKLES options to false
foreach (array('cache', 'logging') as $variable)
{
$this->data['pickles']['logging'] = false;
if (!isset($this->data['pickles'][$variable]))
{
$this->data['pickles'][$variable] = false;
}
}
// Creates constants for the security levels

View file

@ -9,7 +9,7 @@
* Redistribution of these files must retain the above copyright notice.
*
* @author Josh Sherman <josh@gravityblvd.com>
* @copyright Copyright 2007-2011, Josh Sherman
* @copyright Copyright 2007-2011, Josh Sherman
* @license http://www.opensource.org/licenses/mit-license.html
* @package PICKLES
* @link http://p.ickl.es
@ -129,6 +129,11 @@ class Database extends Object
{
$instance->setDatabase($datasource['database']);
}
if (isset($datasource['cache']))
{
$instance->setCache($datasource['cache']);
}
}
// Caches the instance for possible reuse later

View file

@ -29,7 +29,7 @@ abstract class Database_Common extends Object
* @access protected
* @var string
*/
protected $driver;
protected $driver = null;
/**
* Hostname for the server
@ -79,6 +79,14 @@ abstract class Database_Common extends Object
*/
protected $database = null;
/**
* Whether or not to use caching
*
* @access protected
* @var boolean
*/
protected $cache = false;
/**
* Connection resource
*
@ -176,6 +184,16 @@ abstract class Database_Common extends Object
return $this->database = $database;
}
/**
* Set Cache
*
* @param boolean whether or not to use cache
*/
public function setCache($cache)
{
return $this->cache = $cache;
}
/**
* Get Driver
*
@ -189,6 +207,18 @@ abstract class Database_Common extends Object
return $this->driver;
}
/**
* Get Cache
*
* Returns the status of caching for this datasource.
*
* @return string whether or not to use the cache
*/
public function getCache()
{
return $this->cache;
}
/**
* Opens database connection
*

View file

@ -34,6 +34,22 @@ class Model extends Object
*/
protected $db = null;
/**
* Cache Object
*
* @access protected
* @var object
*/
protected $cache = null;
/**
* Whether or not to use cache
*
* @access protected
* @var boolean
*/
protected $use_cache = false;
/**
* SQL Array
*
@ -240,8 +256,15 @@ class Model extends Object
// Runs the parent constructor so we have the config
parent::__construct();
// Gets an instance of the database
$this->db = Database::getInstance($this->datasource != '' ? $this->datasource : null);
// Gets an instance of the cache and database
// @todo Datasource has no way of being set
$this->db = Database::getInstance($this->datasource != '' ? $this->datasource : null);
$this->caching = $this->db->getCache();
if ($this->caching)
{
$this->cache = Cache::getInstance();
}
// Builds out the query
if ($type_or_parameters != null)
@ -263,6 +286,7 @@ class Model extends Object
elseif (ctype_digit((string)$type_or_parameters))
{
$this->loadParameters(array($this->id => $type_or_parameters));
$cache_key = 'PICKLES-' . $this->datasource . '-' . $this->table . '-' . $type_or_parameters;
}
elseif (ctype_digit((string)$parameters))
{
@ -301,7 +325,26 @@ class Model extends Object
break;
}
$this->records = $this->db->fetch(implode(' ', $this->sql), (count($this->input_parameters) == 0 ? null : $this->input_parameters));
$query_database = true;
if (isset($cache_key))
{
//$cached = $this->cache->get($cache_key);
}
if (isset($cached) && $cached)
{
$this->records = $cached;
}
else
{
$this->records = $this->db->fetch(implode(' ', $this->sql), (count($this->input_parameters) == 0 ? null : $this->input_parameters));
if (isset($cache_key))
{
//$this->cache->set($cache_key, $this->records);
}
}
}
else
{
@ -910,6 +953,11 @@ class Model extends Object
{
$sql .= ' WHERE ' . $this->id . ' = :' . $this->id . ' LIMIT 1;';
$input_parameters[':' . $this->id] = $this->record[$this->id];
if ($this->caching)
{
//$this->cache->delete('PICKLES-' . $this->datasource . '-' . $this->table . '-' . $this->record[$this->id]);
}
}
// Executes the query

View file

@ -28,7 +28,15 @@
class Module extends Object
{
/**
* Database object
* Cache Object
*
* @access protected
* @var object
*/
protected $cache = null;
/**
* Database Object
*
* @access protected
* @var object
@ -36,7 +44,7 @@ class Module extends Object
protected $db = null;
/**
* Page title
* Page Title
*
* @access protected
* @var string, null by default
@ -44,7 +52,7 @@ class Module extends Object
protected $title = null;
/**
* Meta description
* Meta Description
*
* @access protected
* @var string, null by default
@ -52,7 +60,7 @@ class Module extends Object
protected $description = null;
/**
* Meta keywords (comma separated)
* Meta Keywords (comma separated)
*
* @access protected
* @var string, null by default
@ -80,7 +88,7 @@ class Module extends Object
protected $private = false;
/**
* Security settings of the page
* Security Settings
*
* @access protected
* @var boolean, null by default
@ -120,7 +128,7 @@ class Module extends Object
protected $hash = null;
/**
* Default display engine
* Default Display Engine
*
* Defaults to PHP but could be set to JSON, XML or RSS. Value is
* overwritten by the config value if not set by the module.
@ -131,7 +139,7 @@ class Module extends Object
protected $engine = DISPLAY_PHP;
/**
* Default template
* Default Template
*
* Defaults to null but could be set to any valid template basename. The
* value is overwritten by the config value if not set by the module. The
@ -156,7 +164,8 @@ class Module extends Object
{
parent::__construct();
$this->db = Database::getInstance();
$this->cache = Cache::getInstance();
$this->db = Database::getInstance();
if ($autorun === true)
{

335
jar.php
View file

@ -266,6 +266,214 @@ class API_Tinychat extends API_Common
}
}
/**
* Caching System for PICKLES
*
* PHP version 5
*
* Licensed under The MIT License
* Redistribution of these files must retain the above copyright notice.
*
* @author Josh Sherman <josh@gravityblvd.com>
* @copyright Copyright 2007-2011, Josh Sherman
* @license http://www.opensource.org/licenses/mit-license.html
* @package PICKLES
* @link http://p.ickl.es
*/
/**
* Cache Class
*
* Wrapper class for Memcache() to allow for better error handling when the
* Memcached server is unavailable. Designed around the syntax for Memcached()
* to allow for an easier transistion to the aforementioned in the future. I
* don't entirely remember specifics, but the reason for not using Memcached()
* was due to an unexplainable bug in the version in the repository for Ubuntu
* 10.04 LTS. Memcached() does support more of the memcached protocol and will
* eventually be what PICKLES uses.
*
* Requires php5-memcache
*
* @link http://us.php.net/manual/en/book.memcache.php
* @link http://packages.ubuntu.com/lucid/php5-memcache
* @link http://www.memcached.org/
*/
class Cache extends Object
{
/**
* Hostname for the Memcached Server
*
* @access private
* @var string
*/
private $hostname = null;
/**
* Port to use to connect
*
* @access private
* @var integer
*/
private $port = null;
/**
* Connection resource to Memcached
*
* @access private
* @var object
*/
private $connection = null;
/**
* Constructor
*
* Sets up our connection variables.
*
* @param string $hostname optional hostname to connect to
* @param string $database optional port to use
*/
public function __construct($hostname = null, $port = null)
{
parent::__construct();
if ($this->config->pickles['cache'])
{
if (isset($this->config->datasources[$this->config->pickles['cache']]))
{
$datasource = $this->config->datasources[$this->config->pickles['cache']];
if (isset($datasource['hostname'], $datasource['port']))
{
$this->hostname = $datasource['hostname'];
$this->port = $datasource['port'];
}
}
}
}
/**
* Destructor
*
* Closes the connection when the object dies.
*/
public function __destruct()
{
if ($this->connection)
{
$this->connection->close();
}
}
/**
* Get Instance
*
* Let's the parent class do all the work.
*
* @static
* @param string $class name of the class to instantiate
* @return object self::$instance instance of the Cache class
*/
public static function getInstance($class = 'Cache')
{
return parent::getInstance($class);
}
/**
* Opens Connection
*
* Establishes a connection to the memcached server.
*/
public function open()
{
if ($this->connection === null)
{
$this->connection = new Memcache();
$this->connection->connect($this->hostname, $this->port);
}
return true;
}
/**
* Get Key
*
* Gets the value of the key and returns it.
*
* @param string $key key to retrieve
* @return mixed value of the requested key, false if not set
*/
public function get($key)
{
if ($this->open())
{
return $this->connection->get($key);
}
return false;
}
/**
* Set Key
*
* Sets key to the specified value. I've found that compression can lead to
* issues with integers and can slow down the storage and retrieval of data
* (defeats the purpose of caching if you ask me) and isn't supported. I've
* also been burned by data inadvertantly being cached for infinity, hence
* the 5 minute default.
*
* @param string $key key to set
* @param mixed $value value to set
* @param integer $expiration optional expiration, defaults to 5 minutes
* @return boolean status of writing the data to the key
*/
public function set($key, $value, $expire = 300)
{
if ($this->open())
{
return $this->connection->set($key, $value, 0, $expire);
}
return false;
}
/**
* Delete Key
*
* Deletes the specified key.
*
* @param string $key key to delete
* @return boolean status of deleting the key
*/
public function delete($key)
{
if ($this->open())
{
return $this->connection->delete($key);
}
return false;
}
/**
* Increment Key
*
* Increments the value of an existing key.
*
* @param string $key key to increment
* @return boolean status of incrementing the key
* @todo Wondering if I should check the key and set to 1 if it's new
*/
public function increment($key)
{
if ($this->open())
{
return $this->connection->increment($key);
}
return false;
}
}
/**
* Configuration Class File for PICKLES
*
@ -449,10 +657,13 @@ class Config extends Object
$this->data['pickles']['profiler'] = false;
}
// Defaults logging to false if it doesn't exist
if (!isset($this->data['pickles']['logging']))
// Defaults expected PICKLES options to false
foreach (array('cache', 'logging') as $variable)
{
$this->data['pickles']['logging'] = false;
if (!isset($this->data['pickles'][$variable]))
{
$this->data['pickles'][$variable] = false;
}
}
// Creates constants for the security levels
@ -1186,7 +1397,7 @@ abstract class Database_Common extends Object
* @access protected
* @var string
*/
protected $driver;
protected $driver = null;
/**
* Hostname for the server
@ -1236,6 +1447,14 @@ abstract class Database_Common extends Object
*/
protected $database = null;
/**
* Whether or not to use caching
*
* @access protected
* @var boolean
*/
protected $cache = false;
/**
* Connection resource
*
@ -1333,6 +1552,16 @@ abstract class Database_Common extends Object
return $this->database = $database;
}
/**
* Set Cache
*
* @param boolean whether or not to use cache
*/
public function setCache($cache)
{
return $this->cache = $cache;
}
/**
* Get Driver
*
@ -1346,6 +1575,18 @@ abstract class Database_Common extends Object
return $this->driver;
}
/**
* Get Cache
*
* Returns the status of caching for this datasource.
*
* @return string whether or not to use the cache
*/
public function getCache()
{
return $this->cache;
}
/**
* Opens database connection
*
@ -1855,7 +2096,7 @@ class Database_PDO_SQLite extends Database_PDO_Common
* Redistribution of these files must retain the above copyright notice.
*
* @author Josh Sherman <josh@gravityblvd.com>
* @copyright Copyright 2007-2011, Josh Sherman
* @copyright Copyright 2007-2011, Josh Sherman
* @license http://www.opensource.org/licenses/mit-license.html
* @package PICKLES
* @link http://p.ickl.es
@ -1975,6 +2216,11 @@ class Database extends Object
{
$instance->setDatabase($datasource['database']);
}
if (isset($datasource['cache']))
{
$instance->setCache($datasource['cache']);
}
}
// Caches the instance for possible reuse later
@ -3817,6 +4063,22 @@ class Model extends Object
*/
protected $db = null;
/**
* Cache Object
*
* @access protected
* @var object
*/
protected $cache = null;
/**
* Whether or not to use cache
*
* @access protected
* @var boolean
*/
protected $use_cache = false;
/**
* SQL Array
*
@ -4023,8 +4285,15 @@ class Model extends Object
// Runs the parent constructor so we have the config
parent::__construct();
// Gets an instance of the database
$this->db = Database::getInstance($this->datasource != '' ? $this->datasource : null);
// Gets an instance of the cache and database
// @todo Datasource has no way of being set
$this->db = Database::getInstance($this->datasource != '' ? $this->datasource : null);
$this->caching = $this->db->getCache();
if ($this->caching)
{
$this->cache = Cache::getInstance();
}
// Builds out the query
if ($type_or_parameters != null)
@ -4046,6 +4315,7 @@ class Model extends Object
elseif (ctype_digit((string)$type_or_parameters))
{
$this->loadParameters(array($this->id => $type_or_parameters));
$cache_key = 'PICKLES-' . $this->datasource . '-' . $this->table . '-' . $type_or_parameters;
}
elseif (ctype_digit((string)$parameters))
{
@ -4084,7 +4354,26 @@ class Model extends Object
break;
}
$this->records = $this->db->fetch(implode(' ', $this->sql), (count($this->input_parameters) == 0 ? null : $this->input_parameters));
$query_database = true;
if (isset($cache_key))
{
//$cached = $this->cache->get($cache_key);
}
if (isset($cached) && $cached)
{
$this->records = $cached;
}
else
{
$this->records = $this->db->fetch(implode(' ', $this->sql), (count($this->input_parameters) == 0 ? null : $this->input_parameters));
if (isset($cache_key))
{
//$this->cache->set($cache_key, $this->records);
}
}
}
else
{
@ -4693,6 +4982,11 @@ class Model extends Object
{
$sql .= ' WHERE ' . $this->id . ' = :' . $this->id . ' LIMIT 1;';
$input_parameters[':' . $this->id] = $this->record[$this->id];
if ($this->caching)
{
//$this->cache->delete('PICKLES-' . $this->datasource . '-' . $this->table . '-' . $this->record[$this->id]);
}
}
// Executes the query
@ -4812,7 +5106,15 @@ class Model extends Object
class Module extends Object
{
/**
* Database object
* Cache Object
*
* @access protected
* @var object
*/
protected $cache = null;
/**
* Database Object
*
* @access protected
* @var object
@ -4820,7 +5122,7 @@ class Module extends Object
protected $db = null;
/**
* Page title
* Page Title
*
* @access protected
* @var string, null by default
@ -4828,7 +5130,7 @@ class Module extends Object
protected $title = null;
/**
* Meta description
* Meta Description
*
* @access protected
* @var string, null by default
@ -4836,7 +5138,7 @@ class Module extends Object
protected $description = null;
/**
* Meta keywords (comma separated)
* Meta Keywords (comma separated)
*
* @access protected
* @var string, null by default
@ -4864,7 +5166,7 @@ class Module extends Object
protected $private = false;
/**
* Security settings of the page
* Security Settings
*
* @access protected
* @var boolean, null by default
@ -4904,7 +5206,7 @@ class Module extends Object
protected $hash = null;
/**
* Default display engine
* Default Display Engine
*
* Defaults to PHP but could be set to JSON, XML or RSS. Value is
* overwritten by the config value if not set by the module.
@ -4915,7 +5217,7 @@ class Module extends Object
protected $engine = DISPLAY_PHP;
/**
* Default template
* Default Template
*
* Defaults to null but could be set to any valid template basename. The
* value is overwritten by the config value if not set by the module. The
@ -4940,7 +5242,8 @@ class Module extends Object
{
parent::__construct();
$this->db = Database::getInstance();
$this->cache = Cache::getInstance();
$this->db = Database::getInstance();
if ($autorun === true)
{

View file

@ -91,7 +91,7 @@ if (is_writable(PICKLES_PATH))
if (file_exists($jar_file))
{
require $jar_file;
// require $jar_file;
}
// }}}