📄 class.wsdlcache.php
字号:
<?php
class wsdlcache
{
var $fplock;
var $cache_lifetime;
var $cache_dir;
var $debug_str = "";
function wsdlcache( $cache_dir = ".", $cache_lifetime = 0 )
{
$this->fplock = array( );
$this->cache_dir = $cache_dir != "" ? $cache_dir : ".";
$this->cache_lifetime = $cache_lifetime;
}
function createfilename( $wsdl )
{
return $this->cache_dir."/wsdlcache-".md5( $wsdl );
}
function debug( $string )
{
$this->debug_str .= get_class( $this ).": {$string}\n";
}
function get( $wsdl )
{
$filename = $this->createfilename( $wsdl );
if ( $this->obtainmutex( $filename, "r" ) )
{
if ( 0 < $this->cache_lifetime )
{
if ( file_exists( $filename ) && $this->cache_lifetime < time( ) - filemtime( $filename ) )
{
unlink( $filename );
$this->debug( "Expired {$wsdl} ({$filename}) from cache" );
$this->releasemutex( $filename );
return null;
}
}
$fp = @fopen( $filename, "r" );
if ( $fp )
{
$s = implode( "", @file( $filename ) );
fclose( $fp );
$this->debug( "Got {$wsdl} ({$filename}) from cache" );
}
else
{
$s = null;
$this->debug( "{$wsdl} ({$filename}) not in cache" );
}
$this->releasemutex( $filename );
return !is_null( $s ) ? unserialize( $s ) : null;
}
return null;
}
function obtainmutex( $filename, $mode )
{
if ( isset( $this->fplock[md5( $filename )] ) )
{
$this->debug( "Lock for {$filename} already exists" );
return false;
}
$this->fplock[md5( $filename )] = fopen( $filename.".lock", "w" );
if ( $mode == "r" )
{
return flock( $this->fplock[md5( $filename )], LOCK_SH );
}
else
{
return flock( $this->fplock[md5( $filename )], LOCK_EX );
}
}
function put( $wsdl_instance )
{
$filename = $this->createfilename( $wsdl_instance->wsdl );
$s = serialize( $wsdl_instance );
if ( $this->obtainmutex( $filename, "w" ) )
{
$fp = fopen( $filename, "w" );
fputs( $fp, $s );
fclose( $fp );
$this->debug( "Put {$wsdl_instance->wsdl} ({$filename}) in cache" );
$this->releasemutex( $filename );
return true;
}
return false;
}
function releasemutex( $filename )
{
$ret = flock( $this->fplock[md5( $filename )], LOCK_UN );
fclose( $this->fplock[md5( $filename )] );
unset( $this->$this->fplock->md5( $filename ) );
return $ret;
}
function remove( $wsdl )
{
$filename = $this->createfilename( $wsdl );
$this->obtainmutex( $filename, "w" );
$ret = unlink( $filename );
$this->debug( "Removed {$wsdl} ({$filename}) from cache" );
$this->releasemutex( $filename );
return $ret;
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -