kernel
[ class tree: kernel ] [ index: kernel ] [ all elements ]

Source for file BitCache.php

Documentation is available at BitCache.php

  1. <?php
  2. /**
  3.  * @package kernel
  4.  * @version $Header$
  5.  */
  6.  
  7. /**
  8.  * A basic library to handle caching of various data
  9.  *
  10.  * @package kernel
  11.  */
  12. class BitCache {
  13.     /**
  14.      * Used to store the directory used to store the cache files.
  15.      * @private
  16.      */
  17.     var $mFolder;
  18.     /**
  19.      * Will check the temp cache folder for existence and create it if necessary.
  20.      *
  21.      * @param string $pSubdir use a specifed subdirectory
  22.      * @param boolean $pUseStorage use the storage directory instead of the temp dir. only makes sense if you need direct webaccess to stored cachefiles
  23.      * @access public
  24.      * @return void 
  25.      */
  26.     function BitCache$pSubdir 'cache'$pUseStorage FALSE {
  27.         if$pUseStorage and defined(STORAGE_PKG_PATH) ) {
  28.             $this->mFolder = STORAGE_PKG_PATH.$pSubdir;
  29.             $this->mUrl STORAGE_PKG_URL.$pSubdir;
  30.         elseifdefined"TEMP_PKG_PATH" )) {
  31.             $this->mFolder = TEMP_PKG_PATH.$pSubdir;
  32.         elseifgetenv"TMP" )) {
  33.             $this->mFolder = getenv"TMP" )."/".$pSubdir;
  34.         else {
  35.             $this->mFolder = "/tmp/".$pSubdir;
  36.         }
  37.  
  38.         if!is_dir$this->mFolder && !mkdir_p$this->mFolder )) {
  39.             error_log'Can not create the cache directory: '.$this->mFolder );
  40.         }
  41.     }
  42.  
  43.     /**
  44.      * getCacheFile
  45.      *
  46.      * @param string $pFile 
  47.      * @access public
  48.      * @return filepath on success, FALSE on failure
  49.      */
  50.     function getCacheFile$pFile {
  51.         if!empty$pFile )) {
  52.             return $this->mFolder."/".$pFile;
  53.         else {
  54.             return FALSE;
  55.         }
  56.     }
  57.  
  58.     /**
  59.      * getCacheUrl will get the URL to the cache file - only works when you're using BitCache with the UseStorage option
  60.      *
  61.      * @param string $pFile 
  62.      * @access public
  63.      * @return fileurl on success, FALSE on failure
  64.      */
  65.     function getCacheUrl$pFile {
  66.         if!empty$this->mUrl && !empty$pFile )) {
  67.             return $this->mUrl.'/'.$pFile;
  68.         }
  69.     }
  70.  
  71.     /**
  72.      * Used to check if an object is cached.
  73.      *
  74.      * @param string $pFile name of the file we want to check for
  75.      * @param numeric $pModTime Pass in the modification time you wish to check against
  76.      * @access public
  77.      * @return true if cached object exists
  78.      */
  79.     function isCached$pFile$pModTime FALSE {
  80.         if!empty$pFile && is_readable$this->getCacheFile$pFile ))) {
  81.             // compare the cache filemtime to the desired file
  82.             ifis_numeric$pModTime )) {
  83.                 $isModified filemtime$this->getCacheFile$pFile )) $pModTime );
  84.             }
  85.             returnempty$isModified ));
  86.         else {
  87.             return FALSE;
  88.         }
  89.     }
  90.  
  91.     /**
  92.      * Used to retrieve an object if cached.
  93.      *
  94.      * @param pKey the unique identifier used to retrieve the cached item
  95.      * @return object if cached object exists
  96.      */
  97.     function readCacheFile$pFile {
  98.         if$this->isCached$pFile )) {
  99.             $cacheFile $this->getCacheFile$pFile );
  100.             if$h fopen$cacheFile'r' )) {
  101.                 $ret fread$hfilesize$cacheFile ) );
  102.                 fclose$h );
  103.             }
  104.         }
  105.  
  106.         return!empty$ret $ret NULL );
  107.     }
  108.  
  109.     /**
  110.      * Used to remove a cached object.
  111.      *
  112.      * @param pKey the unique identifier used to retrieve the cached item
  113.      */
  114.     function expungeCacheFile$pFile {
  115.         if$this->isCached$pFile )) {
  116.             unlink$this->getCacheFile$pFile ));
  117.         }
  118.     }
  119.  
  120.     /**
  121.      * remove the entire cache in the cache folder
  122.      *
  123.      * @access public
  124.      * @return TRUE on success, FALSE on failure
  125.      */
  126.     function expungeCache({
  127.         // the only places we can write to in bitweaver are temp and storage
  128.         $subdir str_replaceSTORAGE_PKG_PATH""$this->mFolder );
  129.         if(( strpos$this->mFolderSTORAGE_PKG_PATH === && $subdir != "users" && $subdir != "common" || strpos$this->mFolderTEMP_PKG_PATH === {
  130.             $ret unlink_r$this->mFolder );
  131.             if!is_dir$this->mFolder )) {
  132.                 mkdir_p$this->mFolder );
  133.             }
  134.         }
  135.         return $ret;
  136.     }
  137.  
  138.     /**
  139.      * writeCacheFile
  140.      *
  141.      * @param string $pFile file to write to
  142.      * @param string $pData string to write to file
  143.      * @access public
  144.      * @return void 
  145.      */
  146.     function writeCacheFile$pFile$pData {
  147.         if!empty$pData && !empty$pFile )) {
  148.             if$h fopen$this->getCacheFile$pFile )'w' )) {
  149.                 fwrite$h$pData );
  150.                 fclose$h );
  151.             }
  152.         }
  153.     }
  154. }
  155. ?>

Documentation generated on Wed, 29 Jul 2015 13:55:46 +0000 by phpDocumentor 1.5.0-lsces