diff --git a/modules/hosting/package/hosting_package.instance.inc b/modules/hosting/package/hosting_package.instance.inc index 4561b40..8dbca3a 100644 --- a/modules/hosting/package/hosting_package.instance.inc +++ b/modules/hosting/package/hosting_package.instance.inc @@ -95,8 +95,15 @@ function hosting_package_instance_create(&$instance) { * * @see hosting_package_instances_load() */ -function hosting_package_instance_load($param) { - return _hosting_package_instances_load($param); +function hosting_package_instance_load($param, $reset = FALSE) { + $instance = hosting_package_instances_load($param, $reset); + + // $instance could be FALSE or an empty array. + if (!empty($instance)){ + $instance = reset($instance); + } + + return $instance; } /** @@ -107,57 +114,83 @@ function hosting_package_instance_load($param) { * $param is an array then the key/value pairs will be used as a WHERE clause * in the form "WHERE key0 = value0 AND ... AND keyN = valueN". Both the keys * and values of the $param array must be strings. + * @param boolean $reset + * If reset is TRUE, the cache of objects matching the current $param will be + * rebuilt and fresh results will be returned. Defaults to FALSE. * * @return mixed * Returns an array of instance objects keyed by the instance iid. If there * are no matches an empty array will be returned. If there was an error FALSE * will be returned. */ -function hosting_package_instances_load($param) { - return _hosting_package_instances_load($param, TRUE); +function hosting_package_instances_load($param, $reset = FALSE) { + return _hosting_package_instances_load($param, $reset); } /** * Internal function for hosting_package_instance(s)_load. + * + * This function has an internal static cache of results. + * The parameters for this function are identical to + * hosting_package_instances_load(). + * + * @see hosting_package_instances_load(). */ -function _hosting_package_instances_load($param, $multiple = FALSE) { +function _hosting_package_instances_load($param, $reset = FALSE) { + static $cache = array(); + $cid = ''; $arguments = array(); + if (is_numeric($param)) { $cond = 'iid = %d'; $arguments[] = $param; + $cid .= 'iid:' . $param . '.'; } elseif (is_array($param)) { // Turn the conditions into a query. foreach ($param as $key => $value) { + // Ignore data types that will break our cache and/or query. + if(!is_string($key) + || is_array($value) + || is_object($value)) { + continue; + } + $cond[] = $key ." = '%s'"; $arguments[] = $value; + $cid .= $key . ':' . $value . '.'; + } + if(empty($cond) || empty($cid)) { + return FALSE; } $cond = implode(' AND ', $cond); } else { return FALSE; } - $instances = array(); - $result = db_query("SELECT - n.title, r.type, p.package_type, p.nid, - i.iid, i.version, i.version_code, i.schema_version, - i.status, p.description, p.short_name, p.old_short_name, i.rid, - i.package_id - FROM {hosting_package_instance} i - LEFT JOIN {hosting_package} p ON p.nid=i.package_id - LEFT JOIN {node} n ON p.nid=n.nid - LEFT JOIN {node} r ON r.nid=i.rid - LEFT JOIN {hosting_platform} h ON r.nid=h.nid - WHERE " . $cond, $arguments); - - while ($instance = db_fetch_object($result)) { - $instance->languages = _hosting_package_instances_load_languages($instance->iid); - if (!$multiple) { - return $instance; + + if(!isset($cache[$cid] || $reset)) { + $instances = array(); + $result = db_query("SELECT + n.title, r.type, p.package_type, p.nid, + i.iid, i.version, i.version_code, i.schema_version, + i.status, p.description, p.short_name, p.old_short_name, i.rid, + i.package_id + FROM {hosting_package_instance} i + LEFT JOIN {hosting_package} p ON p.nid=i.package_id + LEFT JOIN {node} n ON p.nid=n.nid + LEFT JOIN {node} r ON r.nid=i.rid + LEFT JOIN {hosting_platform} h ON r.nid=h.nid + WHERE " . $cond, $arguments); + + while ($instance = db_fetch_object($result)) { + $instance->languages = _hosting_package_instances_load_languages($instance->iid); + $instances[$instance->iid] = $instance; } - $instances[$instance->iid] = $instance; + $cache[$cid] = $instances; } - return $instances; + + return $cache[$cid]; } /**