<?php
/**
*
* @author Benjamin Gillissen <hide@address.com>
*
* **************************************************************
Copyright (C) 2009 Benjamin Gillissen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details at:
http://www.gnu.org/copyleft/gpl.html
* **************************************************************
*/
class file_cache extends file_abstract {
private $CRef;
private $expire;
private $maxsize;
private $match;
private $isconn;
public function __construct($CRef){
$this->CRef = $CRef;
$this->expire = configs::get('file_cache', $this->CRef, 'expire');
$this->maxsize = configs::get('file_cache', $this->CRef, 'size');
$this->match = configs::get('file_cache', $this->CRef, 'match');
parent::__construct(configs::get('file_cache', $this->CRef, 'repo'));
$this->isconn = $this->connect();
}
private function getbasefolder(){
//report/report_%hash%.xml';
$buf = split('/', $this->match);
foreach($buf as $k => $v ){
if ( isset($buf[$k+1]) ){ $out[] = $v; }
}
if ( isset($out) ){ return implode("/", $out); }
return FALSE;
}
private function getfilematch(){
//report/report_%hash%.xml';
$buf = split('/', $this->match);
$buf = $buf[count($buf)-1];
$buf = '('.str_replace('%hash%', ')([a-z0-9]{32})(', $buf).')';
return $buf;
}
public function getpath($ref){
return str_replace('%hash%', CORE::hash($ref), $this->match);
}
public function cleanup(){
if ( !$this->isconn ){ return FALSE; }
$base = $this->getbasefolder();
$this->chdir($base);
$match = $this->getfilematch();
$tots=0;
$rfa=0;
echo "Cleaning Cache : $this->CRef\n";
while( FALSE !== ($file = $this->readdir('')) ){
if ( ereg($match, $file) ){
if ( $this->getmtime($file)+$this->expire <= time() ){
$rfa++;
$this->remove($file);
} else {
$tots = $tots + $this->getsize($file);
}
}
}
echo "$rfa files removed for expiration\n";
errors::raise($rfa.' of the oldest files have been removed from cache '.$this->CRef, CORE_LOG_NOTICE, 'FCACHE');
echo "Cache size : ".$tots.', limit : '.$this->maxsize."\n";
if ( $tots >= $this->maxsize ){
while( FALSE !== ($file = $this->readdir('')) ){
if ( ereg($match, $file) ){ $buf[$file] = $this->getmtime($file); }
}
if ( isset($buf) ){
asort($buf, SORT_NUMERIC);
$i=0;
foreach($buf as $file => $mtime){
if ( $tots >= $this->maxsize ){
$tots = $tots - $this->getsize($file);
$this->remove($file);
$i++;
} else { break; }
}
echo "$i of the oldest files have been removed\n";
errors::raise('Cache '.$this->CRef.' has exceeded his max size, '.$i.' of the oldest files have been removed', CORE_LOG_DEBUG, 'FCACHE');
}
}
$this->chdir('/');
return TRUE;
}
public function iscached($ref){
if ( !$this->isconn ){ return FALSE; }
$p = $this->getpath($ref);
return $this->isfile($p);
}
public function isexpired($ref){
if ( !$this->isconn ){ return FALSE; }
$p = $this->getpath($ref);
if ( !$this->isfile($p) ){ return TRUE; }
if ( $this->getmtime($p)+$this->expire <= time() ){ return TRUE; }
return FALSE;
}
public function import_file($ref, $srcfile, $repo=NULL){
if ( !$this->isconn ){ return FALSE; }
if ( !file_exists($srcfile) ){
errors::raise('File to import to cache '.$this->CRef.', source does not exists localy : '.$srcfile, CORE_LOG_ERROR, 'FCACHE');
return FALSE;
}
return $this->copy($repo, $srcfile, $this->getreporef(), $this->getpath($ref));
}
public function import_data($ref, $data){
if ( !$this->isconn ){ return FALSE; }
if ( empty($data) ){ errors::raise('Importing empty data to cache, is it really needed?', CORE_LOG_NOTICE, 'FCACHE'); }
return $this->writefile($this->getpath($ref), $data);
}
public function append_data($ref, $data){
if ( !$this->isconn ){ return FALSE; }
if ( empty($data) ){ errors::raise('Importing empty data to cache, is it really needed?', CORE_LOG_NOTICE, 'FCACHE'); }
return $this->writefile($this->getpath($ref), $data);
}
}