Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Redis with Ruby

Redis with Ruby

Damian Le Nouaille

April 04, 2012
Tweet

More Decks by Damian Le Nouaille

Other Decks in Programming

Transcript

  1. View Slide

  2. REDIS
    Redis is an open source, advanced key-
    value store. It is often referred to as a data
    structure server since keys can contain
    strings, hashes,lists, sets and sorted sets.
    gem ‘redis’

    View Slide

  3. USE
    Cache
    Session (cookies)
    gem ‘redis-rails’
    App::Application.config.session_store
    :redis_store,
    :servers => "redis://127.0.0.1:6379/1"

    View Slide

  4. Commands
    $redis =
    new Redis(:host => ‘127.0.0.7’, :port => 6379, :db => 0)
    $redis.set(‘my_key’, ‘awesome’)
    $redis.get(‘my_key’) => ‘awesome’
    $redis.del(‘my_key’)

    View Slide

  5. ENVIRONMENT
    Resque (+ Scheduler)
    Ohm
    EventMachine
    PubSub

    View Slide

  6. Resque (+Scheduler)
    Fast ²
    Queue
    YAML - Cron
    gem ‘resque’
    gem ‘resque-scheduler’,
    git: 'git://github.com/bvandenbos/resque-scheduler'

    View Slide

  7. Ohm
    ORM
    Flexible
    Extensible
    gem ‘ohm’
    gem ‘ohm-contrib’

    View Slide

  8. Ohm (model)
    class User < Ohm::Model
    attribute :name
    attribute :nickname
    include Ohm::Validations
    extend ActiveModel::Naming
    include ActiveModel::AttributeMethods
    index :name
    collection :messages, Message
    end

    View Slide

  9. Ohm (model)
    class User < Ohm::Model
    def persisted?
    false
    end
    def to_hash
    super.merge({:name => self.name})
    end
    end

    View Slide

  10. Ohm (model)
    class Message < Ohm::Model
    attribute :content
    include Ohm::Validations
    extend ActiveModel::Naming
    include ActiveModel::AttributeMethods
    index :content
    reference :user, User
    end

    View Slide

  11. EventMachine
    Node.js for Ruby
    em-WebSocket
    em-redis
    gem ‘eventmachine’
    gem ‘em-redis’

    View Slide

  12. PubSub
    $redis =
    new Redis(:host => ‘127.0.0.7’, :port => 6379, :db => 0)
    $redis.publish(‘my_channel’, ‘content’)
    $redis.subscribe(‘my_channel’) do |on|
    on.message do |channel, msg|
    puts msg
    end
    end
    $redis.publish(‘my_channel.*’, ‘content’)

    View Slide

  13. PRODUCTION
    Debian / OSX
    Heroku
    Easy (foreman)

    View Slide

  14. @ damln
    www.dln.name

    View Slide