Acts As Most Popular

Download Acts As Most Popular

Post on 03-Jul-2015

611 views

Category:

Technology

4 download

Embed Size (px)

DESCRIPTION

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

TRANSCRIPT

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