Posted by mikeytown2 on December 8, 2011 at 2:55am
2 followers
| Project: | File (Field) Paths |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | major |
| Assigned: | Unassigned |
| Status: | postponed |
Issue Summary
Checking to see if a file exists for every module can't be cached by APC and is thus slow. Caching a list of .inc files to require would be ideal.
http://drupalcode.org/project/filefield_paths.git/blob/82ab88d5cacbd1d9f...
<?php
foreach (module_list() as $module) {
if (file_exists($file = dirname(__FILE__) . "/modules/{$module}.inc")) {
require_once $file;
}
}
?>Code used to be in filefield_paths_init()
Comments
#1
Stock:
Total Self: 5.5ms
Total Cumulative: 13ms
Memcache->get calls: 0
This Patch:
Total Self: 0.4ms
Total Cumulative: 1.0ms
Memcache->get calls: 1
#2
This is the correct patch.
#3
Thanks mikeytown2,
I appreciate the rewrite.
I reworked it a little bit to be more generic as I use the code in most of my modules:
/*** Include additional files.
*/
$info = pathinfo(__FILE__);
// Get files to include from cache.
$files = cache_get("{$info['filename']}_inc_modules");
if (!empty($files->data)) {
$files = $files->data;
}
else {
$files = array();
// If not in the cache, get files from module list.
foreach (module_list() as $module) {
if (file_exists($file = "{$info['dirname']}/modules/{$module}.inc")) {
$files[] = $file;
}
}
cache_set("{$info['filename']}_inc_modules", $files, 'cache', CACHE_TEMPORARY);
}
// Add in the files.
foreach ($files as $file) {
if (!include_once $file) {
// Clear the cache if a file is missing.
cache_clear_all("{$info['filename']}_inc_modules");
}
}
// Clean up because we are outside of a function call.
unset($info, $files, $file, $module);
Committed to 6.x-1.x and 7.x-1.x.
Cheers,
Deciphered.
#4
Unfortunately I have identified an issue with this code, which is that when new modules are enabled the cache is not reset and therefore the relevant include files are not included and as far as a user would be able to tell the functionality is not available/is broken.
This is a fairly major issue, which if cannot be resolved will require the reversal of this fix.
#5
Reverted code for both 6.x-1.x and 7.x-1.x. Ideally there would be a way to clear the cache whenever a module is enabled or disabled, but hook_enable() and hook_disabled() only trigger on the module being enabled or disabled.
Will revisit as I do like the optimisation, but can't have the module becoming unusable.
#6
for D7 this shouldn't be an issue do to module_implements() caching what hooks are being used. For D6 this is a concern but it's a minor one when D7 is taken into account. Also see #557542-179: Cache module_implements() .
As for D6 this is the callback on the modules page system_modules_submit(). Near the bottom you can see a bunch of clear cache calls. Having this cache cleared inside of hook_menu should do the trick.
#7
I had considered that, but my concern is then would that be respected by 'drush en' or alternate module enabling methods. I will definitely need to do a bit more digging before I can finalize a solution.
#8
hook_menu is fairly safe. If you enable a module and you can't get to its configuration page, that's a broken workflow.