Permalink
Browse files

开始重新设计虚拟文件系统

  • Loading branch information...
1 parent f5a8f79 commit 0217afa9db0a833fa2593a47b0d87318f6cbc0d3 @aleechou aleechou committed Apr 20, 2012
View
@@ -110,6 +110,8 @@ static public function setHighSpeed(Cache $aCache)
*
* @return int
*/
- //abstract public function expireTime($sDataPath) ;
+ //abstract public function expireTime($sDataPath) ;
+
+ protected $sFolderPrefix ;
}
View
@@ -30,12 +30,8 @@
class FSCache extends Cache
{
public function __construct($sFolder)
- {
- $this->aFolder = new Folder($sFolder) ;
- if( !$this->aFolder->exists() )
- {
- $this->aFolder->create() ;
- }
+ {
+ $this->sFolderPrefix = trim($sFolder,'/').'/' ;
}
public function item($sDataPath)
@@ -49,24 +45,28 @@ public function item($sDataPath)
{
$this->delete($sDataPath) ;
return null ;
- }
+ }
+
+ $sDataPath = $this->sFolderPrefix.trim($sDataPath,'/') ;
// 尝试 .php
- if( $sFilePath = $this->aFolder->findFile($sDataPath.'.php',Folder::FIND_RETURN_PATH) )
+ if( is_file($sDataPath.'.php') )
{
- return include $sFilePath ;
+ return include $sDataPath.'.php' ;
}
// 尝试 .data
- if( $sFilePath = $this->aFolder->findFile($sDataPath.'.data',Folder::FIND_RETURN_PATH) )
+ else if( is_file($sDataPath.'.data') )
{
- return unserialize(file_get_contents($sFilePath)) ;
+ return unserialize(file_get_contents($sDataPath.'.data')) ;
}
return null ;
}
public function setItem($sDataPath,$data,$nExpire=self::expire_default,$fCreateTimeMicroSec=-1)
- {
+ {
+ $sDataPath = $this->sFolderPrefix.trim($sDataPath,'/') ;
+
if(is_object($data))
{
$sSerialize = serialize($data) ;
@@ -76,11 +76,14 @@ public function setItem($sDataPath,$data,$nExpire=self::expire_default,$fCreateT
{
$sSerialize = "<?php\r\nreturn ".var_export($data,true).' ;' ;
$sFilePath = $sDataPath.'.php' ;
- }
- if( !$sFilePath=$this->aFolder->findFile($sFilePath,Folder::FIND_AUTO_CREATE|Folder::FIND_RETURN_PATH) )
- {
- return false ;
- }
+ }
+
+ $sDataFolder = dirname($sFilePath) ;
+ if( !is_dir($sDataFolder) and !Folder::createInstance($sDataFolder)->create() )
+ {
+ return ;
+ }
+
file_put_contents( $sFilePath,$sSerialize) ;
// create time
@@ -102,13 +105,8 @@ public function setItem($sDataPath,$data,$nExpire=self::expire_default,$fCreateT
{
$nExpireSec = ceil($fCreateTimeMicroSec) + $nExpire ;
}
-
- if( !$sFilePath=$this->aFolder->findFile($sDataPath.'.time',Folder::FIND_AUTO_CREATE|Folder::FIND_RETURN_PATH) )
- {
- return false ;
- }
- file_put_contents( $sFilePath, "<?php return array({$fCreateTimeMicroSec},{$nExpireSec}) ;" ) ;
+ file_put_contents( $sDataPath.'.time', "<?php return array({$fCreateTimeMicroSec},{$nExpireSec}) ;" ) ;
}
/**
@@ -120,19 +118,24 @@ public function delete($sDataPath)
// 所有
if( empty($sDataPath) )
{
- $this->aFolder->delete(true,true) ;
- }
- // 删除目录
- else if( $aFolder=$this->aFolder->findFolder($sDataPath) )
- {
- $aFolder->delete(true,true) ;
- }
- // 删除指定内容
- else
- {
- $this->aFolder->deleteChild($sDataPath.'.data') ;
- $this->aFolder->deleteChild($sDataPath.'.php') ;
- $this->aFolder->deleteChild($sDataPath.'.time') ;
+ $this->clear() ;
+ }
+ else
+ {
+ $sDataPath = $this->sFolderPrefix.trim($sDataPath,'/') ;
+
+ // 删除目录
+ if( is_dir($sDataPath) )
+ {
+ Folder::createInstance($sDataPath)->delete(true,true) ;
+ }
+ // 删除指定内容
+ else
+ {
+ unlink($sDataPath.'.data') ;
+ unlink($sDataPath.'.php') ;
+ unlink($sDataPath.'.time') ;
+ }
}
}
@@ -143,54 +146,49 @@ public function delete($sDataPath)
*/
public function clear()
{
- return $this->aFolder->delete(true,true) ;
+ return Folder::createInstance($this->sFolderPrefix)->delete(true,true) ;
}
-
+
/**
* Enter description here ...
- *
+ *
* @return bool
*/
public function isExpire($sDataPath)
{
- if( !$sFilePath = $this->aFolder->findFile($sDataPath.'.time',Folder::FIND_RETURN_PATH) )
- {
- return true ;
- }
-
- list(,$nExpireTime) = include $sFilePath ;
+ $nExpireTime = $this->expireTime($sDataPath) ;
return $nExpireTime>0 and $nExpireTime<time() ;
}
/**
* Enter description here ...
- *
+ *
* @return float
*/
public function createTime($sDataPath)
- {
- if( !$sFilePath = $this->aFolder->findFile($sDataPath.'.time',Folder::FIND_RETURN_PATH) )
- {
- return 0 ;
+ {
+ $sPath = $this->sFolderPrefix.trim($sDataPath,'/').'.time' ;
+ if(!is_file($sPath))
+ {
+ return 0 ;
}
-
- list($fCreateTime,) = include $sFilePath ;
- return (float)$fCreateTime ;
+ list($fCreateTime,) = explode(',',file_get_contents($sPath)) ;
+ return (int)$fCreateTime ;
}
-
+
/**
* Enter description here ...
*
* @return int
*/
public function expireTime($sDataPath)
- {
- if( !$sFilePath = $this->aFolder->findFile($sDataPath.'.time',Folder::FIND_RETURN_PATH) )
+ {
+ $sPath = $this->sFolderPrefix.trim($sDataPath,'/').'.time' ;
+ if(!is_file($sPath))
{
- return -1 ;
+ return 0 ;
}
-
- list(,$nExpireTime) = include $sFilePath ;
+ list(,$nExpireTime) = explode(',',file_get_contents($sPath)) ;
return (int)$nExpireTime ;
}
@@ -0,0 +1,166 @@
+<?php
+////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// 这个文件是 JeCat PHP框架的一部分,该项目和此文件 均遵循 GNU 自由软件协议
+//
+// Copyleft 2008-2012 JeCat.cn(http://team.JeCat.cn)
+//
+//
+// JeCat PHP框架 的正式全名是:Jellicle Cat PHP Framework。
+// “Jellicle Cat”出自 Andrew Lloyd Webber的音乐剧《猫》(《Prologue:Jellicle Songs for Jellicle Cats》)。
+// JeCat 是一个开源项目,它像音乐剧中的猫一样自由,你可以毫无顾忌地使用JCAT PHP框架。JCAT 由中国团队开发维护。
+// 正在使用的这个版本是:0.7.1
+//
+//
+//
+// 相关的链接:
+// [主页] http://www.JeCat.cn
+// [源代码] https://github.com/JeCat/framework
+// [下载(http)] https://nodeload.github.com/JeCat/framework/zipball/master
+// [下载(git)] git clone git://github.com/JeCat/framework.git jecat
+// 不很相关:
+// [MP3] http://www.google.com/search?q=jellicle+songs+for+jellicle+cats+Andrew+Lloyd+Webber
+// [VCD/DVD] http://www.google.com/search?q=CAT+Andrew+Lloyd+Webber+video
+//
+////////////////////////////////////////////////////////////////////////////////////////////////////////////
+/*-- Project Introduce --*/
+namespace org\jecat\framework\cache ;
+
+use org\jecat\framework\lang\Object;
+
+class SaeStorageCache extends Cache
+{
+ public function __construct($sDomain,$sFolder='')
+ {
+ $this->sDomain = $sDomain ;
+ $this->sFolderPrefix = trim($sFolder,'/').'/' ;
+ $this->aSaeStorage = Object::singleton('SaeStorage') ;
+ }
+
+ public function item($sDataPath)
+ {
+ $sDataPath = trim($sDataPath,'/') ;
+ $nExpireTime = $this->expireTime($sDataPath) ;
+ if( $nExpireTime<=0 )
+ {
+ return null ;
+ }
+ else if( $nExpireTime>0 and $nExpireTime<time() )
+ {
+ $this->delete($sDataPath) ;
+ return null ;
+ }
+
+ return unserialize($this->aSaeStorage->read($this->sDomain,$this->sFolderPrefix.$sDataPath.'.data',$sSerialize)) ;
+ }
+
+ public function setItem($sDataPath,$data,$nExpire=self::expire_default,$fCreateTimeMicroSec=-1)
+ {
+ $sDataPath = trim($sDataPath,'/') ;
+ $sSerialize = serialize($data) ;
+ $sFilePath = $this->sFolderPrefix.$sDataPath.'.data' ;
+
+ if( !$this->aSaeStorage->write($this->sDomain,$sFilePath,$sSerialize) )
+ {
+ return false ;
+ }
+
+ // create time
+ if($fCreateTimeMicroSec<0)
+ {
+ $fCreateTimeMicroSec = microtime(true) ;
+ }
+
+ // expire time
+ if( $nExpire===Cache::expire_allways )
+ {
+ $nExpireSec = 0 ;
+ }
+ else if( $nExpire===Cache::$expire_default )
+ {
+ $nExpireSec = ceil($fCreateTimeMicroSec) + Cache::$expire_default ;
+ }
+ else
+ {
+ $nExpireSec = ceil($fCreateTimeMicroSec) + $nExpire ;
+ }
+
+ return $this->aSaeStorage->write($this->sDomain,$this->sFolderPrefix.$sDataPath.'.time',$sSerialize) ;
+ }
+
+ /**
+ * Enter description here ...
+ *
+ */
+ public function delete($sDataPath)
+ {
+ // 所有
+ if( empty($sDataPath) )
+ {
+ $this->clear() ;
+ }
+ // 删除指定内容
+ else
+ {
+ $sDataPath = trim($sDataPath,'/') ;
+ $this->aSaeStorage->delete($this->sDomain,$this->sFolderPrefix.$sDataPath.'.data') ;
+ $this->aSaeStorage->delete($this->sDomain,$this->sFolderPrefix.$sDataPath.'.time') ;
+ $this->aSaeStorage->deleteFolder($this->sDomain,$this->sFolderPrefix.$sDataPath) ;
+ }
+ }
+
+ /**
+ * Enter description here ...
+ *
+ * @return bool
+ */
+ public function clear()
+ {
+ return $this->aSaeStorage->deleteFolder($this->sDomain,$this->sFolderPrefix) ;
+ }
+
+ /**
+ * Enter description here ...
+ *
+ * @return bool
+ */
+ public function isExpire($sDataPath)
+ {
+ $nExpireTime = $this->expireTime($sDataPath) ;
+ return $nExpireTime>0 and $nExpireTime<time() ;
+ }
+
+ /**
+ * Enter description here ...
+ *
+ * @return float
+ */
+ public function createTime($sDataPath)
+ {
+ $data = $this->aSaeStorage->read($this->sDomain,$this->sFolderPrefix.ltrim($sDataPath,'/').'.time') ;
+ if($data===false)
+ {
+ return 0 ;
+ }
+ list($fCreateTime,) = explode(',',$data) ;
+ return (int)$fCreateTime ;
+ }
+
+ /**
+ * Enter description here ...
+ *
+ * @return int
+ */
+ public function expireTime($sDataPath)
+ {
+ $data = $this->aSaeStorage->read($this->sDomain,$this->sFolderPrefix.ltrim($sDataPath,'/').'.time') ;
+ if($data===false)
+ {
+ return 0 ;
+ }
+ list(,$nExpireTime) = explode(',',$data) ;
+ return (int)$nExpireTime ;
+ }
+
+ private $sDomain ;
+ private $aSaeStorage ;
+}
@@ -0,0 +1,16 @@
+<?php
+namespace org\jecat\framework\fs\vfs ;
+
+interface IPhysicalFileSystem
+{
+ /**
+ * @return resource
+ */
+ public function opendir($sPath,$options) ;
+
+ /**
+ * @return string
+ */
+ public function readdir($hResource) ;
+}
+
Oops, something went wrong.

0 comments on commit 0217afa

Please sign in to comment.