Development

Changeset 5675

You must first sign up to be able to contribute.

Changeset 5675

Show
Ignore:
Timestamp:
10/25/07 03:11:28 (6 years ago)
Author:
Jonathan.Todd
Message:

sfThumbnailCache: Added cache lifetime, made cache mtime aware

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfThumbnailCachePlugin/config/app.yml.dist

    r4126 r5675  
    11all: 
    2   image_magick_convert:   /usr/bin/convert 
    3   image_magick_identify:  /usr/bin/identify 
     2  thumbnail_cache: 
     3    cache_file_life: 3600 # Keep cached files for 1 hour 
     4    image_magick_convert:   /usr/bin/convert 
     5    image_magick_identify:  /usr/bin/identify 
  • plugins/sfThumbnailCachePlugin/lib/sfThumbnailCache.class.php

    r4880 r5675  
    100100  function __construct($file_master = null, $width = null, $height = null, 
    101101                       $scale = true, $inflate = true, $type = null, 
    102                        $file_thumb = null
     102                       $file_thumb = null, $mtime = null, $create_thumb = true
    103103  { 
    104104    if(!$file_master) 
     
    125125    $this->cache_url_path = '/uploads/sfThumbnailCache'; 
    126126     
    127     $this->createThumbnail(); 
    128        
     127    $this->mtime = $mtime ? $mtime : null; 
     128     
     129    if($create_thumb) 
     130    { 
     131      if(!$this->cacheIsValid()) 
     132      { 
     133        //echo "<br>Making thumb<BR>"; 
     134        $this->createThumbnail(); 
     135      } 
     136    } 
    129137  } 
    130138   
     
    134142  public static function getInstance($file_master = null, $width = null, $height = null, 
    135143                                     $scale = true, $inflate = true, $type = null, 
    136                                      $file_thumb = null) 
     144                                     $mtime = null, $file_thumb = null) 
    137145  { 
    138146    static $sfThumbnailCache; 
     
    142150      // Get instance 
    143151      $className = 'sfThumbnailCache'; 
    144       $sfThumbnailCache = new $className($file_master, $width, $height, $scale, $inflate, $type, $file_thumb);   
     152      $sfThumbnailCache = new $className($file_master, $width, $height, $scale, $inflate, $type, $mtime, $file_thumb);   
    145153    } 
    146154 
     
    153161  public static function getNewInstance($file_master = null, $width = null, $height = null, 
    154162                                     $scale = true, $inflate = true, $type = null, 
    155                                      $file_thumb = null) 
     163                                     $mtime = null, $file_thumb = null) 
    156164  { 
    157165    // Get instance 
    158166    $className = 'sfThumbnailCache'; 
    159     $sfThumbnailCache = new $className($file_master, $width, $height, $scale, $inflate, $type, $file_thumb);   
     167    $sfThumbnailCache = new $className($file_master, $width, $height, $scale, $inflate, $type, $mtime, $file_thumb);   
    160168    return $sfThumbnailCache; 
    161169  } 
    162170   
     171  /** 
     172   * Instantiates and returns a new sfThumbnailCache object without trying to create thumbnail 
     173   */ 
     174  public static function getNewInstanceNoMaster($file_thumb = null,$width = null, $height = null, 
     175                                     $scale = true, $inflate = true, $type = null, $mtime = null) 
     176  { 
     177    // Get instance 
     178    $className = 'sfThumbnailCache'; 
     179    $sfThumbnailCache = new $className('none', $width, $height, $scale, $inflate, $type, $file_thumb, $mtime, false);  
     180    return $sfThumbnailCache; 
     181  } 
    163182   
    164183  public function exists() 
     
    217236   
    218237  /** 
     238   * Is the cached file still valid based on mtime and cache time limit 
     239   *  
     240   * If mtime is set and is older or equal to cached file then cache is valid 
     241   * If mtime is not set and cache life is less than cache time limit then cache is valid 
     242   */ 
     243  public function cacheIsValid() 
     244  { 
     245 
     246    if(!$this->exists()) 
     247      return false; 
     248 
     249    // Use mtime if it's set 
     250    if($this->mtime) 
     251    { 
     252      // File requested is newer than cache 
     253      if($this->mtime > $this->getMTime()) 
     254        return false; 
     255 
     256      // File requested is older or same as cache 
     257      return true ;  
     258    } 
     259 
     260    // Use cache life time 
     261    $cache_life = time() - $this->getMTime(); 
     262    $valid_cache_life = sfConfig::get('app_thumbnail_cache_cache_file_life'); 
     263    return ($cache_life > $valid_cache_life) ? false : true; 
     264 
     265  } 
     266 
     267  public function getMTime() 
     268  { 
     269    return filemtime($this->getPath()); 
     270  } 
     271   
     272  /** 
    219273   * Get name of thumbnail file 
    220274   */ 
     
    232286  private function createThumbnail() 
    233287  { 
    234      
    235     // Do we already have thumb of the given size 
    236     if($this->exists()) 
    237       return $this->getPath(); 
    238288 
    239289    // Create thumb dir if it doesn't exist 
     
    242292    // We need to create a thumbnail 
    243293    $thumb = new sfThumbnail($this->width, $this->height, $this->scale, $this->inflate, $this->quality); 
    244     $thumb = new sfThumbnail($this->width, $this->height, $this->scale, $this->inflate, $this->quality, 'sfImageMagickAdapter', array('convert' => sfConfig::get('app_image_magick_convert'), 'identify' => sfConfig::get('app_image_magick_identify'))); 
     294    $thumb = new sfThumbnail($this->width, $this->height, $this->scale, $this->inflate, $this->quality, 'sfImageMagickAdapter', array('convert' => sfConfig::get('app_thumbnail_cache_image_magick_convert'), 'identify' => sfConfig::get('app_thumbnail_cache_image_magick_identify'))); 
    245295    $thumb->loadFile($this->file_master); 
    246296    $thumb->save($this->getPath(), $this->type); 
    247297    $thumb->freeAll(); 
     298     
     299    // Set the access and modification to match S3 time 
     300    if($this->mtime) 
     301    { 
     302      if(!touch($this->getPath(),$this->mtime)) 
     303        throw new Exception("Could not set last modification time"); 
     304    } 
    248305     
    249306    return $this->getPath();