?>?>?>
namespace API201\Web\Cache;
//may deprecate soon
class DataCache {
static private $instance;
public function __construct() {
}
static function getInstance() {
if (static::$instance) return static::$instance;
static::$instance = new DataCache();
return static::$instance;
}
function filename($key) {
return str_replace('/', '%2f', $key);
}
function exists($key) {
global $_data_cache_loaded;
if (isset($_data_cache_loaded[$key])) return true;
$cache_file = $this->filename($key);
if (strlen($cache_file) > 200) return false;
if (file_exists("../cache/data/$cache_file")) return true;
}
function put($key, $data) {
global $_data_cache_loaded;
$vc = VarCache::getInstance();
$_data_cache_loaded[$key] = $data;
$cache_file = $this->filename($key);
if (strlen($cache_file) > 200) return;
if ($vc->isVarCache($key)) $vc->update();
file_put_contents("../cache/data/$cache_file", json_encode($data, JSON_PRETTY_PRINT), LOCK_EX);
}
function get($key) {
global $_data_cache_loaded;
$cache_file = $this->filename($key);
if (strlen($cache_file) > 200) return null;
if (isset($_data_cache_loaded[$key])) return $_data_cache_loaded[$key];
$_data_cache_loaded[$key] = json_decode(file_get_contents("../cache/data/$cache_file"), true);
return $_data_cache_loaded[$key];
}
function isExpired($key, $timeout = 300 /*5 mins*/) {
global $cache_item_expired_already_found;
global $_data_cache_loaded;
if (isset($_data_cache_loaded[$key])) return false;
if ($cache_item_expired_already_found) return false;
$cache_file = $this->filename($key);
if (strlen($cache_file) > 200) return true;
$now = time();
clearstatcache();
$filedate = filemtime("../cache/data/$cache_file");
if ($now-$filedate> $timeout) {
$cache_item_expired_already_found = true;
return true;
}
return false;
}
function invalidate($key) {
global $_data_cache_loaded;
if (isset($_data_cache_loaded[$key])) unset($_data_cache_loaded[$key]);
$cache_file = $this->filename($key);
if (strlen($cache_file) > 200) return;
if (file_exists("../cache/data/$cache_file")) {
unlink("../cache/data/$cache_file");
}
}
}
namespace API201\Autoloader;
use API201\Repository\ResourceFinder;
class Bootstrap {
const CACHE_BASE_PATH = '../cache/repository';
const CACHE_RESOURCE_LOCATIONS = '../cache/repository/resource_locations.json';
private static $_instance = null;
private $bootsrapIncludes = array(
'API201\Autoloader\Bootstrap' => 'api/api201/autoloader/bootstrap.class.php',
'API201\Repository\ResourceRepository' => 'api/api201/repository/resourcerepository.class.php',
'API201\Repository\ResourceFinder' => 'api/api201/repository/resourcefinder.class.php',
'API201\Repository\ResourceCache' => 'api/api201/repository/resourcecache.class.php',
'API201\App\AppConfig' => 'api/api201/app/appconfig.class.php',
'API201\Request\Request' => 'api/api201/request/request.class.php',
);
private $bootstrapRepositories = array(
//array( 'server' => 'http://repository3.v201.api', 'repositoryPath' => 'http/repository3.v201.api' ),
//array( 'server' => 'http://repository2.v201.api', 'repositoryPath' => 'http/repository2.v201.api' ),
array( 'server' => 'http://repository1.v201.api', 'repositoryPath' => 'http/repository1.v201.api' )
);
private $resource_locations = array();
public static function getInstance() {
if (!static::$_instance) static::$_instance = new Bootstrap();
return static::$_instance;
}
public static function includeAutoloaderFiles() {
if (static::$_instance) {
if (class_exists('API201\Repository\ResourceFinder')) {
$rf = ResourceFinder::getInstance();
$rf->find(null);
}
return;
}
static::$_instance = new Bootstrap();
if (file_exists(self::CACHE_RESOURCE_LOCATIONS)) {
static::$_instance->resource_locations = json_decode(file_get_contents(self::CACHE_RESOURCE_LOCATIONS), TRUE);
}
$flip_declared_classes = array_flip(get_declared_classes());
foreach(static::$_instance->bootsrapIncludes as $className => $classFilename) {
if (array_key_exists($className, $flip_declared_classes)) continue;
static::$_instance->load($classFilename);
}
if (file_exists('../cache/repository/http/repository1.v201.api')) {
if (in_array('API201\Repository\ResourceFinder', get_declared_classes())) {
$rf = ResourceFinder::getInstance();
$rf->find('api/api201/autoloader/bootstrap.class.php');
}
}
}
private function load($classFilename) {
if (!$this->resource_locations[$classFilename] || !file_exists($this->resource_locations[$classFilename].'/'.$classFilename)) {
foreach($this->bootstrapRepositories as $bootstrapRepository) {
try {
$url = $bootstrapRepository['server'].'/'.$classFilename;
$content = @file_get_contents($url, false, stream_context_create(array(
'http'=> array(
'timeout' => 0.2, //0.2 sec
)
)));
if ($content) {
if (!file_exists($bootstrapRepository['repositoryPath'].'/'.dirname($classFilename))) {
$this->makePath($bootstrapRepository['repositoryPath'].'/'.dirname($classFilename));
}
file_put_contents(self::CACHE_BASE_PATH.'/'.$bootstrapRepository['repositoryPath'].'/'.$classFilename, $content);
$this->resource_locations[$classFilename] = self::CACHE_BASE_PATH.'/'.$bootstrapRepository['repositoryPath'];
file_put_contents(self::CACHE_RESOURCE_LOCATIONS, json_encode($this->resource_locations, JSON_PRETTY_PRINT));
if ($classFilename != 'api/api201/autoloader/bootstrap.class.php') {
include(self::CACHE_BASE_PATH.'/'.$bootstrapRepository['repositoryPath'].'/'.$classFilename);
}
break;
}
} catch (Exception $e) {
$content = null;
}
}
} else {
if ($classFilename != 'api/api201/autoloader/bootstrap.class.php') {
include($this->resource_locations[$classFilename].'/'.$classFilename);
}
}
}
private function makePath($path) {
if (!file_exists(self::CACHE_BASE_PATH)) {
@mkdir(self::CACHE_BASE_PATH);
if (!file_exists(self::CACHE_BASE_PATH)) die('cannot mkdir cache base path');
chmod(self::CACHE_BASE_PATH, 0777);
}
$parts = explode('/', $path);
$currentPath = self::CACHE_BASE_PATH;
foreach($parts as $part) {
if (!file_exists($currentPath.'/'.$part)) {
@mkdir($currentPath.'/'.$part);
if (!file_exists($currentPath.'/'.$part)) die('cannot mkdir cache path');
chmod($currentPath.'/'.$part, 0777);
}
$currentPath .= '/'.$part;
}
}
}
?>
|
|
Travel B2B © 2010-Present. All Rights Reserved Powered by Travel B2B |