CODE STYLE class.fileCache.php

This commit is contained in:
Fernando Ontiveros
2012-10-09 12:44:18 -04:00
parent a1f4caea41
commit 9196ae75e3

View File

@@ -1,6 +1,8 @@
<?php <?php
/** /**
* class.memcached.php * class.memcached.php
*
* @package workflow.engine.ProcessMaker * @package workflow.engine.ProcessMaker
* *
* ProcessMaker Open Source Edition * ProcessMaker Open Source Edition
@@ -13,98 +15,102 @@
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details. * GNU Affero General Public License for more details.
* *
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
* For more information, contact Colosa Inc, 2566 Le Jeune Rd., * For more information, contact Colosa Inc, 2566 Le Jeune Rd.,
* Coral Gables, FL, 33134, USA, or email info@colosa.com. * Coral Gables, FL, 33134, USA, or email info@colosa.com.
* *
*/ */
class FileCache { class FileCache
{
function __construct($dir) { function __construct ($dir)
$this->dir = $dir; {
} $this->dir = $dir;
private function _name($key) {
return sprintf ( "%s/%s", $this->dir, sha1 ( $key ) );
}
public function get($key, $expiration = 3600) {
if (! is_dir ( $this->dir ) or ! is_writable ( $this->dir )) {
return FALSE;
} }
$cache_path = $this->_name ( $key ); private function _name ($key)
{
if (! @file_exists ( $cache_path )) { return sprintf( "%s/%s", $this->dir, sha1( $key ) );
return FALSE;
} }
if (filemtime ( $cache_path ) < (time () - $expiration)) { public function get ($key, $expiration = 3600)
// $this->clear($key); {
// different users can have different timeout requests
return FALSE; if (! is_dir( $this->dir ) or ! is_writable( $this->dir )) {
return FALSE;
}
$cache_path = $this->_name( $key );
if (! @file_exists( $cache_path )) {
return FALSE;
}
if (filemtime( $cache_path ) < (time() - $expiration)) {
// $this->clear($key);
// different users can have different timeout requests
return FALSE;
}
if (! $fp = @fopen( $cache_path, 'rb' )) {
return FALSE;
}
flock( $fp, LOCK_SH );
$cache = '';
if (filesize( $cache_path ) > 0) {
$cache = unserialize( fread( $fp, filesize( $cache_path ) ) );
} else {
$cache = NULL;
}
flock( $fp, LOCK_UN );
fclose( $fp );
return $cache;
} }
if (! $fp = @fopen ( $cache_path, 'rb' )) { public function set ($key, $data)
return FALSE; {
if (! is_dir( $this->dir ) or ! is_writable( $this->dir )) {
return FALSE;
}
$cache_path = $this->_name( $key );
if (! $fp = fopen( $cache_path, 'wb' )) {
return FALSE;
}
if (flock( $fp, LOCK_EX )) {
fwrite( $fp, serialize( $data ) );
flock( $fp, LOCK_UN );
} else {
return FALSE;
}
fclose( $fp );
@chmod( $cache_path, 0777 );
return TRUE;
} }
flock ( $fp, LOCK_SH ); public function clear ($key)
{
$cache_path = $this->_name( $key );
$cache = ''; if (file_exists( $cache_path )) {
unlink( $cache_path );
return TRUE;
}
if (filesize ( $cache_path ) > 0) { return FALSE;
$cache = unserialize ( fread ( $fp, filesize ( $cache_path ) ) );
} }
else {
$cache = NULL;
}
flock ( $fp, LOCK_UN );
fclose ( $fp );
return $cache;
}
public function set($key, $data) {
if (! is_dir ( $this->dir ) or ! is_writable ( $this->dir )) {
return FALSE;
}
$cache_path = $this->_name ( $key );
if (! $fp = fopen ( $cache_path, 'wb' )) {
return FALSE;
}
if (flock ( $fp, LOCK_EX )) {
fwrite ( $fp, serialize ( $data ) );
flock ( $fp, LOCK_UN );
}
else {
return FALSE;
}
fclose ( $fp );
@chmod ( $cache_path, 0777 );
return TRUE;
}
public function clear($key) {
$cache_path = $this->_name ( $key );
if (file_exists ( $cache_path )) {
unlink ( $cache_path );
return TRUE;
}
return FALSE;
}
} }