Skip to content

Instantly share code, notes, and snippets.

@nerdsrescueme
Created October 20, 2011 20:05
Show Gist options
  • Save nerdsrescueme/1302179 to your computer and use it in GitHub Desktop.
Save nerdsrescueme/1302179 to your computer and use it in GitHub Desktop.
Atom Cache
<?php
/**
* Core Atom library namespace. This namespace contains all of the fundamental
* components of Atom, plus additional utilities that are provided by default.
* Some of these default components have sub namespaces if they provide child
* objects.
*
* @pacakge Atom
* @subpackage Library
*/
namespace Atom;
// Aliasing rules
use \Atom\Config;
use \Atom\Design\Multiton;
/**
* @package Atom
* @subpackage Library
*/
class Cache implements Multiton {
/**
* Holds a \Atom\Storage instance, to save memory
*
* @var object
*/
private static $storage;
private $items = array();
private $cache;
/**
* Class Constructor
*
* @param string Cache driver to use
* @param boolean Whether to use the persistent cache file extension
* @return void
*/
public function __construct($driver)
{
$this->cache = $driver;
}
public static function init()
{
static::$storage = (static::$storage) ?: new \Atom\Storage();
}
/**
* Get the saved data from cache.
*
*/
public function read($key)
{
// Read from cache...
$expired = $expires !== 0 and $expires < time();
return $data;
}
/**
* Save data to cache
*
* Sets meta=data needed for cache control, and saves it along with
* the data.
*
* @param mixed Cache data
* @param integer Expiry length in seconds
* @return void
*/
public function write($key, $data, $expiry = null, $extras = array())
{
$expiry = ($expiry) ?: Config::get('cache.expiry');
$cache = array(
'data' => $data,
'created' => time(),
'expires' => $expiry === 0 ? 0 : time() + $expiry,
);
$cache = array_merge($cache, $extras);
}
/**
* Remove data and delete cache entity
*
* @param boolean Force cache to expire
* @return boolean
*
* @throws \Exception If cache cannot be deleted
*/
public function expire($key, $force = false)
{
if($force or $this->expired())
{
$this->data = array();
$this->meta = array();
throw new \RuntimeException('Unable to delete cache with key ['.$this->key.']');
}
return false;
}
}
/* End of file cache.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment