Index: karma.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/karma/karma.module,v retrieving revision 1.35 diff -u -F^f -r1.35 karma.module --- karma.module 25 Aug 2006 01:06:35 -0000 1.35 +++ karma.module 12 Sep 2006 00:43:37 -0000 @@ -97,6 +97,13 @@ function karma_get_user_rating($type, $o $id = ($type == 'comment' ? $object->cid : $object->nid); if (!isset($ratings[$type][$object->nid])) { + $cache = cache_get('karma:'. $user->uid .':'. $type .':'. $object->nid); + if (is_object($cache)) { + $ratings[$type][$object->nid] = unserialize($cache->data); + } + } + + if (!isset($ratings[$type][$object->nid])) { $ratings[$type][$object->nid] = array(); switch ($type) { case 'comment': @@ -110,6 +117,7 @@ function karma_get_user_rating($type, $o while ($rating = db_fetch_object($result)) { $ratings[$type][$object->nid][$rating->id] = $rating->rating; } + cache_set('karma:'. $user->uid .':'. $type .':'. $object->nid, serialize($ratings[$type][$object->nid])); } return $ratings[$type][$object->nid][$id]; @@ -141,6 +149,13 @@ function karma_get_rating($type, $object ); } + if (!isset($ratings[$type][$object->nid]) && !$clear) { + $cache = cache_get('karma:'. $type .':'. $object->nid); + if (is_object($cache)) { + $ratings[$type][$object->nid] = unserialize($cache->data); + } + } + if (!isset($ratings[$type][$object->nid])) { $ratings[$type][$object->nid] = array(); switch ($type) { @@ -155,6 +170,7 @@ function karma_get_rating($type, $object while ($rating = db_fetch_object($result)) { $ratings[$type][$object->nid][$type == 'comment' ? $rating->id : 'node'] = array($rating->rating, $rating->count); } + cache_set('karma:'. $type .':'. $object->nid, serialize($ratings[$type][$object->nid])); } return $ratings[$type][$object->nid][$type == 'comment' ? $object->cid : 'node']; @@ -254,10 +270,12 @@ function karma_rate_submit($form_id, $fo switch ($form_values['type']) { case 'comment': $id_type = 'cid'; + $object = db_fetch_object(db_query('SELECT uid, nid FROM {comments} WHERE cid = %d', $form_values['id'])); break; case 'node': $id_type = 'nid'; + $object = db_fetch_object(db_query('SELECT uid, nid FROM {node} WHERE nid = %d', $form_values['id'])); break; } @@ -272,16 +290,11 @@ function karma_rate_submit($form_id, $fo db_query("DELETE FROM {karma_objects} WHERE id = %d AND type = '%s'", $form_values['id'], $id_type); db_query("INSERT INTO {karma_objects} (id, type, rating, count) SELECT id, '%s', avg(rating), count(uid) FROM {karma_ratings} WHERE id = %d AND type = '%s' GROUP BY id", $id_type, $form_values['id'], $id_type); - // Update the comment owner's karma - switch ($form_values['type']) { - case 'comment': - $object = db_fetch_object(db_query('SELECT uid FROM {comments} WHERE cid = %d', $form_values['id'])); - break; + // Clear the cache. + cache_clear_all('karma:'. $user->uid .':'. $form_values['type'] .':'. $object->nid); + cache_clear_all('karma:'. $form_values['type'] .':'. $object->nid); - case 'node': - $object = db_fetch_object(db_query('SELECT uid FROM {node} WHERE nid = %d', $form_values['id'])); - break; - } + // Update the comment owner's karma karma_update_user_karma($object->uid); // todo ??