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. 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 full-size slide

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

    View full-size slide

  3. 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 full-size slide

  4. ENVIRONMENT
    Resque (+ Scheduler)
    Ohm
    EventMachine
    PubSub

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  7. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  11. 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 full-size slide

  12. PRODUCTION
    Debian / OSX
    Heroku
    Easy (foreman)

    View full-size slide

  13. @ damln
    www.dln.name

    View full-size slide