Ticket #580: cache.patch

File cache.patch, 5.7 kB (added by PugFish, 7 months ago)
  • application/config/config.php

     
    6767$config['extension_prefix'] = 'MY_'; 
    6868 
    6969/** 
     70 * Length of time of the internal cache in seconds. 0 or FALSE means no caching. 
     71 * The internal cache stores file paths and config entries across requests 
     72 * and can give significant speed improvements at the expense of delayed updating. 
     73 */ 
     74$config['internal_cache'] = 60; 
     75 
     76/** 
    7077 * Additional resource paths, or "modules". Each path can either be absolute 
    7178 * or relative to the docroot. Modules can include any resource that can exist 
    7279 * in your application directory, configuration files, controllers, views, etc. 
  • system/core/Config.php

     
    11<?php defined('SYSPATH') or die('No direct script access.'); 
    22/** 
    33 * Loads configuration files and retrieves keys. This class is declared as final. 
    4  *  
     4 * 
    55 * $Id$ 
    66 * 
    77 * @package    Core 
     
    185185                        return $config; 
    186186                } 
    187187 
     188                static $found; 
     189 
     190                if ($found === NULL) 
     191                { 
     192                        // First load, try to fetch from cache 
     193                        $found = Kohana::load_cache('configs'); 
     194                } 
     195 
     196                if (isset($found[$name])) 
     197                        return $found[$name]; 
     198 
    188199                $configuration = array(); 
    189200 
    190201                // Find all the configuartion files matching the name 
     
    200211                        } 
    201212                } 
    202213 
     214                // Add merged configuration to cache and save it 
     215                $found[$name] = $configuration; 
     216                Kohana::save_cache('configs', $found); 
     217 
    203218                return $configuration; 
    204219        } 
    205220 
  • system/core/Kohana.php

     
    688688         */ 
    689689        public static function find_file($directory, $filename, $required = FALSE, $ext = FALSE, $use_cache = TRUE) 
    690690        { 
    691                 static $found = array(); 
     691                static $found; 
    692692 
    693                 $search = $directory.'/'.$filename; 
    694                 $hash   = sha1($search.$ext); 
     693                if ($found === NULL) 
     694                { 
     695                        // First load, try to fetch from cache 
     696                        $found = Kohana::load_cache('file_paths'); 
     697                } 
    695698 
    696                 if ($use_cache AND isset($found[$hash])) 
    697                         return $found[$hash]; 
     699                // Users can define their own extensions, css, xml, html, etc 
     700                $ext = ($ext == FALSE) ? EXT : '.'.ltrim($ext, '.'); 
    698701 
     702                $search = $directory.'/'.$filename.$ext; 
     703                //$hash   = sha1($search.$ext); this doesn't seem to be needed 
     704 
     705                if ($use_cache AND isset($found[$search])) 
     706                        return $found[$search]; 
     707 
    699708                if ($directory === 'config' OR $directory === 'i18n' OR $directory === 'l10n') 
    700709                { 
    701710                        $fnd = array(); 
     
    703712                        // Search from SYSPATH up 
    704713                        foreach (array_reverse(Config::include_paths()) as $path) 
    705714                        { 
    706                                 if (is_file($path.$search.EXT)) $fnd[] = $path.$search.EXT; 
     715                                if (is_file($path.$search)) $fnd[] = $path.$search; 
    707716                        } 
    708717 
    709718                        // If required and nothing was found, throw an exception 
    710719                        if ($required == TRUE AND $fnd === array()) 
    711720                                throw new Kohana_Exception('core.resource_not_found', Kohana::lang('core.'.inflector::singular($directory)), $filename); 
    712721 
    713                         return $found[$hash] = $fnd; 
     722                        // Add paths to cache and save it 
     723                        $found[$search] = $fnd; 
     724                        $use_cache AND Kohana::save_cache('file_paths', $found); 
     725                        return $fnd; 
    714726                } 
    715727                else 
    716728                { 
    717                         // Users can define their own extensions, css, xml, html, etc 
    718                         $ext = ($ext == FALSE) ? EXT : '.'.ltrim($ext, '.'); 
    719  
    720729                        // Find the file and return its filename 
    721730                        foreach (Config::include_paths() as $path) 
    722731                        { 
    723                                 if (is_file($path.$search.$ext)) 
    724                                         return $found[$hash] = $path.$search.$ext; 
     732                                $path = $path.$search; 
     733                                if (is_file($path)) 
     734                                { 
     735                                        // Add path to cache and save it 
     736                                        $found[$search] = $path; 
     737                                        $use_cache AND Kohana::save_cache('file_paths', $found); 
     738                                        return $path; 
     739                                } 
    725740                        } 
    726741 
    727742                        // If the file is required, throw an exception 
    728743                        if ($required == TRUE) 
    729744                                throw new Kohana_Exception('core.resource_not_found', Kohana::lang('core.'.inflector::singular($directory)), $filename); 
    730745 
    731                         return $found[$hash] = FALSE; 
     746                        // Add path to cache and save it 
     747                        $found[$search] = FALSE; 
     748                        $use_cache AND Kohana::save_cache('file_paths', $found); 
     749                        return FALSE; 
    732750                } 
    733751        } 
    734752 
    735753        /** 
     754         * Save data to a cache file. 
     755         * This is only used interally by Kohana and should not 
     756         * be used in place of the Cache library. 
     757         * 
     758         * @param   string   filename of cache 
     759         * @param   array    data to save 
     760         * @return  boolean 
     761         */ 
     762        public static function save_cache($file, array $data = NULL) 
     763        { 
     764                $file = APPPATH.'cache/kohana_'.$file; 
     765                if ($data === NULL) 
     766                { 
     767                        // Delete file 
     768                        unlink($file); 
     769                } 
     770                else 
     771                { 
     772                        // Write cache contents to file 
     773                        return (bool) file_put_contents($file, serialize($data)); 
     774                } 
     775        } 
     776 
     777        /** 
     778         * Load data from a cache file. 
     779         * This is only used interally by Kohana and should not 
     780         * be used in place of the Cache library. 
     781         * 
     782         * @param   string  filename of cache 
     783         * @return  array 
     784         */ 
     785        public static function load_cache($file) 
     786        { 
     787                if ($cache_time = Config::item('core.internal_cache')) 
     788                { 
     789                        $filepath = APPPATH.'cache/kohana_'.$file; 
     790                        if (file_exists($filepath)) 
     791                        { 
     792                                // Use file modification time to check cache time 
     793                                if (time() - filemtime($filepath) < $cache_time) 
     794                                { 
     795                                        // Return contents of cache 
     796                                        return unserialize(file_get_contents($filepath)); 
     797                                } 
     798                                else 
     799                                { 
     800                                        // Cache has expired, delete it 
     801                                        Kohana::save_cache($file); 
     802                                } 
     803                        } 
     804                } 
     805 
     806                // No cache was loaded, return empty 
     807                return array(); 
     808        } 
     809 
     810        /** 
    736811         * Lists all files and directories in a resource path. 
    737812         * 
    738813         * @param   string   directory to search