Skip to content

Commit

Permalink
Queue static file writes so that third-party add-ons using the templa…
Browse files Browse the repository at this point in the history
…te_post_parse hook are able to parse the final output
  • Loading branch information
croxton committed Jul 21, 2015
1 parent aa8d396 commit ba63d6f
Showing 1 changed file with 50 additions and 42 deletions.
92 changes: 50 additions & 42 deletions system/expressionengine/third_party/stash/models/stash_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -926,21 +917,17 @@ 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]))
{
self::$queue->updates[$table] = array();
}

// 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;
}

Expand All @@ -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
Expand Down

0 comments on commit ba63d6f

Please sign in to comment.