Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix Kohana's internal cache for Gallery's usage pattern.

Adds a core.internal_cache_read_only config variable to Kohana's internals.

Kohana's internal_cache for find_file wasn't working in Gallery because the cache would be emptied on each request after reading it from disk and before most lookups would run.
 1. Bootstrap sets initial core.modules (= include path): forge, kohana23_compat, gallery.
 2. Kohana::setup() loads find_file cache from disk.
 3. Gallery loads list of active modules and themes, and updates the core.modules value (=include path), which forces the internal find_file cache to be empties (which makes sense).
 4. Request processing starts, and thus 80% of all Kohana::find_file() triggered  is_file() invocations start off with an empty find_file cache.

In the case of my small Gallery installation, we're talking about 3100 is_file() invocations per request with or without internal_cache enabled. With this fix, this number is down to 800 invocations.

The basic idea is that we treat the cache as read only and don't write any (possibly dirty) values to it in memory until we're sure that the include path won't change later on in the request processing. Once we know the list of active modules and themes, we can update core.modules and finally flip the read-only state of the cache and start writing to it.
  • Loading branch information...
commit 5ddd7c9677b644396981de7df8176a3b168ffe21 1 parent be20309
@andyst andyst authored
View
6 application/config/config.php
@@ -67,6 +67,12 @@
* can give significant speed improvements at the expense of delayed updating.
*/
$config["internal_cache"] = FALSE;
+/**
+ * Enable or disable writing to the internal cache. Used by Gallery to treat
+ * the cache as read-only until all active modules and themes are in the
+ * include path.
+ */
+$config["internal_cache_read_only"] = TRUE;
$config["internal_cache_path"] = VARPATH . "tmp/";
/**
View
1  modules/gallery/helpers/gallery_event.php
@@ -25,6 +25,7 @@ class gallery_event_Core {
static function gallery_ready() {
identity::load_user();
theme::load_themes();
+ Kohana_Config::instance()->set('core.internal_cache_read_only', false);
locales::set_request_locale();
}
View
47 system/core/Kohana.php
@@ -365,13 +365,17 @@ public static function include_paths($process = FALSE)
// Add SYSPATH as the last path
Kohana::$include_paths[] = SYSPATH;
- // Clear cached include paths
- self::$internal_cache['find_file_paths'] = array();
- if ( ! isset(self::$write_cache['find_file_paths']))
+ if ( ! Kohana::config('core.internal_cache_read_only'))
{
- // Write cache at shutdown
- self::$write_cache['find_file_paths'] = TRUE;
- }
+ // Clear cached include paths
+ self::$internal_cache['find_file_paths'] = array();
+
+ if ( ! isset(self::$write_cache['find_file_paths']))
+ {
+ // Write cache at shutdown
+ self::$write_cache['find_file_paths'] = TRUE;
+ }
+ }
}
@@ -818,13 +822,17 @@ public static function find_file($directory, $filename, $required = FALSE, $ext
}
}
- if ( ! isset(Kohana::$write_cache['find_file_paths']))
+ if ( ! Kohana::config('core.internal_cache_read_only'))
{
- // Write cache at shutdown
- Kohana::$write_cache['find_file_paths'] = TRUE;
- }
+ Kohana::$internal_cache['find_file_paths'][$search] = $found;
- return Kohana::$internal_cache['find_file_paths'][$search] = $found;
+ if ( ! isset(Kohana::$write_cache['find_file_paths']))
+ {
+ // Write cache at shutdown
+ Kohana::$write_cache['find_file_paths'] = TRUE;
+ }
+ }
+ return $found;
}
/**
@@ -900,8 +908,7 @@ public static function list_files($directory, $recursive = FALSE, $ext = EXT, $p
public static function message($key, $args = array())
{
// Extract the main group from the key
- $group = explode('.', $key, 2);
- $group = $group[0];
+ list ($group, $subkey) = explode('.', $key, 2);
if ( ! isset(Kohana::$internal_cache['messages'][$group]))
{
@@ -913,17 +920,23 @@ public static function message($key, $args = array())
include $file[0];
}
- if ( ! isset(Kohana::$write_cache['messages']))
+ if ( ! isset(Kohana::$write_cache['messages']) && ! Kohana::config('core.internal_cache_read_only'))
{
// Write language cache
Kohana::$write_cache['messages'] = TRUE;
}
-
- Kohana::$internal_cache['messages'][$group] = $messages;
+ if ( ! Kohana::config('core.internal_cache_read_only'))
+ {
+ Kohana::$internal_cache['messages'][$group] = $messages;
+ }
+ }
+ else
+ {
+ $messages = Kohana::$internal_cache['messages'][$group];
}
// Get the line from cache
- $line = Kohana::key_string(Kohana::$internal_cache['messages'], $key);
+ $line = Kohana::key_string($messages, $subkey);
if ($line === NULL)
{
Please sign in to comment.
Something went wrong with that request. Please try again.