Hotaru Docs


Memory Cache

When a page is loaded, it's quite common for the same database queries to be repeated. The memory cache is an associative array that is used to store the first round of SQL queries and their results in memory so that they don't need to be repeated again in the same page load.

Here's how it's used in the getPost function:

 * Gets a single post from the database
 * @return array|false
public function getPost($h, $post_id = 0)
    // Build SQL
    $query = "SELECT * FROM " . TABLE_POSTS . " WHERE post_id = %d ORDER BY post_date DESC";
    $sql = $h->db->prepare($query, $post_id);
    // Create temp cache array
    if (!isset($h->vars['tempPostCache'])) { $h->vars['tempPostCache'] = array(); }
    // If this query has already been read once this page load, we should have it in memory...
    if (array_key_exists($sql, $h->vars['tempPostCache'])) {
        // Fetch from memory
        $post = $h->vars['tempPostCache'][$sql];
    } else {
        // Fetch from database
        $post = $h->db->get_row($sql);
        $h->vars['tempPostCache'][$sql] = $post;
    if ($post) { return $post; } else { return false; }

Other caching options: Standard Database Cache | Hotaru smartCache | HTML Cache

Getting StartedDesign and LayoutPlugin DevelopmentAdvanced TopicsFunction ReferenceTroubleshooting