Acts As Most Popular

780 views

Published on

Presenting acts_as_most_popular: A plugin to cache most popular lists, such as most viewed videos, common to many social networking applications. acts_as_most_popular is built on top of Cache Money

Published in: Technology, Sports
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
780
On SlideShare
0
From Embeds
0
Number of Embeds
25
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Acts As Most Popular

  1. 1. acts_as_most_popular Wolfram Arnold RubyFocus
  2. 2. User Activity Tracking
  3. 3. <ul><li>Join between: </li></ul><ul><ul><li>Viewable entity </li></ul></ul><ul><ul><ul><li>(user profile, image, post, video ...) </li></ul></ul></ul><ul><ul><li>Activity tracking </li></ul></ul><ul><ul><ul><li>(viewings, comments, ratings ...) </li></ul></ul></ul>
  4. 4. <ul><li>Item.find(:all, </li></ul><ul><li>:select => 'items.*, </li></ul><ul><li>COUNT(*) AS viewing_count', </li></ul><ul><li>:joins => :viewings, </li></ul><ul><li>:limit => limit, </li></ul><ul><li>:group => 'viewings.item_id', </li></ul><ul><li>:order => 'viewing_count DESC') </li></ul>
  5. 5. <ul><li>acts_as_most_popular </li></ul>
  6. 6. acts_as_most_popular <ul><li>I want: </li></ul><ul><ul><li>Most popular list from cache </li></ul></ul><ul><ul><li>Cache populared from database once </li></ul></ul><ul><ul><li>List automatically kept sorted & indexed in cache </li></ul></ul><ul><li>I need: </li></ul><ul><ul><li>A caching framework </li></ul></ul><ul><ul><li>Ideally something common, transparent & flexible </li></ul></ul>
  7. 7. Cache Money
  8. 8. Cache Money <ul><li>Transparent </li></ul><ul><ul><li>find vs. get_cache </li></ul></ul><ul><li>Sequence </li></ul><ul><ul><li>AR Cache SQL </li></ul></ul><ul><ul><li>find -> get -> select </li></ul></ul><ul><ul><li>update -> set -> update </li></ul></ul><ul><ul><li>create -> add -> insert </li></ul></ul><ul><ul><li>destroy -> delete/expire -> delete </li></ul></ul><ul><li>Maintains indices automatically! </li></ul>
  9. 9. Cache Money <ul><li>Instance methods on models: </li></ul><ul><ul><li>get </li></ul></ul><ul><ul><li>set </li></ul></ul><ul><ul><li>repository </li></ul></ul><ul><li>Automatic key handling </li></ul><ul><ul><li>User.set(“new_key”) </li></ul></ul><ul><ul><li>-> Key: “User:1/new_key” </li></ul></ul>
  10. 10. Solution
  11. 11. acts_as_most_popular <ul><li>Additional index, sorted by activity count </li></ul><ul><ul><li>primed on first access </li></ul></ul><ul><ul><li>maintained via after_add, after_remove callbacks </li></ul></ul>
  12. 12. Definition <ul><li>class Item < ActiveRecord::Base </li></ul><ul><li>has_many :viewings </li></ul><ul><li>acts_as_most_popular </li></ul><ul><li>:activity_association => :viewings, </li></ul><ul><li>:limit => 5, </li></ul><ul><li>:db_finder_args => </li></ul><ul><li>{ :select => 'item_id, </li></ul><ul><li>COUNT(*) AS activity_count', </li></ul><ul><li>:group => 'item_id' } </li></ul><ul><li>end </li></ul>
  13. 13. Usage <ul><li>class StatsController < Application </li></ul><ul><li>def most_popular </li></ul><ul><li>@items = Item.most_popular </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
  14. 14. References <ul><li>Cache Money </li></ul><ul><li>http://magicscalingsprinkles.wordpress.com/2008/12/11/introducing-cache-money/ </li></ul><ul><li>http://github.com/nkallen/cache-money/tree/master </li></ul><ul><li>Images </li></ul><ul><li>http://www.flickr.com/photos/flirtykitty/142229288/ </li></ul><ul><li>http://www.flickr.com/photos/jojakeman/2434236126/ </li></ul>
  15. 15. <ul><li>Thank You! </li></ul><ul><li>Wolfram Arnold </li></ul><ul><li>RubyFocus </li></ul><ul><li>[email_address] </li></ul>

×