diff options
author | Karl 'vollmerk' Vollmer <vollmer@ampache.org> | 2008-05-12 05:58:17 +0000 |
---|---|---|
committer | Karl 'vollmerk' Vollmer <vollmer@ampache.org> | 2008-05-12 05:58:17 +0000 |
commit | 3634ba80946b818de7f0505ed44d947e70dd41ec (patch) | |
tree | 03749fb9cc1f1fc6ef157ac187cea48f1f1a7098 /lib/class/rating.class.php | |
parent | 3e36e0b01e843ec8d4e8a63a72e5f7425921dab8 (diff) | |
download | ampache-3634ba80946b818de7f0505ed44d947e70dd41ec.tar.gz ampache-3634ba80946b818de7f0505ed44d947e70dd41ec.tar.bz2 ampache-3634ba80946b818de7f0505ed44d947e70dd41ec.zip |
added in some caching and add the database upgrade that will make the taging mostly work
Diffstat (limited to 'lib/class/rating.class.php')
-rw-r--r-- | lib/class/rating.class.php | 55 |
1 files changed, 40 insertions, 15 deletions
diff --git a/lib/class/rating.class.php b/lib/class/rating.class.php index 0519672e..086084ea 100644 --- a/lib/class/rating.class.php +++ b/lib/class/rating.class.php @@ -24,7 +24,7 @@ * This is an amalgamation(sp?) of code from SoundOfEmotion * to track ratings for songs, albums and artists. */ -class Rating { +class Rating extends database_object { /* Provided vars */ var $id; // The ID of the object who's ratings we want to pull @@ -56,31 +56,56 @@ class Rating { return true; } // Constructor + + /** + * build_cache + * This attempts to get everything we'll need for this page load in a single query, saving + * the connection overhead + * //FIXME: Improve logic so that misses get cached as average + */ public static function build_cache($type, $ids) { - $idlist = '(' . implode(',', $ids) . ')'; - $sql = "SELECT `rating`, object_id FROM `rating` WHERE `user`='$user_id' AND `object_id` in $idlist AND `object_type`='$type'"; - global $rating_cache; - $rating_cache = array(); - $db_results = Dba::query($sql); - while ($results = Dba::fetch_assoc($db_results)) { - $rating_cache[intval($results['object_id'])] = $results; - } - } + + $user_id = Dba::escape($GLOBALS['user']->id); + + $idlist = '(' . implode(',', $ids) . ')'; + $sql = "SELECT `rating`, `object_id` FROM `rating` WHERE `user`='$user_id' AND `object_id` IN $idlist " . + "AND `object_type`='$type'"; + $db_results = Dba::query($sql); + + while ($row = Dba::fetch_assoc($db_results)) { + $rating[$row['id']] = $row['rating']; + } + + $user_cache_name = 'rating_' . $type . '_user'; + + foreach ($ids as $id) { + parent::add_to_cache($user_cache_name,$id,intval($rating[$id])); + } // end foreach + + + } // build_cache + /** * get_user * Get the user's rating this is based off the currently logged * in user. It returns the value */ public function get_user($user_id) { - global $rating_cache; - if (isset($rating_cache[intval($this->id)])); - return $rating_cache[intval($this->id)]['rating']; - $user_id = Dba::escape($user_id); + + $id = intval($this->id); - $sql = "SELECT `rating` FROM `rating` WHERE `user`='$user_id' AND `object_id`='$this->id' AND `object_type`='$this->type'"; + if (parent::is_cached('rating_' . $this->type . '_user',$id)) { + return parent::get_from_cache('rating_' . $this->type . '_user',$id); + } + + $user_id = Dba::escape($user_id); + + $sql = "SELECT `rating` FROM `rating` WHERE `user`='$user_id' AND `object_id`='$id' AND `object_type`='$this->type'"; $db_results = Dba::query($sql); $results = Dba::fetch_assoc($db_results); + + parent::add_to_cache('rating_' . $this->type . '_user',$id,$results['rating']); return $results['rating']; |