Files
sihnon-php-lib/source/lib/SihnonFramework/Cache.class.php
Ben Roberts 81521eae5f Updates framework to support automated framework subclasses
All framework classes have been renamed to use the SihnonFramework_ prefix.
The class autoloader now looks for subclasses of the framework classes in the Sihnon_Lib directory, and
automatically creates them if they don't exist. The autoloader correctly creates interfaces and abstract
classes as needed, by using reflection to check the type of the parent class.
All references to classes within the framework now use the Sihnon_ prefix.
The PluginFactory supports multiple scan directories, and will search both the framework and subclass class
tree to find candidate plugins.
2010-10-10 18:32:16 +01:00

62 lines
1.8 KiB
PHP

<?php
class SihnonFramework_Cache {
protected $config;
protected $cache_dir;
public function __construct(Sihnon_Config $config) {
$this->config = $config;
$this->cache_dir = $config->get('cache.base_dir');
if (is_dir($this->cache_dir)) {
if ( ! is_writeable($this->cache_dir)) {
throw new Sihnon_Exception_InvalidCacheDir();
}
} else {
if ( ! Sihnon_Main::mkdir_recursive($this->cache_dir)) {
throw new Sihnon_Exception_InvalidCacheDir();
}
}
}
protected function cacheFilename($source_filename) {
return $this->cache_dir . sha1($source_filename);
}
public function exists($source_filename, $ttl = 3600) {
$cache_filename = $this->cacheFilename($source_filename);
// Check to see if the file is cached
if (!file_exists($cache_filename)) {
return false;
}
// Check to see if the cache has expired
if (filemtime($cache_filename) + $ttl < time()) {
// Delete the cached item
unlink($cache_filename);
return false;
}
return true;
}
public function store($source_filename, $content) {
$cache_filename = $this->cacheFilename($source_filename);
return file_put_contents($cache_filename, $content);
}
public function fetch($source_filename, $ttl = 3600) {
$cache_filename = $this->cacheFilename($source_filename);
if (!$this->exists($source_filename)) {
throw new Sihnon_Exception_CacheObjectNotFound($source_filename);
}
return file_get_contents($cache_filename);
}
};
?>