Allow overriding lifetime on a per-object basis
[gitphp.git] / include / cache / Cache.class.php
blob:a/include/cache/Cache.class.php -> blob:b/include/cache/Cache.class.php
<?php <?php
/** /**
* GitPHP Cache * GitPHP Cache
* *
* Class to store arbitrary data objects in smarty cache * Class to store arbitrary data objects in smarty cache
* *
* @author Christopher Han <xiphux@gmail.com> * @author Christopher Han <xiphux@gmail.com>
* @copyright Copyright (c) 2010 Christopher Han * @copyright Copyright (c) 2010 Christopher Han
* @package GitPHP * @package GitPHP
* @subpackage Cache * @subpackage Cache
*/ */
   
/** /**
* Cache class * Cache class
* *
* @package GitPHP * @package GitPHP
* @subpackage Cache * @subpackage Cache
*/ */
class GitPHP_Cache class GitPHP_Cache
{ {
/** /**
* Template * Template
* *
* Cache template * Cache template
*/ */
const Template = 'data.tpl'; const Template = 'data.tpl';
   
/** /**
* objectCacheInstance * objectCacheInstance
* *
* Stores the singleton instance of the object cache * Stores the singleton instance of the object cache
* *
* @access protected * @access protected
* @static * @static
*/ */
protected static $objectCacheInstance; protected static $objectCacheInstance;
   
/** /**
* GetObjectCacheInstance * GetObjectCacheInstance
* *
* Return the singleton instance of the object cache * Return the singleton instance of the object cache
* *
* @access public * @access public
* @static * @static
* @return mixed instance of cache class * @return mixed instance of cache class
*/ */
public static function GetObjectCacheInstance() public static function GetObjectCacheInstance()
{ {
if (!self::$objectCacheInstance) { if (!self::$objectCacheInstance) {
self::$objectCacheInstance = new GitPHP_Cache(); self::$objectCacheInstance = new GitPHP_Cache();
if (GitPHP_Config::GetInstance()->GetValue('objectcache', false)) { if (GitPHP_Config::GetInstance()->GetValue('objectcache', false)) {
self::$objectCacheInstance->SetEnabled(true); self::$objectCacheInstance->SetEnabled(true);
self::$objectCacheInstance->SetLifetime(GitPHP_Config::GetInstance()->GetValue('objectcachelifetime', 86400)); self::$objectCacheInstance->SetLifetime(GitPHP_Config::GetInstance()->GetValue('objectcachelifetime', 86400));
} }
} }
return self::$objectCacheInstance; return self::$objectCacheInstance;
} }
   
/** /**
* tpl * tpl
* *
* Smarty instance * Smarty instance
* *
* @access protected * @access protected
*/ */
protected $tpl = null; protected $tpl = null;
   
/** /**
* enabled * enabled
* *
* Stores whether the cache is enabled * Stores whether the cache is enabled
* *
* @access protected * @access protected
*/ */
protected $enabled = false; protected $enabled = false;
   
/** /**
* __construct * __construct
* *
* Constructor * Constructor
* *
* @access public * @access public
* @return mixed cache object * @return mixed cache object
*/ */
public function __construct() public function __construct()
{ {
} }
   
/** /**
* GetEnabled * GetEnabled
* *
* Gets whether the cache is enabled * Gets whether the cache is enabled
* *
* @access public * @access public
* @return boolean true if enabled * @return boolean true if enabled
*/ */
public function GetEnabled() public function GetEnabled()
{ {
return $this->enabled; return $this->enabled;
} }
   
/** /**
* SetEnabled * SetEnabled
* *
* Sets whether the cache is enabled * Sets whether the cache is enabled
* *
* @access public * @access public
* @param boolean $enable true to enable, false to disable * @param boolean $enable true to enable, false to disable
*/ */
public function SetEnabled($enable) public function SetEnabled($enable)
{ {
if ($enable == $this->enabled) if ($enable == $this->enabled)
return; return;
   
$this->enabled = $enable; $this->enabled = $enable;
   
if ($this->enabled) if ($this->enabled)
$this->CreateSmarty(); $this->CreateSmarty();
else else
$this->DestroySmarty(); $this->DestroySmarty();
} }
   
/** /**
* GetLifetime * GetLifetime
* *
* Gets the cache lifetime * Gets the cache lifetime
* *
* @access public * @access public
* @return int cache lifetime in seconds * @return int cache lifetime in seconds
*/ */
public function GetLifetime() public function GetLifetime()
{ {
if (!$this->enabled) if (!$this->enabled)
return false; return false;
   
return $this->tpl->cache_lifetime; return $this->tpl->cache_lifetime;
} }
   
/** /**
* SetLifetime * SetLifetime
* *
* Sets the cache lifetime * Sets the cache lifetime
* *
* @access public * @access public
* @param int $lifetime cache lifetime in seconds * @param int $lifetime cache lifetime in seconds
*/ */
public function SetLifetime($lifetime) public function SetLifetime($lifetime)
{ {
if (!$this->enabled) if (!$this->enabled)
return; return;
   
$this->tpl->cache_lifetime = $lifetime; $this->tpl->cache_lifetime = $lifetime;
} }
   
/** /**
* Get * Get
* *
* Get an item from the cache * Get an item from the cache
* *
* @access public * @access public
* @param string $key cache key * @param string $key cache key
* @return the cached object, or false * @return the cached object, or false
*/ */
public function Get($key = null) public function Get($key = null)
{ {
if (empty($key)) if (empty($key))
return false; return false;
   
if (!$this->enabled) if (!$this->enabled)
return false; return false;
   
if (!$this->tpl->is_cached(GitPHP_Cache::Template, $key)) if (!$this->tpl->is_cached(GitPHP_Cache::Template, $key))
return false; return false;
   
$data = $this->tpl->fetch(GitPHP_Cache::Template, $key); $data = $this->tpl->fetch(GitPHP_Cache::Template, $key);
   
return unserialize($data); return unserialize($data);
} }
   
/** /**
* Set * Set
* *
* Set an item in the cache * Set an item in the cache
* *
* @access public * @access public
* @param string $key cache key * @param string $key cache key
* @param mixed $value value * @param mixed $value value
*/ * @param int $lifetime override the lifetime for this data
public function Set($key = null, $value = null) */
  public function Set($key = null, $value = null, $lifetime = null)
{ {
if (empty($key) || empty($value)) if (empty($key) || empty($value))
return; return;
   
if (!$this->enabled) if (!$this->enabled)
return; return;
   
  $oldLifetime = null;
  if ($lifetime !== null) {
  $oldLifetime = $this->tpl->cache_lifetime;
  $this->tpl->cache_lifetime = $lifetime;
  }
   
$this->Delete($key); $this->Delete($key);
$this->tpl->clear_all_assign(); $this->tpl->clear_all_assign();
$this->tpl->assign('data', serialize($value)); $this->tpl->assign('data', serialize($value));
   
// Force it into smarty's cache // Force it into smarty's cache
$tmp = $this->tpl->fetch(GitPHP_Cache::Template, $key); $tmp = $this->tpl->fetch(GitPHP_Cache::Template, $key);
unset($tmp); unset($tmp);
   
  if ($lifetime !== null) {
  $this->tpl->cache_lifetime = $oldLifetime;
  }
} }
   
/** /**
* Exists * Exists
* *
* Tests if a key is cached * Tests if a key is cached
* *
* @access public * @access public
* @param string $key cache key * @param string $key cache key
* @return boolean true if cached, false otherwise * @return boolean true if cached, false otherwise
*/ */
public function Exists($key = null) public function Exists($key = null)
{ {
if (empty($key)) if (empty($key))
return false; return false;
   
if (!$this->enabled) if (!$this->enabled)
return false; return false;
   
return $this->tpl->is_cached(GitPHP_Cache::Template, $key); return $this->tpl->is_cached(GitPHP_Cache::Template, $key);
} }
   
/** /**
* Delete * Delete
* *
* Delete an item from the cache * Delete an item from the cache
* *
* @access public * @access public
* @param string $key cache key * @param string $key cache key
*/ */
public function Delete($key = null) public function Delete($key = null)
{ {
if (empty($key)) if (empty($key))
return; return;
   
if (!$this->enabled) if (!$this->enabled)
return; return;
   
$this->tpl->clear_cache(GitPHP_Cache::Template, $key); $this->tpl->clear_cache(GitPHP_Cache::Template, $key);
} }
   
/** /**
* Clear * Clear
* *
* Clear the cache * Clear the cache
* *
* @access public * @access public
*/ */
public function Clear() public function Clear()
{ {
if (!$this->enabled) if (!$this->enabled)
return; return;
   
$this->tpl->clear_all_cache(); $this->tpl->clear_all_cache();
} }
   
/** /**
* CreateSmarty * CreateSmarty
* *
* Instantiates Smarty cache * Instantiates Smarty cache
* *
* @access private * @access private
*/ */
private function CreateSmarty() private function CreateSmarty()
{ {
if ($this->tpl) if ($this->tpl)
return; return;
   
$this->tpl = new Smarty; $this->tpl = new Smarty;
   
$this->tpl->caching = 2; $this->tpl->caching = 2;
   
$servers = GitPHP_Config::GetInstance()->GetValue('memcache', null); $servers = GitPHP_Config::GetInstance()->GetValue('memcache', null);
if (isset($servers) && is_array($servers) && (count($servers) > 0)) { if (isset($servers) && is_array($servers) && (count($servers) > 0)) {
require_once(GITPHP_CACHEDIR . 'Memcache.class.php'); require_once(GITPHP_CACHEDIR . 'Memcache.class.php');
GitPHP_Memcache::GetInstance()->AddServers($servers); GitPHP_Memcache::GetInstance()->AddServers($servers);
require_once(GITPHP_CACHEDIR . 'memcache_cache_handler.php'); require_once(GITPHP_CACHEDIR . 'memcache_cache_handler.php');
$this->tpl->cache_handler_func = 'memcache_cache_handler'; $this->tpl->cache_handler_func = 'memcache_cache_handler';
} }
   
} }
   
/** /**
* DestroySmarty * DestroySmarty
* *
* Destroys Smarty cache * Destroys Smarty cache
* *
* @access private * @access private
*/ */
private function DestroySmarty() private function DestroySmarty()
{ {
if (!$this->tpl) if (!$this->tpl)
return; return;
   
$this->tpl = null; $this->tpl = null;
} }
   
} }
   
comments