From ba63d6ff12a42c75e434f3d481cf6bcef79cb18f Mon Sep 17 00:00:00 2001 From: croxton Date: Tue, 21 Jul 2015 13:38:41 +0100 Subject: [PATCH] Queue static file writes so that third-party add-ons using the template_post_parse hook are able to parse the final output --- .../third_party/stash/models/stash_model.php | 92 ++++++++++--------- 1 file changed, 50 insertions(+), 42 deletions(-) diff --git a/system/expressionengine/third_party/stash/models/stash_model.php b/system/expressionengine/third_party/stash/models/stash_model.php index 89a2105..df7fc6f 100755 --- a/system/expressionengine/third_party/stash/models/stash_model.php +++ b/system/expressionengine/third_party/stash/models/stash_model.php @@ -84,11 +84,8 @@ function insert_key($key, $bundle_id = 1, $session_id, $site_id = 1, $expire = 0 // cache result to eliminate need for a query in future gets self::$keys[$cache_key] = $parameters; - - // write to static file cache? - $this->write_static_cache($key, $bundle_id, $site_id, $parameters); - return TRUE; + return TRUE; } else { @@ -122,34 +119,28 @@ function update_key($key, $bundle_id = 1, $session_id = '', $site_id = 1, $expir $cache_key = $key . '_'. $bundle_id .'_' .$site_id . '_' . $session_id; $data = array( - 'created' => $this->EE->localize->now, - 'expire' => $expire + 'key_name' => $key, + 'bundle_id' => $bundle_id, + 'site_id' => $site_id, + 'created' => $this->EE->localize->now, + 'expire' => $expire, ); if ($parameters !== NULL) { $data += array('parameters' => $parameters); - } - - $where = array( - 'key_name' => $key, - 'bundle_id' => $bundle_id, - 'site_id' => $site_id - ); + } if ( ! empty($session_id)) { - $where += array('session_id' => $session_id); + $data += array('session_id' => $session_id); } - if ($result = $this->queue_update('stash', $cache_key, $data, $where)) + if ($result = $this->queue_update('stash', $cache_key, $data)) { // update cache self::$keys[$cache_key] = $parameters; - // write to static file cache? - $this->write_static_cache($key, $bundle_id, $site_id, $parameters); - return TRUE; } else @@ -693,21 +684,21 @@ function bundle_entry_count($bundle_id, $site_id = 1) /** * Check if the variable can be cached as a static file and write it * - * @param string $key - * @param integer $bundle_id - * @param string $parameters + * @param array $data * @return boolean */ - protected function write_static_cache($key, $bundle_id, $site_id, $parameters) + protected function write_static_cache($data) { + if ( ! isset($data['bundle_id'])) return FALSE; + // write to static file cache? - if ($this->_can_static_cache($bundle_id)) + if ($this->_can_static_cache($data['bundle_id'])) { // extract the associated uri from the variable key - $uri = $this->parse_uri_from_key($key); + $uri = $this->parse_uri_from_key($data['key_name']); // cache that mother - return $this->_write_file($uri, $site_id, $parameters); + return $this->_write_file($uri, $data['site_id'], $data['parameters']); } return FALSE; @@ -926,10 +917,9 @@ protected function queue_insert($table, $cache_key, $data) * @param string $table The table to insert into * @param string $cache_key Unique key identifying this variable * @param Array $data Array in form of "Column" => "Value", ... - * @param Array $where Array in form of "Column" => "Value", ... * @return boolean */ - protected function queue_update($table, $cache_key, $data, $where) + protected function queue_update($table, $cache_key, $data) { if ( ! isset(self::$queue->updates[$table])) { @@ -937,10 +927,7 @@ protected function queue_update($table, $cache_key, $data, $where) } // overwrite any existing key, so that only the last update to same cached item actually runs - self::$queue->updates[$table][$cache_key] = array( - 'data' => $data, - 'where' => $where - ); + self::$queue->updates[$table][$cache_key] = $data; return TRUE; } @@ -953,25 +940,46 @@ public function process_queue() { // batch inserts - must run first foreach(self::$queue->inserts as $table => $data) - { + { $this->insert_ignore_batch($table, $data); + + // write to static file cache + foreach($data as $query) + { + $this->write_static_cache($query); + } } // run each queued update in order if (count(self::$queue->updates) > 0) { - // using innodb, so we can wrap with a transaction - // to save a tiny bit of overhead - $this->db->trans_start(); - foreach(self::$queue->updates as $table => $updates) - { - foreach($updates as $query) - { - $this->db->where($query['where']); - $this->db->update($table, $query['data']); + // update keys + foreach(self::$queue->updates as $table => $data) + { + foreach($data as $query) + { + // required columns + $where = array( + 'key_name' => $query['key_name'], + 'bundle_id' => $query['bundle_id'], + 'site_id' => $query['site_id'] + ); + unset($query['key_name'], $query['bundle_id'], $query['site_id']); + + if ( isset($query['session_id'])) + { + $where += array('session_id' => $query['session_id']); + unset($query['session_id']); + } + + // update db + $this->db->where($where); + $this->db->update($table, $query); + + // update static file cache + $this->write_static_cache($query); } } - $this->db->trans_complete(); } // reset the queue