<?xml version="1.0"?><phpdoc><class name="Bs_FileCache" extends="Bs_Object" undoc="false" access="public" package="file"><file>C:/usr/local/lib/php/blueshoes-4.2/core/file/Bs_FileCache.class.php</file><doc><author>andrej arn &amp;lt;andrej at blueshoes dot org&amp;gt;</author><author>sam blume &amp;lt;sam at blueshoes dot org&amp;gt;</author><inherited src="Array"/><description>Like a lot of other caches and still a little different.Typical Use :o You have just crunched data typically from a file using a lot of CPU and want topersist the crunched data for later use.Also you want to be notified if the persisted data has become &amp;apos;out of date&amp;apos; becausethe &amp;apos;origin-file&amp;apos; has changed AND/OR the lifetime you have set has passed.- (This is also referred as &amp;quot;Hot Update&amp;quot;).USAGE Samples#################################################################################################### Sample 1# --------# Use with default settings.# Crunch only when the &amp;apos;origin-file&amp;apos; file changes.# *1*) Create the cacher.$cacher =&amp; new Bs_FileCache();   // Create new Cacher# *2*) Try to get the cached data using the file-path as key.$data = $cacher-&amp;gt;fetch(&amp;apos;D:/tmp/toCrunchData.txt&amp;apos;);# *3*) Test the return value, FALSE is error, NULL is &amp;quot;out of date&amp;quot;if ($data === FALSE) {       // ERRORecho &amp;quot;ERROR: &amp;quot; . $cacher-&amp;gt;getLastError();} elseif ($data === NULL){   // &amp;quot;Out Of Date&amp;quot; (or not cached)// Crunch the data (Replaced with *YOUR* crunching function.)$crunchedData = myCruncher(&amp;apos;D:/tmp/toCrunchData.txt&amp;apos;);# *4*) Cache the data as serialized data$cacher-&amp;gt;store(&amp;apos;D:/tmp/toCrunchData.txt&amp;apos;, serialize($crunchedData));} else {                     // SUCCESS$crunchedData = unserialize($data);}// do something with $crunchedData################### Sample 2# --------# Use with some property settings.# This time there is no &amp;apos;origin-file&amp;apos; file and we use a &amp;apos;lifetime&amp;apos; for the cache.# *1*) Create the cacher and set properties.$cacher =&amp; new Bs_FileCache();        // Create new Cacher$cacher-&amp;gt;setDir(&amp;apos;r:/&amp;apos;);               // TIPP: Use a RAM-Disk !$cacher-&amp;gt;setBufferSize(&amp;apos;200k&amp;apos;);       // % of memory is also possible$cacher-&amp;gt;setVerboseCacheNames(TRUE);  // Give cache-files a readable name# *2*) Try to get the cached data using any key *you* define.$MY_CACHE_KEY = &amp;apos;chachKey_1&amp;apos;;$data = $cacher-&amp;gt;fetch($MY_CACHE_KEY);# *3*) Test the return value, FALSE is error, NULL is &amp;quot;out of date&amp;quot;if ($data === FALSE) {       // ERRORecho &amp;quot;ERROR: &amp;quot; . $cacher-&amp;gt;getLastError();} elseif ($data === NULL){   // &amp;quot;Out Of Date&amp;quot; (or not cached)// Crunch the data (Replaced with *YOUR* crunching function.)$crunchedData = myCruncher(&amp;apos;D:/tmp/toCrunchData.txt&amp;apos;);# *4*) Cache the data as serialized data with timeout of 600s and disable &amp;apos;origin-file&amp;apos; check$cacher-&amp;gt;store($MY_CACHE_KEY, serialize($crunchedData), $originCheck=FALSE, 600);} else {                     // SUCCESS$crunchedData = unserialize($data);}###################################################################################################CONTEXT:To avoid confusion with the cache-mechanism used in this object keep in mind that&amp;apos;cache&amp;apos; : Is used to describe the data you want written to a persitent media (a file).&amp;apos;buffer&amp;apos;: Is used to describe the data brought into memory as optimization to avoidreading from the file (second level cache).FEATURES :o Checks if &amp;apos;origin-file&amp;apos; has changed AND/OR the lifetime you have set has passed(cache then becomes &amp;apos;out of date&amp;apos;).o Cache versioning to identify outdated/invalid cache data.*All* cache with older versions then $cacheVersion are considered &amp;apos;out of date&amp;apos;.Very useful during development and updates, where the structure of the data yougenerate has changed and all cache data becomes invalid. Saves you the hassle todelete all old cache files spread all over the disk.o Exclusive cache writing to disk.o Second level memory buffer that holds the cache data in memory for reuse(Currently only until script ends).o setBufferSize() : Ability to set the memory buffer size in bytes OR in % of theavailable script memory.o setDir() : Define where you want the cache-files to be stored. 2 optionsa) In one central dirb) Below each &amp;apos;origin-file&amp;apos; in a subdir.o setVerboseCacheNames() : Define if the &amp;apos;cache-files&amp;apos; should contain a verbose name or not.A verbose name contains the name of the &amp;apos;origin-file&amp;apos; (max first 100 chars).BASICS:Second level caching (memory buffered data):Stored or fetched data is kept in memory as a fifo-list.(Unless the data size is larger then the max allowed memory buffer size).If buffer is too full then the first entries of the fifo-list are removeduntil enough space is available and the new data is added to the end of the list.Entrys are valid only for a limited time (default is 10 secs. See setBufferLifetime()).Older entrys trigger the &amp;apos;up to date&amp;apos;-check and are reloaded from cache when fetched (this incl. ).+---------------------------+Application       |       Memory buffer       |           Diskdata stream &amp;lt;---&amp;gt; |   (second level cache)    |  &amp;lt;----&amp;gt; cache-file|  organized as Fifo-List   |+---------------------------+&amp;apos;Up To Date&amp;apos;-Checking :If in store() the parameter $originCheck===TRUE (default) it is assumed that thepassed &amp;apos;data stream&amp;apos; has it&amp;apos;s origin in a data file that was crunched (or parsedor whatever) and that the cache is valid as long as the &amp;apos;origin-file&amp;apos; exists andis not modified.An &amp;apos;Up To Date&amp;apos;-check compares the mod-time of the &amp;apos;origin-file&amp;apos; against themod-time of the &amp;apos;cache-file&amp;apos;. If&amp;apos;origin-file-time&amp;apos;  &amp;lt;=  &amp;apos;cache-file-time&amp;apos;the cache is considered &amp;apos;up to date&amp;apos;.The &amp;apos;Up To Date&amp;apos; check takes place every time the &amp;apos;cache-file&amp;apos; has to be loadedinto the memory buffer.dependencies: Bs_Dir, Bs_File, Bs_UnitConverter</description><shortdescription>File Cache Class.</shortdescription><version>4.2.$id$</version><since>bs4.1</since><copyright>blueshoes.org</copyright></doc><function name="Bs_FileCache" undoc="false" access="public"><doc><parameter name="$cacheVersion" default="0" type="mixed">sting or int. Cache data with other version is considered invalid.</parameter><return type="void"/><description>NOTE to $cacheVersion use:*All* cache with older versions are considerd &amp;apos;out of date&amp;apos;. Very usefull during developmentand updates, where the structure of the data you generate has changed and all cache databecomes invalid. Saves you the hassle to delete all old cache files spread all over the disk.</description><shortdescription>Constructor.</shortdescription></doc></function><function name="isModifiedSince" undoc="false" access="public"><doc><see type="function">getLastModified()</see><parameter name="$filePath" type="string"/><parameter name="$since" type="int">(unix timestamp in GMT!!)</parameter><return name="" type="bool"/><description/><shortdescription/></doc></function><function name="getLastModified" undoc="false" access="public"><doc><see type="function">isModifiedSince()</see><parameter name="$filePath" type="string"/><return name="" type="int"> (unix timestamp in GMT!!)</return><throws>bool FALSE if not cached.</throws><description/><shortdescription/></doc></function><function name="fetch" undoc="false" access="private"><doc><parameter name="$filePath" type="string">the full path to the &amp;apos;origin-file&amp;apos;.</parameter><return name="" type="string">The cached data on success OR NULL if data is out of date.</return><throws>FALSE on all errors. Check getLastError().</throws><description>The $filePath is used in 2 ways :a) As key to identify the cache.b) See store()(If in store() you set: $originCheck==TRUE)  the full path to the &amp;apos;origin-file&amp;apos; is used to look up the mod-time.(If in store() you set: $originCheck==FALSE) $filePath is &amp;apos;just&amp;apos; a key to identify the cach. No file look ups.</description><shortdescription>Fetch the data stream that was once pushed into the cache.</shortdescription></doc></function><function name="store" undoc="false" access="private"><doc><see type="function">setCacheLifeTime()</see><parameter name="$filePath" type="string">the full path to the &amp;apos;origin-file&amp;apos;.</parameter><parameter name="$dataStream" type="string">the data to store.</parameter><parameter name="$originCheck" default="TRUE," type="string">(default: TRUE) If we should check if the origin has modified. If FALSE don&amp;apos;t even assume an existing origin file.</parameter><parameter name="$maxLifeTime" default="NULL" type="string">(default: see above). The max lifetime in secs for this cache. 0 = forever. NULL (default) = use the setting from setCacheLifeTime(). if nothing was set using setCacheLifeTime() then it&amp;apos;s 0 (which means forever).</parameter><return type="void"/><throws>FALSE on all errors. Check getLastError().</throws><description>The $filePath is used in 2 ways :a) As key to identify the cache.b) (If $originCheck==TRUE)  is the full path to the &amp;apos;origin-file&amp;apos; to look up the mod-time.(If $originCheck==FALSE) *no* &amp;apos;origin-file&amp;apos; is assumed, but $maxLifeTime should be setto a int &amp;gt; 0. If not it will default to 24 houres. You can set a default $maxLifeTimewith setCacheLifeTime() or indivitually by passing a parameter.The cache is considered &amp;apos;out of date&amp;apos; (see fetch()) if one of following happens:a) $maxLifeTime has passed.b) The &amp;apos;origin-file&amp;apos; changes (and $originCheck is set to TRUE)Done by comparing the modified times of the &amp;apos;cache-file&amp;apos; and the &amp;apos;origin file&amp;apos;as given by the parameter $filePath. If cache-file is eqal age oryounger, cache is considered &amp;apos;up to date&amp;apos;Why the need for a max lifetime? You may think &amp;quot;we can check if the originalfile is older than the cache file, that&amp;apos;s enough&amp;quot;. lemme give you 2 reasons:1) If there is *no* origin file at all. That&amp;apos;s the case when we cache somecalcuated data or something out of a database.2) The origin data, may it come from a real file or not, may include alifecycle. In my case the data is made up from an xml property file of aproduct in an eShop. Now that product may become alive or may become obsoletewithout that the original file changes, so that my crunching function comes up witha different result over time.</description><shortdescription>Store the data stream to cache.</shortdescription></doc></function><function name="clearBuffer" undoc="false" access="private"><doc><parameter name="$filePath" default="&amp;apos;&amp;apos;" type="string">the full path to the &amp;apos;origin-file&amp;apos;.</parameter><return type="void"/><description>If the passed  $filePath is not empty, the whole memory buffer is cleared.Otherwise only the entry matching $filePath is marked as invalid (if found)and will not be used any more. (That means it will reloaded from file)</description><shortdescription>Clear the memory buffer (All or just one entry)</shortdescription></doc></function><function name="flushFileCache" undoc="false" access="public"><doc><return type="void"/><description/><shortdescription>kills all cached file content.</shortdescription></doc></function><function name="setBufferSize" undoc="false" access="private"><doc><parameter name="$newBufSize" type="mixed">(see above)</parameter><return name="" type="int">The set buffersize in byte.</return><throws>FALSE on error. (Buffer size is left unchanged)</throws><description>Data that is sorted or fetched will be buffered up to the given size.When buffer is full the oldest entry(s) is kicked out.Default is 5% of the available memory as given by &amp;apos;memory_limit&amp;apos; in the php.ini file.No buffering: If &amp;apos;memory_limit&amp;apos; is not set or if you pass 0.Buffering with no limit: If you pass -1. (Of corse your not able to exceed PHP &amp;apos;memory_limit&amp;apos;)NOTE:It is possible that data of one entry is larger then the buffer limit.It will *NOT* be buffered then.PARAM:If numeric: We assume it as assumed to be absolute byte size.If string : May end with one of the usual unit endings e.g &amp;apos;k&amp;apos;, &amp;apos;kb&amp;apos;, &amp;apos;M&amp;apos; (kilo-, mega-byte)If ending with &amp;apos;%&amp;apos; this means use x % of the available memory as given bymemory_limit in the php.ini file. But we never exceed 80%.</description><shortdescription>Set the size of the cache buffer</shortdescription></doc></function><function name="setBufferLifetime" undoc="false" access="private"><doc><see type="function">setCacheLifeTime()</see><parameter name="$sec" default="10" type="integer">(default is 10s)</parameter><return type="void"/><description>on the &amp;apos;origin&amp;apos;-file is fired. Only active if there is an &amp;apos;origin&amp;apos;-file in use.If the &amp;apos;origin&amp;apos;-file has changed since a fetch() will return the &amp;apos;out of date&amp;apos;-value NULL.(More doc in header).</description><shortdescription>The max time a memory buffered data is considered &amp;apos;up to date&amp;apos; befor an &amp;apos;up to date&amp;apos;-check</shortdescription></doc></function><function name="setCacheLifeTime" undoc="false" access="private"><doc><see type="function">setBufferLifetime()</see><parameter name="$sec" default="0" type="integer">(default is 0 == &amp;apos;no lifetime check&amp;apos;). See above.</parameter><return type="void"/><description>hasn&amp;apos;t changed. After this time a fetch() will return the &amp;apos;out of date&amp;apos;-value NULL.Use if you want to limit the chache lifetime or when there is no &amp;apos;origin&amp;apos;-file tocheck against (More doc in header).</description><shortdescription>The max time until the cached data is considered &amp;apos;out to date&amp;apos; even if the &amp;apos;origin&amp;apos;-file</shortdescription></doc></function><function name="setDir" undoc="false" access="private"><doc><parameter name="$path" default="&amp;apos;&amp;apos;" type="string">(see above).</parameter><return type="void"/><description>If a valid path is passed all cache-files are stored in that dir. if the path does notexist yet, this method tries to create it.If called without param (or the dir is not writeable or not createable or whatever) asubdir called &amp;apos;_cache&amp;apos; will be created below the &amp;apos;origin-file&amp;apos; dir and the cache-filesare stored there (default behavior, fallback too).</description><shortdescription>Define where you want the cache-files to be stored.</shortdescription></doc></function><function name="setVerboseCacheNames" undoc="false" access="private"><doc><parameter name="$trueFalse" default="TRUE" type="string">(see above).</parameter><return type="void"/><description>A verbose name contains the name of the &amp;apos;origin-file&amp;apos; up to 100 chars.To keep the file unique, a md5-string is always appended to the chach-file.( The full path is taken to built the md5-string)</description><shortdescription>Define if the &amp;apos;cache-files&amp;apos; should contain a verbose name or not.</shortdescription></doc></function><function name="getLastError" undoc="false" access="private"><doc><return name="" type="mixed">If last public fuction had an error, a string is returned otherwise FALSE.</return><description/><shortdescription>Get last error.</shortdescription></doc></function><function name="_getFiFoIndex" undoc="false" access="private"><doc><parameter name="$filePath" undoc="true"/><return name="" type="int">The index (see above)</return><throws>FALSE if not found.</throws><description/><shortdescription>Find the index of the fifo-array matching $filePath</shortdescription></doc></function><function name="_addToFiFo" undoc="false" access="private"><doc><parameter name="$cacheBlock" type="array">A data chunck</parameter><return name="" type="bool">TRUE if add succeded, otherwise FALSE</return><description/><shortdescription>Add $cacheBlock to fifo-array</shortdescription></doc></function><function name="_fifoFreeSpace" undoc="false" access="private"><doc><parameter name="$sizeOfNewData" undoc="true"/><return type="void"/><description/><shortdescription>Free up the fifo-array until $sizeOfNewData fits in.</shortdescription></doc></function><function name="_fifoGarbageCollect" undoc="false" access="private"><doc><return type="void"/><description/><shortdescription>Get rid of entries in the fifo-array that are marked invalid</shortdescription></doc></function><function name="_determinePathToCache" undoc="false" access="private"><doc><parameter name="$filePath" undoc="true"/><return type="void"/><description>Problems solved here:a) If &amp;apos;verboseName&amp;apos; is TRUE then include the origin-name into the cache name.b) If &amp;apos;storeDir&amp;apos; is set all cache-files are stored in one dir and have to be unique.c) Unique file names are achieved by appending a md5-stringMake unique name with md5(abs.path/origin-file)</description><shortdescription>Determine the cache-dir from the origin-dir and make the cache name.</shortdescription></doc></function><function name="_isCacheFileUptodate" undoc="false" access="private"><doc><parameter name="$filePath" undoc="true"/><parameter name="$cacheFilePath" undoc="true"/><return name="" type="bool">TRUE on success. FALSE Otherwise.</return><throws>FALSE on all errors. Check getLastError().</throws><description>It is assumed that the passed &amp;apos;data stream&amp;apos; has it&amp;apos;s origin in data file thatwas crunched (or parsed or whatever) and that the cache is valid as long asthe &amp;apos;origin-file&amp;apos; exits and is not modified.An &amp;apos;Up To Date&amp;apos;-check compars the mod-time of the &amp;apos;origin-file&amp;apos; against themod time of the &amp;apos;cache-file&amp;apos;. If&amp;apos;origin-file-time&amp;apos;  &amp;lt;=  &amp;apos;cache-file-time&amp;apos;the cache is consitered &amp;apos;up to date&amp;apos;.The &amp;apos;Up To Date&amp;apos; check takes place every time the &amp;apos;cache-file&amp;apos; has to be loadedinto the memory buffer.</description><shortdescription>&amp;apos;Up To Date&amp;apos; - checking:</shortdescription></doc></function><function name="_writeCacheFile" undoc="false" access="private"><doc><parameter name="$cacheFilePath" undoc="true"/><parameter name="$cacheBlock" undoc="true"/><return type="void"/><throws>FALSE on all errors. Check getLastError().</throws><description/><shortdescription>Write cache</shortdescription></doc></function><function name="_readCacheFile" undoc="false" access="private"><doc><parameter name="$cacheFilePath" undoc="true"/><return type="void"/><throws>FALSE on all errors. Check getLastError().</throws><description/><shortdescription>Read cache</shortdescription></doc></function><variable name="$_bsFile" access="private" type="object [unknown]"><doc><description/><shortdescription>STATIC instance of Bs_File. gets instanced the first time in _writeCacheFile().</shortdescription></doc></variable><variable name="$_cacheProp" access="private" type="array">array(&#x0a;       &amp;apos;maxCacheLifeTime&amp;apos; =&amp;gt; 0,       // &amp;lt;= Time (in secs<doc><see type="function">setVerboseCacheNames()</see><description/><shortdescription>Internal cache properties</shortdescription></doc></variable><variable name="$_fifoBuffer" access="private" type="array">array()<doc><description/><shortdescription>The second level cache as fifo list.</shortdescription></doc></variable><variable name="$_cacheVersion" access="private" type="string">NULL<doc><description>*All* cache with older versions are then &amp;apos;out of date&amp;apos;. Very usefull during developmentand updates, where stucture of data you generate (and cache) has changed. Saves youthe hassel to go an delete all old cache files spread all over the disk.Use an int and increase it on every structure change. Set it in the constructor.</description><shortdescription>The cache data has a version to identify outdated/invalid cache data.</shortdescription></doc></variable><variable name="$_lastErrMsg" access="private" type="string">&amp;apos;&amp;apos;<doc><see type="var">$_errMsgHistory</see><description/><shortdescription>contains the last error that occured and *is* reset after every *successfull* public function call.</shortdescription></doc></variable><variable name="$_errMsgHistory" access="private" type="array">array()<doc><see type="var">$_errMsgHistory</see><description/><shortdescription>for debugging. contains an error history that is *not* reset.</shortdescription></doc></variable><variable name="$_determineFileNameHash" access="private" type="array">array()<doc></doc></variable><constant name="Bs_FILECACHE_VERSION" undoc="true" access="private" case="default: case sensitive">4.0.$x$<doc></doc></constant><inherited src="Bs_Object" type="functions"><element>isex</element><element>isexception</element><element>tostring</element><element>tohtml</element><element>persist</element><element>unpersist</element><element>bs_object</element><element>bbsetoutput</element><element>bbawake</element><element>bbisawake</element><element>bbxmsg</element><element>bbxfunctionstart</element><element>bbxfunctionend</element><element>bbxecho</element><element>bbxvar</element><element>bbxvardump</element><element>bbforcetrace</element><element>bbbufferstart</element><element>bbbufferget</element><element>bbbufferendflush</element><element>bbbufferendclean</element></inherited><inherited src="Bs_Object" type="consts"><element>bs_object_version</element></inherited><path><parent>Bs_Object</parent></path><baseclass>Bs_Object</baseclass></class></phpdoc>