Changeset 1289


Ignore:
Timestamp:
03/09/10 14:09:29 (3 years ago)
Author:
nick_ramsay
Message:

[Branch 1.2] New caching function for HTML blocks. Unlike smartCache, this doesn't check database table update times. It just expires the cached file after a specified time.

Location:
branches/1.2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/1.2/Hotaru.php

    r1286 r1289  
    14771477     
    14781478     
     1479    /** 
     1480     * Cache HTML without checking for database updates 
     1481     * 
     1482     * This function caches blocks of HTML code 
     1483     * 
     1484     * @param int $timeout timeout in minutes before cache file is deleted 
     1485     * @param string $html block of HTML to cache 
     1486     * @param string $label name to identify the cached file 
     1487     * @return bool 
     1488     */ 
     1489    public function cacheHTML($timeout = 0, $html = '', $label = '') 
     1490    { 
     1491        require_once(LIBS . 'Caching.php'); 
     1492        $caching = new Caching(); 
     1493        return $caching->cacheHTML($this, $timeout, $html, $label); 
     1494    } 
     1495     
     1496     
    14791497 /* ************************************************************* 
    14801498 * 
  • branches/1.2/libs/Caching.php

    r1270 r1289  
    245245        return $last_update; 
    246246    } 
     247     
     248     
     249    /** 
     250     * Cache HTML without checking for database updates 
     251     * 
     252     * This function caches blocks of HTML code 
     253     * 
     254     * @param int $timeout timeout in minutes before cache file is deleted 
     255     * @param string $html block of HTML to cache 
     256     * @param string $label name to identify the cached file 
     257     * @return bool 
     258     */ 
     259    public function cacheHTML($h, $timeout = 0, $html = '', $label = '') 
     260    { 
     261        if (!$timeout || (HTML_CACHE_ON != 'true') || !$label) { return false; } 
     262         
     263        $cache_length = $timeout*60;   // seconds 
     264        $cache = CACHE . 'html_cache/'; 
     265        $file = $cache . $label . ".cache"; 
     266         
     267        if (!$html) { 
     268            // we only want to read the cache if it exists, hence no $html passed to this function 
     269            if (file_exists($file)) { 
     270                $content = file_get_contents($file); 
     271                $last_modified = filemtime($file); 
     272                //echo "last modified: " . $last_modified . "<br />"; 
     273                if ($last_modified <= (time() - $cache_length)) {  
     274                    // delete cache 
     275                    unlink($file); 
     276                    return false; 
     277                } else { 
     278                    return $content;    // return the HTML to display 
     279                } 
     280            } else { 
     281                return false; 
     282            } 
     283        } 
     284         
     285        // if we're here, we need to make or rewrite the cache 
     286         
     287        $fp = fopen($file, "w"); 
     288 
     289        if (flock($fp, LOCK_EX)) { // do an exclusive lock 
     290            ftruncate($fp, 0);  // truncate file 
     291            fwrite($fp, $html); // write HTML 
     292            flock($fp, LOCK_UN); // release the lock 
     293        } else { 
     294            echo "Couldn't get the lock for the HTML cache!"; 
     295        } 
     296         
     297        fclose($fp); 
     298        return true; // the calling function already has the HTML to output 
     299    } 
    247300} 
    248301?> 
Note: See TracChangeset for help on using the changeset viewer.