$ENTITY_CACHE = array();
/**
- * Cache subtypes and related class names once loaded.
+ * Cache subtypes and related class names.
*
* @global array $SUBTYPE_CACHE
* @access private
if ($subtype == "") {
return FALSE;
}
+
+ if (!$SUBTYPE_CACHE) {\r
+ populate_subtype_cache();\r
+ }
- // @todo use the cache before hitting database
- $result = get_data_row("SELECT * from {$CONFIG->dbprefix}entity_subtypes
- where type='$type' and subtype='$subtype'");
-
- if ($result) {
- if (!$SUBTYPE_CACHE) {
- $SUBTYPE_CACHE = array();
- }
-
- $SUBTYPE_CACHE[$result->id] = $result;
- return $result->id;
+ // use the cache before hitting database\r
+ $result = retrieve_cached_subtype($type, $subtype);\r
+ if ($result!==null) {\r
+ return $result->id;\r
}
return FALSE;
return false;
}
+ if (!$SUBTYPE_CACHE) {\r
+ populate_subtype_cache();\r
+ }\r
+
if (isset($SUBTYPE_CACHE[$subtype_id])) {
return $SUBTYPE_CACHE[$subtype_id]->subtype;
}
- $result = get_data_row("SELECT * from {$CONFIG->dbprefix}entity_subtypes where id=$subtype_id");
- if ($result) {
- if (!$SUBTYPE_CACHE) {
- $SUBTYPE_CACHE = array();
- }
+ return false;
+}
- $SUBTYPE_CACHE[$subtype_id] = $result;
- return $result->subtype;
+/**\r
+ * Retrieve subtype from the cache.\r
+ *
+ * @return stdClass|null\r
+ * @access private\r
+ */\r
+function retrieve_cached_subtype($type, $subtype) {\r
+ global $SUBTYPE_CACHE;\r
+\r
+ foreach ($SUBTYPE_CACHE as $id => $obj) {
+ if ($obj->type==$type && $obj->subtype==$subtype) {
+ return $obj;
+ }
}
+ return null;\r
+}
- return false;
+/**
+ * Fetch all suptypes from DB to local cache.
+ * @access private
+ */
+function populate_subtype_cache() {
+ global $CONFIG, $SUBTYPE_CACHE;
+
+ $results = get_data("SELECT * from {$CONFIG->dbprefix}entity_subtypes");
+
+ $SUBTYPE_CACHE = array();
+ foreach ($results as $result) {
+ $SUBTYPE_CACHE[$result->id] = $result;
+ }
}
/**
$type = sanitise_string($type);
$subtype = sanitise_string($subtype);
- // @todo use the cache before going to the database
- $result = get_data_row("SELECT * from {$CONFIG->dbprefix}entity_subtypes
- where type='$type' and subtype='$subtype'");
-
- if ($result) {
- if (!$SUBTYPE_CACHE) {
- $SUBTYPE_CACHE = array();
- }
-
- $SUBTYPE_CACHE[$result->id] = $result;
+ if (!$SUBTYPE_CACHE) {\r
+ populate_subtype_cache();\r
+ }\r
+
+ // use the cache before going to the database
+ $result = retrieve_cached_subtype($type, $subtype);
+ if ($result!==null) {
return $result->class;
}
return false;
}
+ if (!$SUBTYPE_CACHE) {\r
+ populate_subtype_cache();\r
+ }\r
+
if (isset($SUBTYPE_CACHE[$subtype_id])) {
return $SUBTYPE_CACHE[$subtype_id]->class;
}
- $result = get_data_row("SELECT * from {$CONFIG->dbprefix}entity_subtypes where id=$subtype_id");
-
- if ($result) {
- if (!$SUBTYPE_CACHE) {
- $SUBTYPE_CACHE = array();
- }
- $SUBTYPE_CACHE[$subtype_id] = $result;
- return $result->class;
- }
-
return NULL;
}
* @see get_entity()
*/
function add_subtype($type, $subtype, $class = "") {
- global $CONFIG;
+ global $CONFIG, $SUBTYPE_CACHE;
$type = sanitise_string($type);
$subtype = sanitise_string($subtype);
$class = sanitise_string($class);
$id = get_subtype_id($type, $subtype);
if ($id == 0) {
- return insert_data("insert into {$CONFIG->dbprefix}entity_subtypes"
+ $result = insert_data("insert into {$CONFIG->dbprefix}entity_subtypes"
. " (type, subtype, class) values ('$type','$subtype','$class')");
+
+ // add entry to cache
+ $obj = new stdClass();
+ $obj->id = $result;
+ $obj->type = $type;
+ $obj->subtype = $subtype;
+ $obj->class = $class;
+ $SUBTYPE_CACHE[$obj->id] = $obj;
+ \r
+ return $result;
}
return $id;
$type = sanitise_string($type);
$subtype = sanitise_string($subtype);
+ if (!$SUBTYPE_CACHE) {\r
+ populate_subtype_cache();\r
+ }\r
+
$result = update_data("UPDATE {$CONFIG->dbprefix}entity_subtypes
SET type = '$type', subtype = '$subtype', class = '$class'
WHERE id = $id