Commit 918d406d authored by Jakob Petsovits's avatar Jakob Petsovits

Centralize the file cache in field_file.inc for all concerned functions

(load, save_upload, save, delete).
parent fda84c00
......@@ -11,46 +11,41 @@
* Whether to reset the internal file_load cache.
*/
function field_file_load($fid, $reset = NULL) {
static $files = array();
if (empty($fid)) {
return FALSE;
// Reset internal cache.
if (isset($reset)) {
_field_file_cache(NULL, TRUE);
}
// Reset internal cache.
if ($reset) {
$files = array();
if (empty($fid)) {
return array();
}
$files = _field_file_cache();
// Serve file from internal cache if available.
if (!empty($fid) && !empty($files[$fid])) {
return (array)$files[$fid];
}
if (empty($files[$fid])) {
if (is_numeric($fid)) {
$file = db_fetch_object(db_query('SELECT f.* FROM {files} f WHERE f.fid = %d', $fid));
}
else {
$file = db_fetch_object(db_query("SELECT f.* FROM {files} f WHERE f.filepath = '%s'", $fid));
}
if (is_numeric($fid)) {
$file = db_fetch_object(db_query('SELECT f.* FROM {files} f WHERE f.fid = %d', $fid));
}
else {
$file = db_fetch_object(db_query("SELECT f.* FROM {files} f WHERE f.filepath = '%s'", $fid));
}
if (!$file) {
return array();
}
if (!$file) {
return FALSE;
}
foreach(module_implements('file') as $module) {
$function = $module .'_file';
$function('load', $file);
}
foreach(module_implements('file') as $module) {
$function = $module .'_file';
$function('load', $file);
// Cache the fully loaded file for later reusability.
$files = _field_file_cache($file);
}
// Cache the fully loaded value by both fid and filepath.
// use non-copying objects to save memory.
$files[$fid] = $file;
$files[$file->filepath] = $file;
// Cast to array for field. hook_file() expects objects as well as
// core file functions.
return (array)$file;
return (array)$files[$fid];
}
/**
......@@ -92,6 +87,7 @@ function field_file_save_upload($source, $validators = array(), $dest = FALSE, $
$function = $module .'_file';
$function('insert', $file);
}
_field_file_cache($file); // cache the file in order to minimize load queries
return (array)$file;
}
......@@ -131,6 +127,8 @@ function field_file_save($node, &$file) {
$function = $module .'_file';
$function('update', $file);
}
_field_file_cache($file); // update the cache, in case the file has changed
return $file;
}
......@@ -160,11 +158,36 @@ function field_file_delete($file, $force = FALSE) {
// database, so UIs can still find the file in the database.
if (file_delete($file->filepath)) {
db_query('DELETE FROM {files} WHERE fid = %d', $file->fid);
_field_file_cache(NULL, $file); // delete the file from the cache
return TRUE;
}
return FALSE;
}
/**
* Internal cache, in order to minimize database queries for loading files.
*/
function _field_file_cache($file = NULL, $reset = FALSE) {
static $files = array();
// Reset internal cache.
if (is_object($reset)) { // file object, uncache just that one
unset($files[$reset->fid]);
unset($files[$reset->filepath]);
}
else if ($reset) { // TRUE, delete the whole cache
$files = array();
}
// Cache the file by both fid and filepath.
// Use non-copying objects to save memory.
if (isset($file)) {
$files[$file->fid] = $file;
$files[$file->filepath] = $file;
}
return $files;
}
/**
* A silent version of file.inc:file_check_directory it's only talkative
* on errors.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment