Source for file DiskCache.php

Documentation is available at DiskCache.php

  1. <?
  2.  
  3. /** A disk cache.
  4.  *
  5.  * @package    php-lastfm-api
  6.  * @author  Felix Bruns <felixbruns@web.de>
  7.  * @version    1.0
  8.  */
  9. final class DiskCache extends Cache {
  10.     /** The directory where the cache data is stored.
  11.      *
  12.      * @var        string 
  13.      * @access    private
  14.      */
  15.     private $directory;
  16.  
  17.     /** Constructor that sets up the DiskCache.
  18.      *
  19.      * @param    string    $directory    The directory to use. (Optional)
  20.      * @access    public
  21.      */
  22.     public function __construct($directory null){
  23.         parent::__construct();
  24.  
  25.         if($directory != null){
  26.             $this->directory $directory;
  27.         }
  28.         else{
  29.             $this->directory sys_get_temp_dir().'/lastfm.cache';
  30.         }
  31.  
  32.         if(!file_exists($this->directory)){
  33.             @mkdir($this->directory);
  34.         }
  35.  
  36.         if(!is_dir($this->directory)){
  37.             $this->directory dirname($this->directory);
  38.         }
  39.     }
  40.  
  41.     /** Checks if data associated with a hash exists in the cache.
  42.      *
  43.      * @param    string    $hash    The hash of the entry to be checked.
  44.      * @return    boolean            true if the entry exists, otherwise false.
  45.      * @access    public
  46.      */
  47.     public function contains($hash){
  48.         return file_exists($this->directory.'/'.$hash.'.xml');
  49.     }
  50.  
  51.     /** Loads data from the cache.
  52.      *
  53.      * @param    string    $hash    The hash of the entry to be loaded.
  54.      * @return    string            The cached data.
  55.      * @access    public
  56.      */
  57.     public function load($hash){
  58.         return @file_get_contents($this->directory.'/'.$hash.'.xml');
  59.     }
  60.  
  61.     /** Removes data from the cache.
  62.      *
  63.      * @param    string    $hash    The hash of the entry to be removed.
  64.      * @access    public
  65.      */
  66.     public function remove($hash){
  67.         @unlink($this->directory.'/'.$hash.'.xml');
  68.         @unlink($this->directory.'/'.$hash.'.meta');
  69.     }
  70.  
  71.     /** Stores data in the cache.
  72.      *
  73.      * @param    string    $hash        The hash of the data to be stored.
  74.      * @param    string    $data        The data to be stored.
  75.      * @param    string    $expiration    The expiration time of the data (unix timestamp).
  76.      * @access    public
  77.      */
  78.     public function store($hash$data$expiration){
  79.         file_put_contents($this->directory.'/'.$hash.'.xml'$data);
  80.         file_put_contents($this->directory.'/'.$hash.'.meta'$expiration);
  81.     }
  82.  
  83.     /** Removes all data from the cache.
  84.      *
  85.      * @access    public
  86.      */
  87.     public function clear(){
  88.         @rmdir($this->directory);
  89.         @mkdir($this->directory);
  90.     }
  91.  
  92.     /** Checks if data associated with a hash is expired.
  93.      *
  94.      * @param    string    $hash    The hash of the entry to be checked.
  95.      * @return    boolean            true if the entry is expired, otherwise false.
  96.      * @access    public
  97.      */
  98.     public function isExpired($hash){
  99.         $expiration @file_get_contents($this->directory.'/'.$hash.'.meta');
  100.  
  101.         return (time(intval($expiration));
  102.     }
  103. }
  104.  
  105. ?>

Documentation generated on Mon, 22 Dec 2008 16:57:35 +0100 by phpDocumentor 1.4.1