Skip to content

Instantly share code, notes, and snippets.

@ThePixelDeveloper
Created June 4, 2011 17:42
Show Gist options
  • Save ThePixelDeveloper/1008113 to your computer and use it in GitHub Desktop.
Save ThePixelDeveloper/1008113 to your computer and use it in GitHub Desktop.
<?php
/**
* When given a coaster ID, this method will recalculate the number of downloads
* a coaster has had. The reason for manual queries is because it's around 300% faster than
* the current method.
*
* Use this function only when you want to bulk update a lot of coasters at the
* same time.
*
* @param type $id
* @return type
*/
public static function reload_download_count_fast($id)
{
$id = (int) $id;
$current = (int) DB::query(Database::SELECT, 'SELECT downloads FROM files WHERE id = :id')
->param(':id', $id)
->execute()
->get('downloads', 0);
$count = (int) DB::query(Database::SELECT, 'SELECT COUNT(*) AS total FROM file_logs WHERE file_id = :file_id')
->param(':file_id', $id)
->execute()
->get('total', 0);
if ($current !== $count)
{
DB::update('files')
->set(array('downloads' => $count))
->where('id', '=', $id)
->execute();
}
return $count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment