Ticket #580: cache.patch
| File cache.patch, 5.7 kB (added by PugFish, 7 months ago) |
|---|
-
application/config/config.php
67 67 $config['extension_prefix'] = 'MY_'; 68 68 69 69 /** 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 /** 70 77 * Additional resource paths, or "modules". Each path can either be absolute 71 78 * or relative to the docroot. Modules can include any resource that can exist 72 79 * in your application directory, configuration files, controllers, views, etc. -
system/core/Config.php
1 1 <?php defined('SYSPATH') or die('No direct script access.'); 2 2 /** 3 3 * Loads configuration files and retrieves keys. This class is declared as final. 4 * 4 * 5 5 * $Id$ 6 6 * 7 7 * @package Core … … 185 185 return $config; 186 186 } 187 187 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 188 199 $configuration = array(); 189 200 190 201 // Find all the configuartion files matching the name … … 200 211 } 201 212 } 202 213 214 // Add merged configuration to cache and save it 215 $found[$name] = $configuration; 216 Kohana::save_cache('configs', $found); 217 203 218 return $configuration; 204 219 } 205 220 -
system/core/Kohana.php
688 688 */ 689 689 public static function find_file($directory, $filename, $required = FALSE, $ext = FALSE, $use_cache = TRUE) 690 690 { 691 static $found = array();691 static $found; 692 692 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 } 695 698 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, '.'); 698 701 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 699 708 if ($directory === 'config' OR $directory === 'i18n' OR $directory === 'l10n') 700 709 { 701 710 $fnd = array(); … … 703 712 // Search from SYSPATH up 704 713 foreach (array_reverse(Config::include_paths()) as $path) 705 714 { 706 if (is_file($path.$search .EXT)) $fnd[] = $path.$search.EXT;715 if (is_file($path.$search)) $fnd[] = $path.$search; 707 716 } 708 717 709 718 // If required and nothing was found, throw an exception 710 719 if ($required == TRUE AND $fnd === array()) 711 720 throw new Kohana_Exception('core.resource_not_found', Kohana::lang('core.'.inflector::singular($directory)), $filename); 712 721 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; 714 726 } 715 727 else 716 728 { 717 // Users can define their own extensions, css, xml, html, etc718 $ext = ($ext == FALSE) ? EXT : '.'.ltrim($ext, '.');719 720 729 // Find the file and return its filename 721 730 foreach (Config::include_paths() as $path) 722 731 { 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 } 725 740 } 726 741 727 742 // If the file is required, throw an exception 728 743 if ($required == TRUE) 729 744 throw new Kohana_Exception('core.resource_not_found', Kohana::lang('core.'.inflector::singular($directory)), $filename); 730 745 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; 732 750 } 733 751 } 734 752 735 753 /** 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 /** 736 811 * Lists all files and directories in a resource path. 737 812 * 738 813 * @param string directory to search
