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

  • 1. acts_as_most_popular Wolfram Arnold RubyFocus

2. User Activity Tracking 3.

  • Join between:
    • Viewable entity
      • (user profile, image, post, video ...)
    • Activity tracking
      • (viewings, comments, ratings ...)

4.

  • Item.find(:all,
  • :select => 'items.*,
  • COUNT(*) AS viewing_count',
  • :joins => :viewings,
  • :limit => limit,
  • :group => 'viewings.item_id',
  • :order => 'viewing_count DESC')

5.

  • acts_as_most_popular

6. acts_as_most_popular

  • I want:
    • Most popular list from cache
    • Cache populared from database once
    • List automatically kept sorted & indexed in cache
  • I need:
    • A caching framework
    • Ideally something common, transparent & flexible

7. Cache Money 8. Cache Money

  • Transparent
    • find vs. get_cache
  • Sequence
    • AR Cache SQL
    • find->get-> select
    • update-> set -> update
    • create-> add -> insert
    • destroy -> delete/expire -> delete
  • Maintains indices automatically!

9. Cache Money

  • Instance methods on models:
    • get
    • set
    • repository
  • Automatic key handling
    • User.set(new_key)
    • ->Key: User:1/new_key

10. Solution 11. acts_as_most_popular

  • Additional index, sorted by activity count
    • primed on first access
    • maintained via after_add, after_remove callbacks

12. Definition

  • class Item < ActiveRecord::Base
  • has_many :viewings
  • acts_as_most_popular
  • :activity_association => :viewings,
  • :limit => 5,
  • :db_finder_args =>
  • { :select => 'item_id,
  • COUNT(*) AS activity_count',
  • :group => 'item_id' }
  • end

13. Usage

  • class StatsController < Application
  • def most_popular
  • @items = Item.most_popular
  • end
  • end

14. References

  • Cache Money
  • http://magicscalingsprinkles.wordpress.com/2008/12/11/introducing-cache-money/
  • http://github.com/nkallen/cache-money/tree/master
  • Images
  • http://www.flickr.com/photos/flirtykitty/142229288/
  • http://www.flickr.com/photos/jojakeman/2434236126/

15.

  • Thank You!
  • Wolfram Arnold
  • RubyFocus
  • [email_address]